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

9.2 Expressions and Assignment Statements                           261


        One can complement accumulator D by complementing accumulator A and then
        complementing accumulator B. We might want to implement NEGD in the same way
        with a pair of instructions NEGA NEGB, but 1 is always added to A whether a carry is
        generated by adding 1 to B or not. Because NEGB sets C if the contents of B is nonzero,
        we can cancel the addition of 1 to A by NEGA, except when the contents of B is 0, by

                  NEGA
                  NEGB
                  SBCA #0

             Multiplication uses a multiply instruction EMUL or EMULS, depending on
        whether the operations is signed; for instance the expression in Figure 9.3, lui = Isc
        * guc, is
                   LDAA     2, SP  ; get global variable Isc
                   SEX     A, Y    ; upcast from char to int, copy to register Y
                   LDAB     $0800 ; get guc
                   CLRA            ; upcast from unsigned char to int
                   EMUL            ; multiply unsigned
                   STD      0, SP  ; put 16-bit result into local lui

            Similarly, division can be implemented using a divide instruction IDIV, EDIV, or
        EDIVS, depending on whether the operation is signed; for instance in Figure 9.3 the
        expression lui = lui / guc; (or equivalently, lui /= guc;) is implemented

                   STD      2, -SP ; save numerator from previous statement
                   LDAB     $ 8 0 0  ; get denominator guc
                   CLRA            ; upcast from unsigned char to unsigned int
                   TFR      D, X   ; put in X for IDIV
                   LDD      2  f SP+ ; get global variable lui
                   IDIV            ; divide X into D, putting quotient in X
                   STX      0, SP  ; put into local variable lui
            To discuss how increment and decrement operators are handled, we use main in
        Figure 9.4 as an example; it merely increments and decrements some variables.
            The simple increment and decrement instructions can be used directly with char or
        unsigned char variables .The statement in Figure 9.4, guc++;, is implemented

                   INC    $ 0 8 0 0  ; increment global variable guc

        and the statement Isc—; in Figure 9.4 is implemented
                   DEC    0, SP    ; decrement local variable Isc

        However, the increment and decrement instructions on int or unsigned int variables
        use a longer sequence. The first problem with this operator is that there is no 16-bit
        memory operand version of it, and the second problem is that the carry bit is not affected
        by the 8-bit version of this operator. The statement gsi++; in Figure 9.4 is coded as
   279   280   281   282   283   284   285   286   287   288   289