Page 210 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 210
7.2 Integer Conversion 187
Doing our calculations iteratively from the inner pair of parentheses, we can get the
same result as before without storing any constants. A subroutine that does this is
shown in Figure 7.5. In this subroutine, there is only one local variable, except for a
program sequence to save the low 16 bits of the sum, so that the binding process can be
omitted. If we compare the two subroutines, we see that the second one has the clear edge
in terms of lines of code or static efficiency, particularly if you consider the ten bytes
used by the dc.w directive in the first subroutine. Furthermore, if one wanted to convert a
7-digit decimal number to a 32-bit binary number, the difference in the static efficiencies
of these two multiplication techniques would become even more pronounced. For
example, a 40-byte table of constants would be needed for the analog of the subroutine of
Figure 7.4, an increase of 30 bytes that is not needed by the corresponding analog of
Figure 7.5's subroutine. Finally, each subroutine can take the ASCII decimal digits
directly from the terminal, using subroutine INCH, effectively accessing the digits as a
character sequence rather than a vector. (See the problems at the end of the chapter.)
* CVBTD converts unsigned binary number in D into five ASCII decimal
* digits at the location passed in X. For each power of 10, subtract it as many
* times as possible from D without causing a carry. The number obtained,
* after ASCII conversion, is the ASCII decimal coefficient of that power of 10.
*
K: dc.w 10000,1000,100,10,1
*
CVBTD: LEAY K+10,PCR ; End of power-of-tens
PSHY ; Save a local variable for CPY
LEAY K, PCR ; Point Y to power-of-tens
*
CVB1: MOVE #$ 3 0,1, X+ ; Generate ASCII '0' in character position
CVB2: SUBD 0, Y ; Try removing one power-of-ten
BLO CVB3 ; If unsuccessful, quit
INC -1, X ; If successful, up the ASCII character
BRA CVB2 ; Repeat trial
*
CVB3: ADDD 2, Y+ ; Restore D, point Y to next constant
•%
CPY 0, SP ; At end of constants?
BNE CVB 1 ; If not, repeat for next power-of-ten
*
PULY ; Deallocate local variables
RTS ; Return
Figure 7.6. Conversion from Binary to Decimal by Successive Subtraction