Skip to main content
Be Bitwise
x86 AssemblyData Movement & Arithmetic

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:

nasm
; ADD destination, source (destination = destination + source)
add rax, rbx ; rax = rax + rbx
add rax, 10 ; rax = rax + 10
add rax, [rbx] ; rax = rax + value at address rbx
add [rbx], rax ; value at rbx = value at rbx + rax
; SUB destination, source (destination = destination - source)
sub rax, rbx ; rax = rax - rbx
sub rax, 1 ; rax = rax - 1
sub [rsp + 8], rdx ; value at rsp+8 = value at rsp+8 - rdx

ADD 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:

nasm
inc rax ; rax = rax + 1
dec rcx ; rcx = rcx - 1
inc dword [rbx] ; increment the 32-bit value at address rbx

INC 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:

nasm
; MUL source
; Multiplies RAX by source, result goes to RDX:RAX
mov rax, 100 ; multiplicand
mov rbx, 50 ; multiplier
mul 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 sizeImplicit multiplicandResult stored in
mul r/m8ALAX
mul r/m16AXDX:AX
mul r/m32EAXEDX:EAX
mul r/m64RAXRDX:RAX

IMUL (signed multiplication) has three forms:

nasm
; 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 * 10
imul rcx, [rdi], 3 ; rcx = value at rdi * 3

The two- and three-operand forms earn their keep by letting you pick the destination and leaving RDX untouched.

Security Concept: Integer Overflow Vulnerabilities

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.

Try itRun it, then set user_count to 0x1000 and run again. Finally swap mul rbx for imul rax, rbx and jc for jo.
Assembly (NASM 2.14.02)

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:

nasm
; DIV source (unsigned division)
; Divides RDX:RAX by source
; Quotient goes to RAX, remainder to RDX
mov rdx, 0 ; IMPORTANT: clear upper half of dividend
mov rax, 100 ; dividend (lower half)
mov rbx, 7 ; divisor
div rbx ; RAX = 100/7 = 14, RDX = 100%7 = 2
Operand sizeDividendQuotientRemainder
div r/m8AXALAH
div r/m16DX:AXAXDX
div r/m32EDX:EAXEAXEDX
div r/m64RDX:RAXRAXRDX

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:

nasm
; Signed division
mov rax, -100 ; dividend
cqo ; sign-extend RAX into RDX:RAX
mov rbx, 7 ; divisor
idiv rbx ; RAX = -14, RDX = -2
Warning: Division Exceptions

Dividing 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

rdx (high half)
0x0000_0000_0000_0000
rax (low half)
0x0000_0000_0000_1388

128-bit product = 5000

CF=0OF=0rdx is zero: the product fits in rax. SF, ZF, AF and PF are undefined.

NEG - Two's Complement Negation

nasm
neg rax ; rax = -rax (flip all bits and add 1)
neg dword [rbx] ; negate the 32-bit value at address rbx

NEG 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

nasm
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
syscall

Arithmetic Idioms

Compilers and seasoned hand-written assembly lean on these constantly:

nasm
xor rax, rax ; zero a register (faster/smaller than mov rax, 0)
lea rax, [rax*2] ; multiply by 2 without touching flags
shl rax, 3 ; multiply by 8 (shift left 3 bits)
add rax, rax ; multiply by 2
sub rax, rax ; also zeroes it; like xor, it clears CF and sets ZF

What 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.