Hardware Reference
In-Depth Information
This instruction sequence performs the following addition:
$2 5
$ 3 1
$5 6
+
+
When the HCS12 executes this instruction sequence, it adds the numbers according to the rules
of binary addition and produces the sum $56. This is the correct BCD answer, because the
result represents the decimal sum of 25 1 31 5 56. In this example, the HCS12 gives the ap-
pearance of performing decimal addition. However, a problem occurs when the HCS12 adds
two BCD digits and generates a sum greater than 9. Then the sum is incorrect in the decimal
number system, as the following three examples illustrate:
$1 8
$ 4 7
$5 F
$3 5
$ 4 7
$7 C
$1 9
$ 4 7
$6 0
+
+
+
The answers to the fi rst two problems are obviously wrong in the decimal number system because
the hex digits F and C are not between 0 and 9. The answer to the third example appears to contain
valid BCD digits, but in the decimal system 19 plus 47 equals 66, not 60; this example involves a
carry from the lower nibble to the higher nibble.
In summary, a sum in the BCD format is incorrect if it is greater than $9 or if there is a
carry to the next-higher nibble. Incorrect BCD sums can be adjusted by adding $6 to them. To
correct the examples,
1. Add $6 to every sum digit greater than 9.
2. Add $6 to every sum digit that had a carry of 1 to the next higher digit.
Here are the problems with their sums adjusted.
$1 8
$ 4 7
$3 5
$ 4 7
$7 C
$1 9
$ 4 7
$6 0
+
+
+
$5 F
+
$ 6
+
$ 6
+
$ 6
$6 5
$ 8 2
$ 6 6
The fi fth bit of the condition code register is the half-carry, or H fl ag. A carry from the lower
nibble to the higher nibble during the addition operation is a half-carry. A half-carry of 1 during
addition sets the H fl ag to 1, and a half-carry of 0 during addition clears it to 0. If there is a carry
from the high nibble during addition, the C fl ag is set to 1; this indicates that the high nibble is
incorrect. $6 must be added to the high nibble to adjust it to the correct BCD sum.
Fortunately, we don't need to write instructions to detect the illegal BCD sum following
a BCD addition. The HCS12 provides a decimal adjust accumulator A instruction, daa, which
takes care of all these detailed detection and correction operations. The daa instruction moni-
tors the sums of BCD additions and the C and H flags and automatically adds $6 to any nibble
that requires it. The rules for using the daa instruction are
1. The daa instruction can only be used for BCD addition. It does not work for subtraction
or hex arithmetic.
2. The daa instruction must be used immediately after one of the three instructions that
leave their sum in accumulator A. (These three instructions are adda, adca, and aba.)
3. The numbers added must be legal BCD numbers to begin with.
 
 
Search WWH ::




Custom Search