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

190                                         Chapter? Arithmetic Operations


            An improved technique shown in Figure 7.7 uses division, rather than successive
        subtraction, to speed up the algorithm, but it has the same difficulties as the one in
        Figure 7.3; larger numbers require a larger table of constants. We now look at the other
        division scheme. For example, if the contents of D is divided by 10, the remainder is the
        binary expansion of NQ. Dividing this quotient by 10 yields a remainder equal to the
        binary expansion of Nj, and so on. See Figure 7.8a. This approach can be implemented
        by a recursive algorithm. See Figure 7.8b.
            Suppose that we wanted to convert a 16-bit unsigned binary integer in D to the
        equivalent decimal number using the best multiplication technique, in particular, the one
        that used (4) instead of (3). Write the binary expansion of the number bjs  , bo in
        decimal notation as




        and assume that the equivalent five-decimal digits are to be placed at the address passed in
        X, with one decimal digit stored per byte in binary. (We can convert each digit to ASCII
        later, if necessary.) After initializing the result to 0, we iteratively build the equivalent
        decimal number from the innermost parentheses above by repeating the following steps:


        * CVBTD converts the 16-bit unsigned number in D to an equivalent 6-digit BCD
        * number stored in 3 bytes at the address passed in by the calling routine in X.
        *
        CVBTD:    PSHD                     ; Save number to be converted
                  CLR      0,X
                  CLR      1,X
                  CLR     2, X             ; Initialize decimal number
                  MOVE    #16,1, -SP       ; Push count
        *
        CBD1:     ASL     2,SP
                  ROL     1, SP            ; Put next bit in C
                  LDAB    #2               ; 3-byte decimal addition
        *
        CBD2;     LDAA    B,X              ; Get ith byte
                  ADCA    B, X             ; Adding a number to itself doubles it
                  DAA                      ; Double in decimal
                  STAA    B, X             ; Put back ith byte
        *
                  DECB
                  BPL     CBD2             ; Count down and loop 3 bytes
                  DEC     0,SP
                  BNE     CBD1             ; Count down and loop 16 bits
        *
                  LEAS    3, SP            ; Balance stack
                  RTS

              Figure 7.9. Conversion from Binary to Decimal by Decimal Multiplication
   208   209   210   211   212   213   214   215   216   217   218