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.

Intermediate 2 min readLesson 7 of 15

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:

Assembly
    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 = 48

The pattern is op destination, source1, source2. The zero register always reads as 0.

Memory: loads and stores

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

Summing an array
// 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;
}

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

Assembly
# 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 ra
g: locals← topf: saved racaller's frame
The stack grows downward with each call.

That 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

InstructionMeaning
mov rax, rbxrax = 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, rbxarithmetic (two operands: the destination is also a source)
cmp a, b then je / jne / jl / jgcompare (sets flags), conditional jump
push / popstack operations
call f / retcall (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.
Key takeaways
  • 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.