Page 214 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 214
7.3 From Formulas to Subroutine Calls 191
1. Multiply the five decimal digits pointed to by X by 2.
2, Add 1 to the five decimal digits pointed to by X if b n = 1.
Recall, however, that the preceding operations must be done in decimal. For this, it is
more convenient to output our digits in BCD rather than one decimal digit per byte
because we can take advantage of the DAA instruction. In particular, notice that in the
subroutine of Figure 7.9, the carry bit in loop CBD2 equals b n the first time through,
while the next two times through, the loop completes the doubling in decimal of the
BCD contents of the three bytes pointed to by X. The conversion of the BCD number to
ASCII, if desired, is straightforward. (See the problems at the end of the chapter.)
Conversion of signed numbers is straightforward once the foregoing techniques are
understood. This conversion can be done strictly by the formula that defines the signed
number, as done above for unsigned numbers, or the signed number can be expressed as a
sign and a magnitude, and the magnitude can be converted as before because it is an
unsigned number. The idea of conversion is quite general. You can convert to base 12 or
from base 12 using any of these ideas. You can convert between the time in a week
expressed in days of the week, hours, minutes, and seconds and the time of the week in
seconds represented in binary. This type of conversion is similar to going between a
binary sector number and a disk track sector number, something that becomes important
if you are involved in writing disk controller programs. Conversion can be met in
unexpected places, and with the techniques of this section, you should be able to handle
any integer conversion problem.
An important aspect of this discussion is the manner in which a software engineer
approaches any problem in general and a numeric problem in particular. The general rale
is to learn about all the algorithms that can be used to solve the problem. In
algebraically specified problems such as conversion between number systems, the
algorithms are described by different formulas. The software engineer researches all the
formulas that have been applied to the problem. For example, we found that formulas (3)
and (4) apply to number system conversions. Then the different programming styles,
such as storing coefficients in a table, generating the required constants by a subroutine,
and using loops or recursion can be attempted to derive different programs. The shortest,
fastest, or clearest program is then selected for the application.
7.3 From Formulas to Subroutine Calls
In following sections, we develop techniques to write subroutines to carry out the usual
arithmetic operations on long integer and floating-point numbers, namely, +, -, *, and /.
We could, with further study, write a whole collection of subroutines that would carry
2 z
out the functions we would meet in engineering, such as sin(x), cos(x), log(x), (x) , y ,
and so on. In a given application, we might have to write a program segment to evaluate
a complicated formula involving these operations, for example,
This section examines a general technique to write such a program segment.
For simplicity, we will assume that all operands are long integers or single-
precision floating point numbers (four bytes) and that the subroutines are written so that