Page 209 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 209
186 Chapter 7 Arithmetic Operations
probably familiar with this technique because it is easy to go from a decimal
representation to any base with it using a calculator, because the calculator does decimal
arithmetic. The other scheme using division simply divides the number N, assumed to be
m
1 1
less than b , by b" " , so that the quotient is the most significant digit c m _i. Dividing
m 2
the remainder by b ~ produces the next most significant digit, and so on. We will also
consider these schemes below. Which of these four schemes is best for microcomputers?
We now look more closely at each for conversion between decimal and binary bases,
Consider first the multiplication scheme that evaluates formula (3) directly. Suppose
that N4 , . . ,, NO represent five decimal digits stored in a buffer pointed to by X.
Assume that these five decimal digits have been put in from the terminal using INCH so
that each digit is in ASCII. Then
3
4
N 4 * 10 + N 3 * 10 + , . . + NO * 10° (5)
is the integer that we want to convert to binary. To carry out (5) we can store constants
K dc.w 10000,1000,100,10,1
and then multiply N4 times (K):(K + 1), NS times (K + 2):(K + 3), and so on, adding up
the results and putting the sum in D. The subroutine shown in Figure 7.4 does just that,
indicating an overflow by returning the carry bit equal to 1. The multiplication scheme
4
in Figure 7.4 takes advantage of the fact that the assembler can convert 10 through 10 9
into equivalent 16-bit binary numbers using the dc.w directive.
Looking at the second multiplication conversion scheme applied to our present
example, we rewrite the decimal expansion formula (3) as (6).
* CVDIB converts the five ASCII decimal digits, stored at the location
* contained in X, into an unsigned 16-bit number stored in D.
*
CVDIB: CLRA ; Generate 16-bit zero
CLRB ; Which becomes the result
LEAY 5 , X ; Get address of end of string
PSHY ; Save it on stack
*
C2; LDY #10 ; Multiply previous by 10
EMUL ; Multiply D * Y
PSHD ; Low 16 bits of product to stack
LDAB 1, X+ ; Next ASCII digit into B
SUBB #$30 ; ASCII to binary
CLRA ; Extend to 16 bits
ADDD 2 , SP+ ; Add previous result
CPX 0, SP ; At end of ASCII string?
BNE C2 ; No, repeat
PULY ; Balance the stack
RTS ; Return to caller
Figure 7.5. Conversion from Decimal to Binary by Multiplication by 10