Skip to content

Instructions

x86-64

ADD / SUB

ADD computes the sum and SUB the difference of two operands, storing the result and updating all arithmetic flags.

ADD and SUB are the fundamental arithmetic instructions. Both modify ZF, CF, SF, OF, PF, and AF.

Syntax

asm
add rax, rbx       ; rax += rbx
add rax, 8         ; rax += 8  (immediate)
add [rsp+0x10], 1  ; increment memory operand directly

sub rsp, 0x30      ; allocate 48 bytes on the stack (prologue pattern)
sub rax, rcx       ; rax -= rcx

Flags set (both instructions)

FlagSet when
ZFResult is zero
CFUnsigned overflow (carry out / borrow)
SFResult is negative (MSB=1)
OFSigned overflow
PFParity of low byte

Common compiler patterns

PatternInterpretation
sub rsp, NStack frame allocation in function prologue
add rsp, NStack frame teardown or callee-cleans-up
add rax, 1Increment (also seen as inc rax)
sub rax, 1Decrement (also seen as dec rax)
add rdi, rdxPointer arithmetic: advance by rdx bytes

Reverse-engineering notes

  • sub rsp, 0x?8 in a prologue allocates a local variable area (and often ensures 16-byte alignment before the next call).
  • Compilers sometimes emit add rsp, N mid-function to clean up temporary stack allocations made for variadic calls.
  • add / sub with immediate −1 on an rdx loop counter will set ZF when the counter reaches zero, enabling a jnz loop_top without a separate cmp.
  • ADC (add-with-carry) and SBB (subtract-with-borrow) extend addition/subtraction across multiple words — common in big-integer and cryptographic code.

Try it — Virtual CPU

open full playground →
  1. 1 mov eax, 10
  2. 2 add eax, 5
  3. 3 sub eax, 20
  4. 4 add eax, 5
  5. 5 cmp eax, 0
  6. 6
step 0
Loading emulator…