Hardware Reference
In-Depth Information
4.7 Mathematical Subroutines
Mathematical computation is one of the most important application areas for the micro-
controller. Several examples will be used to illustrate parameter passing, local variable alloca-
tion, and result returning.
4.7.1 Subroutine for Performing Multiple-Byte Division
The HCS12 provides instructions for 16-bit by 16-bit signed and unsigned divisions and
also for 32-bit by 16-bit signed and unsigned divisions. However, the HCS12 does not have
instructions for performing higher-precision divisions; say, for example, 32-bit by 32-bit division.
We will need to write subroutines to synthesize such operations.
The most popular method for performing high-precision divide operations is the repeated
subtraction method. The conceptual hardware for implementing this algorithm is shown in
Figure 4.10.
m
s
l
s
R register
Q register
C
32-bit
32-bit
Set lsb
Shift left
Write R
Controller
ALU
P register
32-bit
Figure 4.10 Conceptual hardware for implementing the repeated subtraction method
Step 1
icnt
n; R
0; Q
dividend; P
divisor.
Step 2
Shift the register pair (R, Q) 1 bit left.
Step 3
Subtract the register P from the register R; put the result back to R if the result is
nonnegative.
Step 4
If the result of step 2 is negative, then set the least significant bit of Q to 0. Otherwise, set
the least significant bit of Q to 1.
Step 5
icnt
icnt 2 1.
Step 6
If (icnt 55 0), then stop;
else go to step 2.
 
Search WWH ::




Custom Search