
Logical, Shift and Rotate Instructions 155
will shift DO left by 10 bits. This will shift in 10 zeros, and the value
in DO after the shift will be C00016. Note that we are performing word
operations on the register. The bits shifted out of the low-order word are
not shifted into the high-order word.
Even though the 68000 has a number of multiply and divided instruc
tions, it is sometimes easier and faster to use a shift for these operations.
This only works for multiplication or division by a power of two. A left
shift will multiply by 2n, where n is the number of bits to shift. A right
shift (be sure to use ASR for a signed divide) will divide by 2n. Since
the bits shifted off the end are lost, there will not be a remainder. Also,
be aware that no rounding of the result is performed. This means that
positive numbers are truncated towards zero, and negative numbers are
truncated towards negative infinity. In other words, 5/2 will result in 2,
while —5/2 will result in —3. You can verify this for yourself by writing
the binary values for 5 and —5 and then shifting.
An interesting application of this use of the shift instructions is a mul
tiplication which is not a power of two. A simple unsigned multiplication
can be performed using the left shift, LSL. Simply check each bit position
in the multiplier to determine if it is a 1 or 0. If it is a 1, add the multipli
cand shifted left by the number of bits corresponding to the bit position
in the multiplier. Sum these partial products as we go along. Here is a
simple routine to perform this multiplication:
* UNSIGNED MULTIPLY
* DO = MULTIPLIER
Dl = MULTIPLICAND
D2 = PRODUCT
D3 = TEMP FOR MULTIPLICAND
D4 = SHIFT COUNT
NEXT:
CLR.L D2
CLEAR PRODUCT
MOVEQ.L
#-l,D4
SET UP SHIFT COUNT
TST.L
DO FINISHED?
BEQ
FINI
YES
ADDQ.L
#1,D4
SET UP COUNT FOR NEXT SHIFT
LSR.L
#1 / DO GET NEXT BIT OF MULTIPLIER
BCC NEXT
ZERO, CONTINUE
MOVE.L
D1/D3
COPY MULTIPLICAND
LSL. L
D4/D3
GET PARTIAL PRODUCT
ADD.L
D3/D2
SUM TO PRODUCT
BRA NEXT
FINI:
Notice that the shift count is initialized to —1. This is to ensure that the
first time it is used it will be zero. We drop out of the loop as soon as
the multiplier is zero. This allows the algorithm to operate faster than if
Kommentare zu diesen Handbüchern