AAM: ASCII Adjust AX After Multiplication

For information about interpreting this page, see the help page.
Opcode and MnemonicEncoding16 bit Mode32 bit Mode64 bit ModeDescription
D4 0A
AAM
ZOValidValidInvalidASCII adjust AX after multiply.
D4 ib
AAM imm8
IValidValidInvalidASCII adjust AX after multiply to number base imm8.

Encoding

EncodingOperand
ZONone (implicitly 10)
Iimm8[r]

Description

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

This instruction will not be useful unless it immediately follows an MUL instruction.

Traditionally, this instruction is called "ASCII Adjust After Multiplication". 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)).

Due to a quirk of history, this instruction encodes the base of the result in the instruction (the second byte). As such, it is possible to have this instruction work on other bases through an immediate 8 bit operand. Such methods are not guaranteed to be supported by your assembler, and in such situations, one must encode the instruction directly.

This instruction is invalid in 64 bit mode, and, if encountered, the processor will raise a #UD exception.

Operation

This pseudo-code uses C# syntax. A list of the types used is available here.
public void AAM(byte imm8)
{
  AH = AL / imm8;
  AL = AL & imm8;
}

Flags Affected

CF (carry flag)
Undefined.
PF (parity flag)
Set based on the resulting value in AL.
AF (auxiliary flag)
Undefined.
ZF (zero flag)
Set based on the resulting value in AL.
SF (sign flag)
Set based on the resulting value in AL.
OF (overflow flag)
Undefined.

Exceptions

Protected Mode

#DE
If an immediate value of 0 is used.
#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.