Page 202 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 202
7
Arithmetic Operations
This chapter deals with how one number crunches, using algebraic formulas, and how
one writes subroutines for conversion between different bases, multiple-precision integer
arithmetic, floating-point arithmetic, and fuzzy logic.
The first section describes unsigned and signed multiplication and division.
Although the subroutines developed herein are in fact equivalent to 6812 instructions,
they clearly illustrate the algorithms used to execute these instructions in a typical
controller, and easily developed variants of these subroutines are usable on other
microcontrollers that do not have these instructions.
In the next section, we develop ways to convert integers between number systems,
such as binary and decimal. These techniques are needed when you input numbers from a
terminal keyboard and output numbers to a terminal display. A full comparison of these
techniques is made, examining all known possibilities and selecting the best, something
that you should try to do in writing any of your own subroutines for any purpose.
The third section presents a technique to write program segments that evaluate
algebraic formulas. The operations in the formulas are implemented with macros.
Formula evaluation becomes a sequence of macro calls. You can hand assemble these
macros. The actual variables used in the formula could be 32-bit integers, floating-point
numbers, or any other type of variable that the subroutines have been written to handle.
Some C compilers do not support long and float data types. Section 7.4 shows
how to perform 32-bit signed and unsigned integer arithmetic, which is useful to one
who uses a microprocessor in a numerical control application. The fifth section deals
with floating-point arithmetic. These sections will provide subroutines that enable you
to perform arithmetic using these number systems.
Section 7.6 deals with fuzzy logic, for which the 6812 has special instructions. This
section will give you some background so that you can describe a fuzzy logic system,
and you can write assembly language subroutines to execute fuzzy logic.
After reading this chapter, you should be able to convert integers from one base to
another. You should be able to write a sequence of subroutine calls to evaluate any
algebraic formula and write subroutines to work out any multiprecision arithmetic
operation, especially 32-bit long arithmetic. You should understand the principles of
floating point arithmetic and fuzzy logic to the point that you could write subroutines for
the usual floating-point and fuzzy logic operations.
179