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

188                                        Chapter? Arithmetic Operations


        *
        * CVBTD converts the unsigned contents of D into an equivalent 5-digit ASCII decimal
        * number at the location passed in X. Registers A, B, CC, and X are changed,
        *
        CVBTD:    LEAY    K+12,PCR         ; End address of power-of-tens
                  PSHY                     ; Save a local variable for CPY
                  LEAY    K, PCR           ; Point Y to power-of-tens
                  PSHY                     ; Save for LDY
                  PSHX                     ; Save output string pointer
        L1:       LDY      2, SP           ; Get address of powers-of-ten
                  LDX      2,Y+            ; Get a power of ten, move pointer
                  CPY      4, SP           ; At end of string?
                  BEQ     L2               ; Yes, exit
                  STY      2,SP            ; Save address of powers-of-ten
                  LDY     #0               ; High 16-bits of dividend (low 16-bits in D)
                  EDIV                     ; D is remainder Y is quotient
                  XGDY                     ; Put quotient in D
                  ADDB    #$30             ; ASCII conversion
                  PULX                     ; Restore output string pointer
                  STAB     1, X+           ; Store character, move pointer
                  PSHX                     ; Save output string pointer
                  XGDY                     ; Remainder to D
                  BRA     LI               ; Continue
        L2:       LEAS     6, SP           ; Balance stack
                  RTS                      ; Exit

             Figure 7.7 Conversion from Binary to Decimal by Division by Powers of Ten


            The remaining two techniques use division. The second division technique that
        divides by different numbers each time, getting the most significant digit first, suffers
        from the same malady as the first multiplication scheme. A lot of numbers have to be
        stored in a table, and that reduces static efficiency. The other division scheme, getting the
        least significant digits first, is the one most commonly taught in introductory courses on
        logic design. Although it is better than the last division scheme, it is going to be less
        useful on the 6812 than the multiplication schemes, because this microcomputer has no
        decimal divide instruction, and the divide routine will take up memory and be slow. Thus
        the best scheme for conversion from decimal to binary is the multiplication scheme that
        uses the nesting formula (4) to avoid the need to store all the powers of 10.
            Let's apply the division techniques to converting a binary number into ASCII
        decimal digits to be output to a terminal. In particular, suppose that we want to convert
        the unsigned 16-bit number in D into five ASCII decimal digits.
            Consider the division scheme that generates the most significant digit first. We
        could again have a table of constants in the subroutine with the directive


                         K    dc.w 10000,1000,100,10,1
   206   207   208   209   210   211   212   213   214   215   216