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

7.2 Integer Conversion                                               185


        where 0 £ q < b for 0 <. i < m. The sequence c m _[,.. . CQ is called an m-digit base-b
        representation of N. We are interested in going from the representation of N in one base
        to its representation in another base. There are two common schemes for this conversion
        that are based on multiplication and two schemes that are based on division. Although
        one of the division schemes is taught in introductory logic design courses, and you are
        likely to select it because you know it well, it does not turn out to be the most efficient
        to implement in a microcomputer. We look at all the schemes in general and then give
        examples of each to find the most promising one.
            The two multiplication schemes simply carry out (3), doing the arithmetic in the
        base that we want the answer in. There are two ways to do this. Either evaluate
        expression (3) as it appears or else nest the terms as shown.
             N = ( ... ( 0 + c m_, ) * b + c m_ 2) *b+.. . + c, )*b+c o      (4)

        The other two schemes involve division. Notice from (3) that if you divide N by b, the
        remainder is CQ. Dividing the quotient by b again yields c\, and so forth, until one of the
        quotients becomes 0. In particular, if one has a base-r representation of N and wants to
        go to a base-b representation, division of N by b is done in base-r arithmetic. You are

        *
        *    SUBROUTINE CVDTB puts the unsigned equivalent of five ASCII decimal digits
        *    pointed to by X into D.
        *
        SCRATCH: ds. b     6               ; Scratch area for product and multiplier
        Kt        dc.w     10000,1000,100,10,1 ; Coefficient Vector
        *
        CVDTB:    LDAB    #6               ; Clear scratch area
                  LDY     #SCRATCH
        Cl:       CLR     1,Y+
                  DBNE    B,C1
        *
                  LDAB    #5               ; Five terms to be evaluated
                  LDY     #K               ; Constants in vector K, Y = multiplicand address
        *
        C2:       LDAA     1,X+            ; Next ASCII digit into A
                  PSHX                     ; Save pointer for next character
                  SUBA    #$30             ; ASCII to binary
                  STAA    SCRATCH*5        ; Save in last byte in scratch
                  LDX     #SCRATCH+4       ; Get address of multiplier
                  EMACS   SCRATCH          ; Multiply and accumulate
                  LEAY    2, Y             ; Next multiplicand address
                  PULX                     ; Restore pointer for next character
                  DBNE    B, C2            ; Count down and loop
                  LDD     SCRATCH+2        ; Get number
                  RTS                      ; Return to Caller
          Figure 7.4. Conversion from Decimal to Binary by Multiplication by Powers of 10
   203   204   205   206   207   208   209   210   211   212   213