3-3-3 Unpacked BCD Arithmetic

In unpacked BCD there is only one digit per byte and, because of this, unpacked multiplication and division can be done.

Figure 3-25 Unpacked BCD adjust instructions.

NameMnemonic and FormatDescription
Unpacked BCD adjust for addition
AAA
(AL)Sum in AL adjusted to unpacked BCD format
(AH)(AH) + carry from adjustment
Unpacked BCD adjust for subtraction
AAS
(AL)Difference in AL adjusted to unpacked BCD format
(AH)(AH) - borrow from adjustment
Unpacked BCD adjust for multiplication
AAM
(AX)Product in AL adjusted to unpacked BCD format with AH containing high-order digit
Unpacked BCD adjust for division
AAD
(AL)10*(AH)+(AL)
(AH)0

Note: The high-order nibble of the operands should be zero.

Flags: For AAA and AAS the flags AF and CF are set if there is an adjustment and the remaining flags are undefined. AAM and AAD set PF, SF, and ZF according to their rules and leave OF, AF, and CF undefined.

Addressing modes: Operand is in AL or AX register.

Figure 3-26 Example involving unpacked BCD addition and subtraction.

Data related directives, input, and other code
UP1, UP2, and UP3 are defined as byte variables
     MOV    AL,UP1      ;ADD LOW-ORDER
     ADD    AL,UP2      ;DIGITS AND
     AAA                ;ADJUST
     MOV    DL,AL       ;PUT SUM IN DL
     MOV    AL,UP1+1    ;ADD HIGH-ORDER
     ADC    AL,UP2+1    ;DIGITS WITH CARRY
     AAA                ;AND ADJUST
     XCHG   AL,DL       ;EXCHANGE AL AND DL
     SUB    AL,UP3      ;SUBTRACT LOW-ORDER DIGIT
     AAS                ;FROM SUM AND ADJUST
     XCHG   AL,DL       ;EXCHANGE AL AND DL
     SBB    AL,UP3+1    ;SUBTRACT HIGH-ORDER DIGIT
     AAS                ;FROM SUM AND ADJUST
     MOV    DH,AL       ;MOVE AL TO DH
Output and other code

Table of Contents | Next page | Previous page

This is page 32.