The AAD instruction
Adjusting the AX register before division.
The AAD instruction format
| Instruction | Description |
|---|---|
| AAD | Adjusting the AX register before division |
| AAD imm8 | Adjusting the AX register before division by the numeric radix imm8 |
Adjusts two unpacked BCD digits (the least significant digit in the al register and the most significant digit in the ah register) so that the division operation performed on the result produces the correct unpacked BCD value. The aad instruction is useful only when it precedes a div instruction that divides (binary division) the adjusted value in the ax register by an unpacked BCD value.
The aad instruction sets the value in the al register to (al + (10 * ah)), and then clears the ah register to 00h. The value in the ax register is then equal to the binary equivalent of the original unpacked two-digit number (base 10) in the ah and al registers.
The generalized version of this instruction allows two unpacked digits of any number base to be adjusted by setting the imm8 byte to the selected number base (for example, 08h for octal, 0Ah for decimal, or 0Ch for base 12 numbers). The aad mnemonic is interpreted by all assemblers as meaning to adjust ASCII values (base 10). To adjust values in another number base, the instruction must be hand-coded in machine code (D5 imm8).
First example: 17 / 2
title .model small .stack 100h code segment public ; Start of the code segment assume cs:code main proc ; Start of the main procedure mov ax, 0107h ; Copy the unpacked value 0107 into the ax register aad ; Adjust the contents of the al register mov bl, 2 ; Copy the value 2 into the bl register div bl ; Divide the value of the al register by the value in the bl register mov ax, 4C00h ; Copy the process termination function number into ah and the value into al int 21h ; Call the interrupt main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".
Second example: 53 / 3 / 2 with the quotient printed on the screen
title .model small .stack 100h code segment public ; Start of the code segment assume cs:code main proc ; Start of the main procedure mov ax, 0503h ; Copy the unpacked value 53 into the ax register mov bx, 3 ; Copy the value 3 into the bx register aad ; Adjust the contents of the ax register xor dx, dx ; Clear the value of the dx register div bx ; Divide the ax register by the bx register mov bx, 2 ; Copy the value 2 into the bx register aad ; Adjust the contents of the ax register xor dx, dx ; Clear the value of the dx register div bx ; Divide the ax register by the bx register or al, 30h ; Convert the value in the al register into an ASCII number mov ah, 02h ; Copy the console output function number into ah mov dl, al ; Copy the ASCII number from the al register into the dl register int 21h ; Call the interrupt mov ax, 4C00h ; Copy the process termination function number into ah and the value into al int 21h ; Call the interrupt main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".