Page 224 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 224
7.5 Floating-Point Arithmetic and Conversion 201
* ADD adds the word pointed to by X into L (combined PUSH and ADD)
ADD: ADDD 2, X ; add low 16 bits
XGDY ; put high 16 bits in D
ADCB I, X ; add mid 8 bits
ADCA 0, X ; add high 16 bits
XGDY ; put high 16 bits in Y
RTS ; return to caller
Figure 7.19. Push-and-Add Subroutine
7.5 Floating-Point Arithmetic and Conversion
We have been concerned exclusively with integers, and, as we have noted, all of the
subroutines for arithmetic operations and conversion from one base to another could be
extended to include signs if we wish. We have not yet considered arithmetic operations
for numbers with a fractional part. For example, the 32-bit string bsi,. . .,bo could be
used to represent the number x, where
23
x = b 31 * 2 + ... + b 8 * 2° + ... + b 0 * 2~ 8 (13)
The notation b3i . . . bg • bj . . . bo is used to represent x, where the symbol "%" called
the binary point, indicates where the negative powers of 2 start. Addition and subtraction
of two of these 32-bit numbers, with an arbitrary placement of the binary point for each,
is straightforward except that the binary points must be aligned before addition or
subtraction takes place and the specification of the exact result may require as many as 64
bits. If these numbers are being added and subtracted in a program (or multiplied and
divided), the programmer must keep track of the binary point and the number of bits
being used to keep the result. This process, called scaling, was used on analog computers
and early digital computers. In most applications, scaling is so inconvenient to use that
most programmers use other representations to get around it.
One technique, called a fixed-point representation, fixes the number of bits and the
position of the binary point for all numbers represented. Thinking only about unsigned
numbers for the moment, notice that the largest and smallest nonzero numbers that we
can represent are fixed once the number of bits and the position of the binary point are
fixed. For example, if we use 32 bits for the fixed-point representation and eight bits for
24
the fractional part as in (13), the largest number that is represented by (13) is about 2
8
and the smallest nonzero number is 2~ . As one can see, if we want to do computations
with either very large or very small numbers (or both), a large number of bits will be
required with a fixed-point representation. What we want then is a representation that
uses 32 bits but gives us a much wider range of represented numbers and, at the same
time, keeps track of the position of the binary point for us just as the fixed-point
representation does. This idea leads to the floating-point representation of numbers,
which we discuss in this section. After discussing the floating-point representation of
numbers, we examine the arithmetic of floating-point representation and the conversion
between floating-point representations with different bases.