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

198                                         Chapter 7 Arithmetic Operations

        * SUBROUTINE COMPAR subtracts the next word on the stack from L, and pulls the
        * next word into L. Condition codes reflect top - next
        Jf!
        COMPAR: SUED      4 , SP    ; Compare with low 16 bits of operand
                  XGDY              ; Get high 16-bits
                  SBCB    3, SP     ; Compare with next 8 bits of operand
                  SBCA    2 , SP    ; Compare with high 8 bits of operand
        CPZRO:    TBNE    D, Ll     ; If high 16 bits of result are nonzero, go to clear Z
                  TBEQ    Y, L2     ; If low 16 bits of result are zero, leave Z alone
        Ll:       ANDCC #$FB        ; Clear Z bit
        L2:       PULX              ; Pull return address
                  PULY              ; Remove operand from stack
                  PULD              ; Remove operand from stack
                  JMP     0,X       ; Return to caller
                           Figure 7.15. 32-Bit Compare Subroutine


            Multiple-precision multiplication takes advantage of the EMUL instruction. The
        general 32-bit by 32-bit unsigned case is illustrated in Figure 7.16 and handled by the
        subroutine in Figure 7.17. A signed multiplication subroutine can be easily written that
        combines Figures 7.17 and 7.3. Unsigned division is shown in Figure 7.18. It can be
        modified to leave the remainder, rather than the quotient. For signed division, recall that
        the sign of the remainder is the same as the sign of the dividend and that the sign of the
        quotient is positive if the signs of the dividend and divisor are equal; otherwise, it is
        negative. A signed divide can be implemented with the unsigned divide and sign
        modification using the above rule. These subroutines can also be optimized. When a
        PUSH macro precedes an ADD subroutine, the combined operation can be done simply as
        seen in Figure 7.19.
            We have completed our examination of multiple-precision arithmetic for both signed
        and unsigned integers. With the techniques developed in this section and in the examples
        of the earlier chapters, you should be prepared to handle any arithmetic calculation with
        signed or unsigned long (32-bit) integers.


















                         Figure 7.16. Multiplication of 32-Bit Numbers
   216   217   218   219   220   221   222   223   224   225   226