Page 207 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 207
184 Chapter 7 Arithmetic Operations
While each decimal digit can be converted to binary by subtracting $30 from its
ASCII representation, the number 3275 still has to be converted to binary (e.g., $OCCB)
or some other representation if any numerical computation is to be done with it. One
also has to convert a binary number into an equivalent ASCII decimal sequence if this
number is to be displayed on the terminal screen. For example, suppose that the result of
some arithmetic computation is placed in accumulator D, say, $OCCB. The equivalent
decimal number, in this case 3275, must be found and each digit converted to ASCII
before the result can be displayed on the terminal screen. We focus on the ways of doing
these conversions in this section.
One possibility is to do all of the arithmetic computations with binary-coded
decimal (BCD) numbers, where two decimal digits are stored per byte. For example, the
BCD representation of 3275 in memory would be
Going between the ASCII decimal representation of a number to or from equivalent BCD
representation is quite simple, involving only shifts and the AND operation. With the
6812, it is a simple matter to add BCD numbers; use ADDA or ADC A with the DAA
instruction. Subtraction of BCD numbers on the 6812 must be handled differently from
the decimal adjust approach because the subtract instructions do not correctly set the half-
carry bit H in the CC register. (See the problems at the end of the chapter.) For some
applications, addition and subtraction may be all that is needed, so that one may prefer to
use just BCD addition and subtraction. There are many other situations, however, that
require more complex calculations, particularly applications involving control or
scientific algorithms. For these, the ASCII decimal numbers are converted to binary
because binary multiplication and division are much more efficient than multiplication
and division with BCD numbers. Thus we convert the input ASCII decimal numbers to
binary when we are preparing to multiply and divide efficiently. However, depending on
the MPU and the application, BCD arithmetic may be adequate so that the conversion
routines below are not needed.
We consider unsigned integer conversion first, discussing the general idea and then
giving conversion examples between decimal and binary representations. A brief
discussion of conversion of signed integers concludes this section. The conversion of
numbers with a fractional part is taken up in a later section.
m
An unsigned integer N less than b has a unique representation