AAS: ASCII Adjust AL After Subtraction

For information about interpreting this page, see the help page.
Opcode and MnemonicEncoding16 bit Mode32 bit Mode64 bit ModeDescription
3F
AAS
ZOValidValidInvalidASCII adjust AL after subtraction.

Encoding

EncodingOperand
ZONone

Description

The AAS instruction converts the result of a subtraction of two BCD digits to a valid 2-digit BCD number.

This instruction will not be useful unless it immediately follows an ADD (Add) instruction using the AL register and a single digit operand (either register or immediate).

Traditionally, this instruction is ASCII Adjust After Subtraction. This would lead one to believe that it works on ASCII digits (030h (0) through 039h (9)), however this would be incorrect. This instruction actually operates on BCD digits (00h (0) through 09h (9)).

This instruction is not valid in 64 bit mode, and if encountered, the processor will raise a #UD exception. In 16 or 32 bit mode, this instruction will only modify the AF and CF flags. The other arithmetic flags are undefined and undocumented.

Operation

This pseudo-code uses C# syntax. A list of the types used is available here.
public void AAS() {
  if (((AL & 0xF) > 9) || AF)
  {
    AX = AX - 6;
    AH = AH - 1;
  }
  AL = AL & 0xF;
}

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

Protected Mode

#UD
If the LOCK prefix is used.

Real-Address Mode

Same exceptions as protected mode.

Virtual-8086 Mode

Same exceptions as protected mode.

Compatibility Mode

Same exceptions as protected mode.

Long Mode

#UD
If in 64 bit mode.