Assembly Language
Read and write simple assembly — registers, loads and stores, branches and loops, function calls and the stack — and see what compilers generate from your code.
Assembly is a human-readable form of machine code: one line per instruction. You'll rarely write it, but being able to read it lets you understand performance, debug crashes, and see exactly what the compiler did with your code. The best way to learn is Compiler Explorer (godbolt.org): type code on the left, see the assembly on the right.
Hello, registers
RISC-V syntax is the easiest to read, so we'll start there:
addi a0, zero, 5 # a0 = 0 + 5
addi a1, zero, 7 # a1 = 7
add a2, a0, a1 # a2 = a0 + a1 = 12
slli a3, a2, 2 # a3 = a2 << 2 = 48The pattern is op destination, source1, source2. The zero register always reads as 0.
Memory: loads and stores
lw t0, 0(a0) # t0 = memory[a0 + 0] (load word, 4 bytes)
lw t1, 4(a0) # t1 = memory[a0 + 4]
add t2, t0, t1
sw t2, 8(a0) # memory[a0 + 8] = t2 (store word)offset(base) is base + offset addressing. If a0 holds the address of a struct, the offsets are its fields; for an array, 4 × i selects element i.
Control flow: if and loops
There's no if or while, only compare and branch.
// Sum the integers in an array
int sum(int[] a, int n) {
int s = 0;
for (int i = 0; i < n; i++) s += a[i];
return s;
}# a0 = pointer to the array, a1 = n
sum:
li t0, 0 # s = 0
li t1, 0 # i = 0
loop:
bge t1, a1, done # if i >= n goto done
slli t2, t1, 2 # t2 = i * 4 (byte offset)
add t2, a0, t2 # t2 = &a[i]
lw t3, 0(t2) # t3 = a[i]
add t0, t0, t3 # s += a[i]
addi t1, t1, 1 # i++
j loop
done:
mv a0, t0 # return value in a0
ret; rdi = pointer, esi = n
sum:
xor eax, eax ; s = 0
test esi, esi
jle .done ; n <= 0 → return 0
lea rdx, [rdi + rsi*4] ; end pointer
.loop:
add eax, DWORD PTR [rdi] ; s += *p (memory operand!)
add rdi, 4 ; p++
cmp rdi, rdx
jne .loop
.done:
retFunction calls and the stack
A call saves the return address and jumps; the callee sets up a stack frame for its locals and saved registers.
# int f(int x) { return g(x) + 1; } (RISC-V)
f:
addi sp, sp, -16 # allocate a 16-byte frame
sw ra, 12(sp) # save the return address (we're about to call g)
call g # ra = return address; jump to g (arg already in a0)
addi a0, a0, 1 # result + 1
lw ra, 12(sp) # restore the return address
addi sp, sp, 16 # free the frame
ret # jump to raThat pattern — prologue (allocate the frame, save registers), body, epilogue (restore, free, return) — appears at the top and bottom of almost every compiled function.
x86-64 cheat sheet
| Instruction | Meaning |
|---|---|
mov rax, rbx | rax = rbx |
mov rax, [rbx+8] | load 8 bytes from rbx + 8 |
lea rax, [rbx+rcx*4] | rax = rbx + rcx×4 (address arithmetic, no memory access) |
add / sub / imul rax, rbx | arithmetic (two operands: the destination is also a source) |
cmp a, b then je / jne / jl / jg | compare (sets flags), conditional jump |
push / pop | stack operations |
call f / ret | call (push the return address) / return |
Two syntaxes exist: Intel (mov rax, rbx, destination first) and AT&T (movq %rbx, %rax, source first, % on registers), which is the default in GNU tools.
Why read assembly?
- Performance: check that a loop was vectorised, that a call was inlined, or that bounds checks were removed.
- Debugging: crash dumps and debuggers show instructions and registers when source isn't available.
- Security: exploits, reverse engineering and malware analysis all happen at this level.
- Understanding: how virtual calls, closures and exceptions really work.
- Assembly is one human-readable line per machine instruction.
- Values move between registers and memory with loads and stores; ALU ops work on registers (on RISC).
- Loops and ifs become compare-and-branch.
- Calls follow the ABI: arguments in registers, the return address saved, a stack frame with a prologue and epilogue.
- Use Compiler Explorer to see what your code compiles to.