The SUB instruction
Subtraction.
The SUB instruction format
| Instruction | Description |
|---|---|
| SUB AL, imm8 | Subtract imm8 from AL. |
| SUB AX, imm16 | Subtract imm16 from AX. |
| SUB reg/mem8, imm8 | Subtract imm8 from reg/mem8. |
| SUB reg/mem16, imm16 | Subtract imm16 from reg/mem16. |
| SUB reg/mem16, imm8 | Subtract sign-extended imm8 from reg/mem16. |
| SUB reg/mem8, reg8 | Subtract reg8 from reg/mem8. |
| SUB reg/mem16, reg16 | Subtract reg16 from reg/mem16. |
| SUB reg8, reg/mem8 | Subtract reg/mem8 from reg8. |
| SUB reg16, reg/mem16 | Subtract reg/mem16 from reg16. |
Subtracts the second operand (source operand) from the first operand (destination operand) and stores the result in the destination operand. The destination operand can be a register or a memory location; the source operand can be an immediate value, a register or a memory location. (Two memory operands, however, cannot be used in the same instruction.) When an immediate value is used as an operand, it is sign-extended to the length of the destination operand format.
Instruction sub performs integer subtraction. It computes the result for both signed and unsigned integer operands and sets the OF and CF flags to indicate an overflow in the signed or unsigned result, respectively. The SF flag indicates the sign of the signed result.
Usage example
title .model small .stack 100h code segment public ; Start of the code segment assume cs:code main proc ; Start of the main procedure mov al, 18 ; Copy the value 18 into the al register mov bl, 4 ; Copy the value 4 into the bl register sub al, bl ; Subtract the value in the bl register from the al 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)".