3-3-2 Packed BCD Arithmetic

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.

NameMnemonic and FormatDescription
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.

InstructionActionContents of
ALCFAF
MOV    AL,BCD1
(AL)3434----
ADD    AL,BCD2
(AL)34+89BD00
DAA
ADJUST231*
MOV    BCD3,AL
(BCD3)23231*
MOV    AL,BCD1+1
(AL)18181*
ADC    AL,BCD2+1
(AL)18+27+(CF)4001
DAA
ADJUST460*
MOV    BCD3+1,AL
(BCD3+1)46460*
*Not important

Figure 3-24 Packed BCD subtraction.

InstructionActionContents of
ALCFAF
MOV    AL,BCD1
(AL)3434----
SUB    AL,BCD2
(AL)34-122200
DAS
ADJUST220*
MOV    BCD3,AL
(BCD3)22220*
MOV    AL,BCD1+1
(AL)12120*
SBB    AL,BCD2+1
(AL)12-46-(CF)CC11
DAS
ADJUST661*
MOV    BCD3+1,AL
(BCD3+1)(AL)661*
*Not important
Result = 5622 = -3378 in 10's complement

Table of Contents | Next page | Previous page

This is page 31.