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

7,2 Integer Conversion                                               189


        * CVBTD converts the unsigned contents of D into an equivalent 5-digit ASCII decimal
        * number ending at the location passed in X. Register Y is unchanged.
        *
        CVBTD:    PSHX                     ; Save beginning address of string
                  LEAK     5, X            ; End address of string
                  PSHX                     ; Save on stack
        LI:       LDX     #10              ; Divide by 10
                  LDY     #0               ; High 16-bits of dividend (low 16-bits in D)
                  EDIV                     ; D is remainder Y is quotient
                  ADDB    #$30             ; ASCII conversion
                  PULX                     ; Restore output string pointer
                  STAB     1  r -X         ; Store character, move pointer
                  PSHX                     ; Save output string pointer
                  XGDY                     ; Remainder to D
                  CPX      2 , SP          ; At end of string?
                  BNE      LI              ; Continue
                  LEAS     4, SP           ; Balance stack
                  RTS                      ; Exit


                                      a. Using a Loop
        *          SUBROUTINE CVBTD converts unsigned binary number in D into five
        *         ASCII decimal digits ending at the location passed in X, using recursion.
         *
        CVBTD:    PSHX                     ; Save string pointer
                  LDY     #0               ; High dividend
                  LDX     #10              ; Divisor
                  EDIV                     ; Unsigned (Y:D) / X -> Y, remainder to D
                  ADDB    #$30             ; Convert remainder to ASCII
                  PULX                     ; Get string pointer
                  STAB 1, -X                ; Store in string
                  TFR     Y, D             ; Put quotient in D
                  TBEQ     D, LI           ; If zero, just exit
                  BSR     CVBTD            ; Convert quotient to decimal (recursively)
        LI:       RTS                      ; Return

                                    b. Using Recursion

                 Figure 7.8 Conversion from Binary to Decimal by Division by 10

        Looking at the expansion (3) we see that N4 is the quotient of the division of (D) by
        (K):(K + 1). If we divide the remainder by (K + 2):(K + 3), we get N3 for the quotient,
        and so forth. These quotients are all in binary, so that a conversion to ASCII is also
        necessary. The subroutine CVBTD of Figure 7.6 essentially uses this technique, except
        that the division is carried out by subtracting the largest possible multiple of each power
        of ten which does not result in a carry.
   207   208   209   210   211   212   213   214   215   216   217