Arithmetic Operations
Arithmetic is where assembly stops being scenery. Loop counters, pointer offsets, array indexing, the inner loops of crypto code, all of it boils down to these instructions. x86-64 ships a full kit of integer ops, each one fussy about which registers it reads and writes.
For background on how binary numbers relate to arithmetic, see the Binary Number Systems theory module.
ADD and SUB
The simplest arithmetic instructions in the set:
; ADD destination, source (destination = destination + source)add rax, rbx ; rax = rax + rbxadd rax, 10 ; rax = rax + 10add rax, [rbx] ; rax = rax + value at address rbxadd [rbx], rax ; value at rbx = value at rbx + rax
; SUB destination, source (destination = destination - source)sub rax, rbx ; rax = rax - rbxsub rax, 1 ; rax = rax - 1sub [rsp + 8], rdx ; value at rsp+8 = value at rsp+8 - rdxADD and SUB both touch the flags register. The ones to watch:
- ZF (Zero Flag) - set if the result is zero
- SF (Sign Flag) - set if the result is negative (high bit is 1)
- CF (Carry Flag) - set if an unsigned overflow occurred
- OF (Overflow Flag) - set if a signed overflow occurred
INC and DEC
Increment and decrement by one:
inc rax ; rax = rax + 1dec rcx ; rcx = rcx - 1inc dword [rbx] ; increment the 32-bit value at address rbxINC and DEC update every flag except CF. A historical wart, and one that occasionally bites multi-precision arithmetic.
MUL and IMUL - Multiplication
Multiplication in x86 carries more weight than addition: the product of two N-bit numbers can stretch to 2N bits, and the architecture has to put those extra bits somewhere.
MUL (unsigned multiplication) uses implicit operands:
; MUL source; Multiplies RAX by source, result goes to RDX:RAX
mov rax, 100 ; multiplicandmov rbx, 50 ; multipliermul rbx ; RDX:RAX = RAX * RBX = 5000 ; RDX = upper 64 bits (0 in this case) ; RAX = lower 64 bits (5000)The result spans two registers: RDX carries the upper half, RAX carries the lower. For narrower operands:
| Operand size | Implicit multiplicand | Result stored in |
|---|---|---|
mul r/m8 | AL | AX |
mul r/m16 | AX | DX:AX |
mul r/m32 | EAX | EDX:EAX |
mul r/m64 | RAX | RDX:RAX |
IMUL (signed multiplication) has three forms:
; One-operand form (same as MUL but signed)imul rbx ; RDX:RAX = RAX * RBX (signed)
; Two-operand form (result truncated to register size)imul rax, rbx ; rax = rax * rbx (lower 64 bits only)imul rcx, [rdi] ; rcx = rcx * value at rdi
; Three-operand form (immediate multiplier)imul rax, rbx, 10 ; rax = rbx * 10imul rcx, [rdi], 3 ; rcx = value at rdi * 3The two- and three-operand forms earn their keep by letting you pick the destination and leaving RDX untouched.
Integer overflow is one of the most dangerous vulnerability classes in systems programming. When a multiplication result exceeds the register size, the upper bits are silently discarded (in the two-operand IMUL form). An attacker can exploit this: if a program computes buffer_size = user_count * element_size and the multiplication overflows, the program allocates a tiny buffer but tries to fill it with a huge amount of data, causing a heap overflow. Always check for overflow after arithmetic on untrusted input.
The check costs one instruction, because the multiply has already told you. After one-operand MUL, CF and OF are both set exactly when the upper half in RDX is non-zero. After IMUL (any form), they're set when the true signed product didn't fit in the destination. So mul plus jc (or imul plus jo) is a complete overflow check.
That starting user_count is no accident. The full product is 0x1_0000_0000_0000_0008, so the low 64 bits in RAX are just 8. Without the jc, the program would allocate an 8-byte buffer and then write more than two quintillion elements' worth of data into it.
DIV and IDIV - Division
Division is the awkward one. Like MUL, it leans on implicit operands:
; DIV source (unsigned division); Divides RDX:RAX by source; Quotient goes to RAX, remainder to RDX
mov rdx, 0 ; IMPORTANT: clear upper half of dividendmov rax, 100 ; dividend (lower half)mov rbx, 7 ; divisordiv rbx ; RAX = 100/7 = 14, RDX = 100%7 = 2| Operand size | Dividend | Quotient | Remainder |
|---|---|---|---|
div r/m8 | AX | AL | AH |
div r/m16 | DX:AX | AX | DX |
div r/m32 | EDX:EAX | EAX | EDX |
div r/m64 | RDX:RAX | RAX | RDX |
IDIV (signed division) works the same way but reads operands as signed. Before issuing it, use CQO (convert quad to oct) to sign-extend RAX across RDX:RAX:
; Signed divisionmov rax, -100 ; dividendcqo ; sign-extend RAX into RDX:RAXmov rbx, 7 ; divisoridiv rbx ; RAX = -14, RDX = -2Dividing by zero triggers a hardware exception (signal SIGFPE on Linux) that will kill your process. Also, if the quotient is too large to fit in the destination register, you get the same exception. This happens, for example, if RDX is not zeroed before unsigned DIV - the dividend RDX:RAX could be enormous, making the quotient overflow. Always zero RDX (or use CQO for signed) before dividing.
Here are all four instructions on the RDX:RAX pair. Start with mul overflows and watch the 65th bit land in RDX. Then load Forgot to zero rdx and idiv without cqo: one faults with #DE, the other quietly hands back a wrong answer, which is arguably the nastier bug. The xor edx, edx and cqo buttons fix each one.
mul, imul, div and idiv on rdx:rax
Presets
mul rbx ; rdx:rax = rax * rbx (unsigned)
Result in rdx:rax
128-bit product = 5000
NEG - Two's Complement Negation
neg rax ; rax = -rax (flip all bits and add 1)neg dword [rbx] ; negate the 32-bit value at address rbxNEG flips a value into its two's complement negation. CF lands at 1 unless the operand was zero, in which case the result is zero and CF=0. ZF, SF, and OF all get refreshed too.
Practical Example: Computing an Average
section .data values dq 10, 20, 30, 40, 50 ; array of 5 qwords count equ 5
section .text global _start
_start: ; Sum all values xor rax, rax ; accumulator = 0 xor rcx, rcx ; index = 0 lea rsi, [rel values] ; pointer to array
.sum_loop: add rax, [rsi + rcx*8] ; add values[rcx] to accumulator inc rcx ; rcx++ cmp rcx, count ; compare index to count jl .sum_loop ; loop if rcx < count
; Divide sum by count to get average xor rdx, rdx ; clear upper dividend mov rbx, count ; divisor = 5 div rbx ; rax = sum/count, rdx = sum%count
; rax now holds the average (30) ; Exit with the average as the status code mov rdi, rax mov rax, 60 syscallArithmetic Idioms
Compilers and seasoned hand-written assembly lean on these constantly:
xor rax, rax ; zero a register (faster/smaller than mov rax, 0)lea rax, [rax*2] ; multiply by 2 without touching flagsshl rax, 3 ; multiply by 8 (shift left 3 bits)add rax, rax ; multiply by 2sub rax, rax ; also zeroes it; like xor, it clears CF and sets ZFWhat is Next
Arithmetic instructions leave breadcrumbs in the flags register. The next lesson digs into that register: how to read it, how to test conditions without disturbing data, and how flags wire into the rest of conditional execution.