ASCII Arithmetic
The ASCII arithmetic instruction function with ASCII-coded numbers. These numbers range in value from 30H to 39H for the numbers 0 to 9. There are four instructions used with ASCII arithmetic operations:
- AAA (ASCII Adjust after Addition)
- AAD (ASCII Adjust before Division)
- AAM (ASCII Adjust after Multiplication)
- AAS (ASCII Adjust after Subtraction)
These instructions use register AX as the source and as destination.
1. AAA Instruction
- Adjust the sum of two unpacked BCD values to create an unpacked BCD result.
- The AL register is the implied source and destination operand for this instruction.
- The AAA instruction is only useful when its follow an ADD instruction that adds (binary addition) two unpacked BCD values.
- The AAA instruction then adjusts the contents of AL register to contain the correct 1-digit unpacked BCD result.
- If the addition produces a decimal carry, the AH register is incremented by 1, and the CF and AF flags are set.
2. AAD Instruction
- Unlike all other adjustment instructions, the AAD instruction appears before a division.
- The AAD instruction requires that the AX register contain a two-digit unpacked BCD number (not ASCII) before executing.
- After adjusting the AX register with AAD, it is divided by an unpacked BCD number to generate a single-digit result in AL with any remainder in AH.
- Example:
MOV AX, 702H
AAD
DIV BL
- The above example show how 72 is unpacked BCD is divided by 9 to produce a quotient of 8. The 0702H loaded into AX register is adjusted by the AAD instruction to 0048H.
3. AAM Instruction
- Adjust the result of the multiplication of two unpacked BCD values to create a pair of unpacked BCD values.
- The AX register is the implied source and destination operand for this instruction.
- The AAM instruction is only useful when it follows an MUL instruction that multiplies (binary multiplication) two unpacked BCD values and stores a word result in AX registers.
4. AAS Instruction
- Adjust the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.
- The AL register is the implied source and destination for this instructions.
- The AAS instruction is only useful when it follows a SUB instruction that subtracts (binary subtraction) one unpacked BCD valued from another and stores a byte result in the AL register.
- If the subtraction produced a decimal carry, the AH register is decremented by 1, and the CF and AF flags are set.
- If no decimal carry occurred, the CF and AF flags are cleared, and the AH register is unchanged.