ASCII Adjust AL After Addition

Encoding

EncodingOperand
zoNone

Description

The AAA instruction converts the result of an addition of two (unpacked) BCD digits to a valid 2-digit BCD number.

An "unpacked" BCD number is one where each byte contains a single digit. In contrast, a packed BCD number is one where each byte contains two digits – one in each nibble. The DAA (Decimal Adjust AL After Addition) instruction handles that case.

Traditionally, this instruction is 'ASCII Adjust After Addition'. This would lead one to believe that it works on ASCII digits (30h ("0") through 39h ('9')), however, this is incorrect. This instruction actually operates on binary coded decimal (BCD) digits (00h through 09h).

This instruction is not valid in Long Mode.

Operation

public void AAA()
{
    if ((AL & 0xF) > 9 || EFLAGS.AF)
        AX += 0x106;
}
  1. According to Bochs' source code (/cpu/bcd.cc), this instruction is implemented differently on the 8086, 8088, and 80186 architectures. On them, the addition to AX is performed as two separate operations: adding 1 to AH and adding 6 to AL. In practice, this makes no difference when true BCD digits are used (i.e. nothing outside 00h through 09h)

Example

mov ax, 0x106 ; 16 (decimal) in BCD
add al, 5     ; AX == 0x10B
aaa           ; AX == 0x202 (22 (decimal) in BCD)

Flags Affected

CF (carry flag)
Set if an adjustment is made. Cleared otherwise.
PF (parity flag)
Undefined.
AF (auxiliary flag)
Set if an adjustment is made. Cleared otherwise.
ZF (zero flag)
Undefined.
SF (sign flag)
Undefined.
OF (overflow flag)
Undefined.

Exceptions

Real-Address Mode

#UD
  • If the LOCK prefix is used.

Virtual-8086 Mode

#UD
  • If the LOCK prefix is used.

Protected Mode

#UD
  • If the LOCK prefix is used.

Compatibility Mode

#UD
  • If the LOCK prefix is used.

Long Mode

#UD
  • If in Long Mode.