Morgan - Numerical Methods (523161), страница 7
Текст из файла (страница 7)
It is set if even and reset if odd.Sticky Bit. This useful flag can obviate the need for guard digits on certainarithmetic operations. Among the processors in Table l-2, it is found only onthe 80C196. It is set if, during a multiple right shift, more than one bit was shiftedinto the carry with a one in the carry at the end of the shift.Rounding and the Sticky BitA multiple shift to the right is a divide by some power of two. If the carry is set,the result is equal to the integer result plus l/2, but should we round up or down? Thisproblem is encountered frequently in integer arithmetic and floating point. Mostfloating-point routines have some form of extended precision so that rounding canbe performed. This requires storage, which usually defaults to some minimal datatype (the routines in Chapter 4 use a word). The sticky bit reduces the need for suchextended precision.
It indicates that during a right shift, a one was shifted into thecarry flag and then shifted out.Along with the carry flag, the sticky bit can be used for rounding. For example,suppose we wish to divide the hex value 99H by 16D. We can do this easily with afour-bit right shift. Before the shift, we have:Operand10011001Carry flag0Sticky bit0We shift the operand right four times with the following instruction:shrax, #425NUMERICAL METHODSDuring the shift, the Least Significant Bit (LSB) of the operand (a one) is shiftedinto the carry and then out again, setting the sticky bit followed by two zeros and afinal one. The operand now has the following form:Operand00001001Sticky bitCarry flag1 (from the last shift) 1 (because of the first oneshifted in and out of the carry)To round the result, check the carry flag.
If it’s clear, the bits shifted out were lessthan half of the LSB, and rounding can be done by truncation. If the carry is set, thebits shifted out were at least half of the LSB. Now, with the sticky bit, we can see ifany other bits shifted out during the divide were ones; if so, the sticky bit is set andwe can round up.Rounding doesn’t have to be done as described here, but however you do it thesticky bit can make your work easier.
Too bad it’s not available on more machines.BranchingYour ability to do combined jumps depends on the flags. All the processors listedin the table have the ability to branch, but some implement that ability on moresophisticated relationships. Instead of a simple “jump on carry,” you might find“jump if greater, ” “jump if less than or equal,” and signed and unsigned operations.These extra instructions can cut the size and complexity of your programs.Of the devices listed, the TMS34010, 80x86 and 80C196 have the richest set ofbranching instructions. These include branches on signed and unsigned comparisonsas well as branches on the state of the flags alone.InstructionsAdditionAdd.
Of course, to perform any useful arithmetic, the processor must be capableof some form of addition. This instruction adds two operands, signaling anyoverflow from the result by setting the carry.26NUMBERSAdd-with-Carry. The ability to add with a carry bit allows streamlined,multiprecision additions. In multibyte or multiword additions, the add instruction is usually used first; the add-with-carry instruction is used in each succeeding addition.
In this way, overflows from each one addition can ripple throughto the next.SubtractionSubtract. All the devices in Table l-2 can subtract except the 8048 and 8051.The 8051 uses the subtract-with-carry instruction to fill this need. On the 8048,to subtract one quantity (the subtrahend) from another (the minuend), you mustcomplement the subtrahend and increment it, then add it to the minuend-inother words, add the two’s complement to the minuend.Subtract-with-Carry. Again, the 8048 does not support this instruction, while allthe others do. Since the 8051 has only the subtract-with-carry instruction, it isimportant to see that the carry is clear before a subtraction is performed unlessit is a multiprecision operation.
The subtract-with-carry is used in multiprecisionsubtraction in the same manner as the add-with-carry is used in addition.Compare. This instruction is useful for boundary, magnitude and equalitychecks. Most implementations perform a comparison by subtracting one valuefrom another. This process affects neither operand, but sets the appropriate flags.Many microprocessors allow either signed or unsigned comparisons.MultiplicationMultiply. This instruction performs a standard unsigned multiply based on theword size of the particular microprocessor or microcontroller. Hardware canmake life easier. On the 8088 and 8086, this instruction was embarrassingly slowand not that much of a challenge to shift and add routines.
On later members ofthe 80x86 family, it takes a fraction of the number of cycles to perform, makingit very useful for multiprecision and single-precision work.Signed Multiply. The signed multiply, like the signed divide (which we’ll27NUMERICAL METHODSdiscuss in a moment), has limited use. It produces a signed product from twosigned operands on all data types up to and including the word size of themachine.
This is fine for tame applications, but makes the instruction unsuitablefor multiprecision work. Except for special jobs, it might be wise to employ ageneric routine for handling signed arithmetic. One is described in the nextchapter.DivisionDivide. A hardware divide simplifies much of the programmer’s work unless itis very, very slow (as it is on the 8088 and 8086). A multiply canextend the usefulrange of the divide considerably. The following chapter gives examples of howto do this.Signed Divide. Except in specialized and controlled circumstances, the signeddivide may not be of much benefit.
It is often easier and more expeditious tohandle signed arithmetic yourself, as will be shown in Chapter 2.Modulus. This handy instruction returns the remainder from the division of twooperands in the destination register. As the name implies, this instruction is veryuseful when doing modular arithmetic. This and signed modulus are available onthe TMS34010.Signed Modulus. This is the signed version of the earlier modulus instruction,here the remainder bears the sign of the dividend.Negation and SignsOne’s Complement.
The one’s complement is useful for logical operations anddiminished radix arithmetic (see Positive and Negative Numbers, earlier in thischapter). This instruction performs a bit-by-bit complement of the input argument; that is, it makes each one a zero and each zero a one.Two’s Complement. The argument is one’s complemented, then incremented by28NUMBERSone. This is how negative numbers are usually handled on microcomputers.lSign Extension. This instruction repeats the value of the MSB of the byte or wordthrough the next byte, word, or doubleword so the proper results can be obtainedfrom an arithmetic operation. This is useful for converting a small data type toa larger data type of the same sign for such operations as multiplication anddivision.Shifts, Rotates and NormalizationRotate.
This simple operation can occur to the right or the left. In the case of aROTATE to the right, each bit of the data type is shifted to the right; the LSB isdeposited in the carry, while a zero is shifted in on the left. If the rotate is to theleft, each bit is moved to occupy the position of the next higher bit in the data typeuntil the last bit is shifted out into the carry flag (see figure l-3).
On the Z80,some shifts put the same bit into the carry and the LSB of the byte you areshifting. Rotation is useful for multiplies and divides as well as normalization.Rotate-through-Carry. This operation is similar to the ROTATE above, exceptthat the carry is shifted into the LSB (in the case of a left shift), or the MSB (inthe case of a right shift). Like the ROTATE, this instruction is useful formultiplies and divides as well as normalization.Arithmetic Shift. This shift is similar to a right shift.
As each bit is shifted, thevalue of the MSB remains the same, maintaining the value of the sign.Normalization. This can be either a single instruction, as is the case on the80C196, or a set of instructions, as on the TMS34010. NORML will cause the80C196 to shift the contents of a doubleword register to the left until the MSBis a one, “normalizing” the value and leaving the number of shifts required in aregister. On the TMS34010, LMO leaves the number of bits required to shift adoubleword so that its MSB is one.
A multibit shift can then be used to normalizeit. This mechanism is often used in floating point and as a setup for binary tableroutines.29NUMERICAL METHODSFigure 1-3. Shifts and rotates.Decimal and ASCII InstructionsDecimal Adjust on Add. This instruction adjusts the results of the addition of twodecimal values to decimal. Decimal numbers cannot be added on a binarycomputer with guaranteed results without taking care of any intrabyte carriesthat occur when a digit in a position exceeds nine. On the 8086, this instructionaffects only the AL register.