Packed BCD numbers are stored two digits to a byte, in 4-bit groups referred to as nibbles. The ALU is capable of performing only binary addition and subtraction, but by adjusting the sum or difference the correct result in packed BCD format can be obtained. The correction rule for addition is:
If the addition of any two digits results in a binary number between 1010 and 1111, which are not valid BCD digits, or there is a carry into the next digit, then 6 (0110) is to be added to the current digit.
For example:
Essentially, the rule is needed to "skip over" the six bit combinations that are unused by the BCD format whenever such a skip is warranted.
Figure 3-22 Packed BCD adjust instructions.
Name | Mnemonic and Format | Description | ||
---|---|---|---|---|
Decimal adjust for addition | DAA | (AL)Sum in AL adjusted to packed BCD format | ||
Decimal adjust for subtraction | DAS | (AL)Difference in AL adjusted to packed BCD format | ||
Flags: OF flag is undefined and all other condition flags are affected. | ||||
Addressing modes: Operates only on the AL register. |
Figure 3-23 Packed BCD addition.
Instruction | Action | Contents of | ||||
---|---|---|---|---|---|---|
AL | CF | AF | ||||
MOV AL,BCD1 | (AL)34 | 34 | -- | -- | ||
ADD AL,BCD2 | (AL)34+89 | BD | 0 | 0 | ||
DAA | ADJUST | 23 | 1 | * | ||
MOV BCD3,AL | (BCD3)23 | 23 | 1 | * | ||
MOV AL,BCD1+1 | (AL)18 | 18 | 1 | * | ||
ADC AL,BCD2+1 | (AL)18+27+(CF) | 40 | 0 | 1 | ||
DAA | ADJUST | 46 | 0 | * | ||
MOV BCD3+1,AL | (BCD3+1)46 | 46 | 0 | * | ||
*Not important |
Figure 3-24 Packed BCD subtraction.
Instruction | Action | Contents of | ||||
---|---|---|---|---|---|---|
AL | CF | AF | ||||
MOV AL,BCD1 | (AL)34 | 34 | -- | -- | ||
SUB AL,BCD2 | (AL)34-12 | 22 | 0 | 0 | ||
DAS | ADJUST | 22 | 0 | * | ||
MOV BCD3,AL | (BCD3)22 | 22 | 0 | * | ||
MOV AL,BCD1+1 | (AL)12 | 12 | 0 | * | ||
SBB AL,BCD2+1 | (AL)12-46-(CF) | CC | 1 | 1 | ||
DAS | ADJUST | 66 | 1 | * | ||
MOV BCD3+1,AL | (BCD3+1)(AL) | 66 | 1 | * | ||
*Not important | ||||||
Result = 5622 = -3378 in 10's complement |
This is page 31.