SUB
By Vesper Vei
1 minute read
Table of Contents
SUB (sub)
Basic function
The SUB instruction performs subtraction, writing the result of x1 - x2 back to x1.
Instruction format
sub x1, x2x1 = x1 - x2x1 and x2 can be:
- Register
- Memory
- Immediate value
Restrictions:
- They cannot both be memory operands at the same time
Instruction execution process
x1 ← x1 - x2EFLAGS ← 根据结果更新Affected flags:
- OF (signed overflow)
- SF (sign)
- ZF (whether the result is 0)
- CF (used to determine whether a borrow occurred)
- AF, PF
Example
sub eax, ebxsub rax, 0x100sub [rbp-0x4], 1Equivalent expansion
sub rax, rbx; 等价于tmp = rax - rbxrax = tmp更新 EFLAGSCommon uses
- Decrementing, loop counting
- Moving the stack pointer upward (such as the reverse of a
suboperation) - Numeric computation
- Address offsetting (such as moving backward within a structure)