Page 223 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 223

200                                         Chapter 7 Arithmetic Operations


                   ORG       LCSAVE
        *
        *          SUBROUTINE DIV
        * DIV divides the unsigned next word on the stack into L, and pulls the next word
        *
        DIV:       PULX                    ; unstack return address
                   LEAS      - 4, SP       ; room for remainder right above dividend
                   PSHD                    ; save low 16 bits of divisor
                   PSHY                    ; save high 16 bits of divisor
                   MOVB      #32,1, -SP    ; count for 32 bits
                   PSHX                    ; put back return address
                   CLRA
                   CLRB
                   STD       REM,SP
                   STD       REM+2,SP
        *
        DIV1:      CLC                     ; divide loop
                   LDAA      #8            ; shift remainder and divisor: shift 8 bytes
                   LEAK      QUOT+3, SP    ; pointer for bottom of quotient-remainder
        DIV2:      ROL       1, X-
                   DBNE      A,DIV2
        *
                   LDY       REM, SP       ; subtract from partial product
                   LDD       REM+2 , SP    ; (note: 4 extra bytes on stack)
        *
                   SUED      DVS+2,SP
                   XGDY
                   SBCB      DVS+1,SP
                   SBCA      DVS,SP
                   XGDY
        *
                   BCS       DIV3          ; if borrow
                   STD       REM+2 , SP    ; then put it back
                   STY       REM,SP
                   INC       QUOT+3, SP    ; and put 1 into Isb of quotient
        DIV3:      DEC       COUNT, SP     ; counter is high byte of last operand
                   BNE       DIV1          ; count down - 32 bits collected
        *
                   PULX                    ; pull return
                   LEAS      9, SP         ; balance stack - remove divisor
                   PULY
                   PULD                    ; pop quotient
        DIVEXIT: JMP         0,X           ; return to caller


                                  Figure 7.18. Continued.
   218   219   220   221   222   223   224   225   226   227   228