AAM: ASCII Adjust AX After Multiplication

info-signFor information about interpreting this page, see the help page.
Opcode and MnemonicEncoding16 and 32 bit Mode64 bit ModeDescription
D4 0A
AAM
ZOValidInvalidASCII adjust AX after multiply.
D4 ib
AAM imm8
IValidInvalidASCII adjust AX after multiply to number base imm8.

Encoding

EncodingOperand
ZONone (implicitly 10)
Iimm8[r]

Bit Encoding

implicit 10
1101 0100 : 0000 1010
immediate
1101 0100 : immediate

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 not valid in 64 bit mode, and if encountered, the processor will raise a #UD exception.

Operation

info-signThis pseudo-code uses a Rust-like syntax. A list of the types used is available here.
pub fn aam(imm8: u8) {
  if MODE == 64 {
    #UD;
  } else {
    AH = AL / imm8;
    AL = AL % imm8;
  }
}

Flags Affected

The SF, ZF, and PF flags are set according to the resulting binary value in the AL register. The OF, AF, and CF flags are 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.