Page 204 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 204
7,1 Multiplication and Division 181
0010
0110 ) 0001101
0011
0000
0110
0001
0000
0001
Another way of doing the bookkeeping of this last version is to first put all zeros in the
nibble A and put the dividend in nibble B so that the contents of A:B look like
which we will think of as our initial 8-bit state. Next, shift the bits of A:B left, putting
1 in the rightmost bit of B if the divisor can be subtracted from A without a carry,
putting in 0 otherwise. If the subtraction can be done without a carry, put the result of
the subtraction in A. Repeat this shift-subtract process three more times to get the
remainder in A and the quotient in B.
*
* DIVIDE divides the 1-byte unsigned number in B (dividend) by the
* 1-byte unsigned number in A (divisor), putting the quotient in B and
* the remainder in A. Register Y is unchanged.
*
DIVIDE: PSHA ; Save divisor
CLRA ; Expand dividend, fill with zeros
LDX #8 ; Initialize counter
*
LOOP: ASLD ; Shift dividend and quotient left
*
CMPA 0, SP ; Check if subtraction will leave positive rslt
BLO JUMP ; If so
*
SUBA 0, SP ; Subtract divisor
INCB ; Insert 1 in quotient
*
JUMP: DBNE X,LOOP ; Decrement counter
*
LEAS 1, SP ; Balance stack
RTS ; Return with quotient in B, remainder in A
Figure 72.8-Bit Unsigned Divide Subroutine