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

182                                         Chapter 7 Arithmetic Operations


            The 8-bit case is identical to the above except that A and B become bytes, our state
        A:B becomes sixteen bits, and there are eight shifts instead of four. The shift left on D
        can be done with ASLD, which always puts a zero in the rightmost bit of B. If the
        divisor can be subtracted from A without a carry, one needs to execute INCH and perform
        the subtraction from A. The complete subroutine is shown in Figure 7.2. You should be
        able to see how to adapt this subroutine for, say, dividing two unsigned 16-bit numbers
        or an unsigned 16-bit number by an 8-bit one. (When the dividend and the divisor are of
        different lengths, one has to check if a one has been shifted out when the state is shifted
        left, and if so, the divisor should be subtracted from the remainder part of the state. This
        prevents the remainder from being truncated.)
            We now discuss briefly the multiplication and division of signed integers with a
        special look at the multiplication of 8-bit signed integers. Using the usual rule for the
        sign of the product of two signed numbers, we can extend the multiplication subroutines
        of this section in a straightforward way to handle signed integers. Extending the
        subroutines for division in this way is also straightforward after recalling 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.
            There are also algorithms to multiply n-bit signed integers directly. We leave the
        details of this to more advanced treatments of arithmetic, because it is quite easy to
        modify any of the subroutines that we have presented for unsigned integers to work for
        signed integers. We illustrate the technique by showing how to modify the results of the
        MUL instruction to obtain an 8-bit signed multiply of Jl and $, which are 8-bit signed
                                                                               f
        integers located in registers A and B. Suppose & is represented as bits a-j,. . ., a0 and S
        is represented as by,. . ., bo- Then the numerical value of signed number % is,

                                            6
                                   7
                         # = -a 7 * 2  + a 6 * 2  +..,. + OQ * 2°
                           8                              8        7       7
        Now, by adding a? * 2  to both sides, thereby making 37 * 2  + -37 * 2  = a.j * 2
        we get, from the previous expression:
                                   8        7       6
                         (A + a 7 * 2 ) = a 7 * 2  + a 6 * 2  + . . . + ao * 2°
        (This addition of a constant is called biasing.) We recognize this equation's right side as
        an unsigned number A. It can be input to the MUL instruction, which actually
        multiplies unsigned A times unsigned B. The result in accumulator D is
                                            8
                                                         8
                                  (A+ a 7 * 2 )*(#+ b 7 * 2 )                (!)
        which, by multiplying out the terms, is
                                                     8
                                         8
                         A. * « + a 7 * -5 * 2  + b 7 * %. * 2  + a 7 * b 7 * 2 16  (2)
        We see that to get the first term J? * 2? from (2) requires subtracting the two middle terms
                                                      16
        of (2) from accumulator D. [The rightmost term of (2), 2 , does not appear in D and can
        be ignored.1 The subroutine in Figure 7.3a makes this adjustment in D, where we note
                                8
                        8
        that to subtract 2  * $ or 2  * %. from accumulator D, we subtract 'B or %. from
        accumulator A. See Figure 7.3. Subroutine SGNMUL behaves like an instruction that
        multiplies the signed contents of accumulator A with the signed contents of accumulator
        B, putting the signed result in D. The Z bit is not set correctly by SGNMUL, however.
   200   201   202   203   204   205   206   207   208   209   210