Assembly and Computer Architecture
How a modern computer really runs your program, from a C++ function to the bytes, registers, caches and cores underneath, on x86-64, ARM64 and RISC-V.
Free to read now, no account needed: Before you start: programs, just enough C++, and the terminal. Create a free account to open the other chapters, take the quizzes and keep your progress.
My setup
Lessons that differ by operating system or tool show your choice first, and fold away notes meant for other setups. You can switch tabs in any lesson β it remembers what you pick.
Operating system
Processor family
Assembler
x86-64 syntax
Part 1
Readiness
Chapters 0–0
›
Part 2
What is really happening inside a computer
Chapters 1–6
›
1 From C++ to a running program π
Create a free account to open this chapter.
- The layers, top to bottom ~22 min
- The build pipeline as a black box ~30 min
- What an instruction is ~28 min
- Three instruction sets ~28 min
- Instruction set versus processor design ~24 min
- How to read a listing in this course ~24 min
2 Bits, bytes and hexadecimal π
Create a free account to open this chapter.
- Why computers use two symbols ~25 min
- Place value and binary ~35 min
- Hexadecimal as four-bit shorthand ~35 min
- Bytes, words and sizes ~30 min
- Unsigned integers: range and wraparound ~35 min
- Characters are numbers ~30 min
3 Signed integers and two's complement π
Create a free account to open this chapter.
- The sign problem and two rejected designs ~25 min
- Two's complement ~40 min
- Widening and narrowing ~35 min
- Overflow: signed versus unsigned ~40 min
- Mixing signed and unsigned in C++ ~35 min
4 Memory, addresses and pointers π
Create a free account to open this chapter.
- Memory as a numbered row of bytes ~30 min
- Endianness ~35 min
- A pointer is an address ~40 min
- Arrays and address arithmetic ~40 min
- Alignment, a first look ~30 min
- The process memory map ~35 min
5 Inside the CPU π
Create a free account to open this chapter.
- Registers: the processor's own tiny, fast storage ~28 min
- The ALU and the control unit ~26 min
- The program counter and the fetch-decode-execute cycle ~35 min
- Two designs: load/store and register-memory ~34 min
- The register files of x86-64, ARM64 and RISC-V ~36 min
- The clock, and what "a 4 GHz CPU" does and doesn't tell you ~30 min
6 Your first assembly programs π
Create a free account to open this chapter.
- Installing your toolchain ~30 min
- An assembly function called from C++ ~35 min
- Assemble, link, run ~35 min
- Reading the compiler's version ~35 min
- Watching it run in a debugger ~35 min
- The same function in RISC-V ~25 min
Part 3
Assembly fundamentals
Chapters 7–14
›
7 Moving data π
Create a free account to open this chapter.
- Register-to-register moves and immediates ~35 min
- Register widths and the zeroing rules ~35 min
- Loading and storing memory ~40 min
- Addressing modes ~45 min
- Sign- and zero-extending loads ~40 min
- Large constants and addresses ~40 min
8 Arithmetic, logic and flags π
Create a free account to open this chapter.
- Add, subtract, negate ~35 min
- Status flags ~40 min
- Detecting overflow and adding wide numbers ~40 min
- Multiply and divide ~40 min
- Bitwise operations and masks ~40 min
- Shifts and rotates ~40 min
- Compiler arithmetic idioms ~40 min
9 Comparisons and branches π
Create a free account to open this chapter.
- Labels and unconditional jumps ~30 min
- Compare is a subtract that only sets flags ~35 min
- Conditional branches, signed and unsigned ~45 min
- Translating if/else ~45 min
- cbz, cbnz, tbz and other shortcuts ~30 min
- Branchless selection ~40 min
- switch and jump tables ~45 min
10 Loops π
Create a free account to open this chapter.
- while and do-while as branches back ~45 min
- for loops and counters ~40 min
- Walking an array with an index or a pointer ~45 min
- Nested loops ~40 min
- Off-by-one and other loop bugs, found in the debugger ~45 min
- What the compiler does to a loop at -O0 and -O2 ~40 min
11 Functions and the stack π
Create a free account to open this chapter.
- Calling and returning ~40 min
- The stack and the stack pointer ~45 min
- Stack frames ~45 min
- Prologue and epilogue ~40 min
- Leaf functions and the red zone ~35 min
- Watching frames in the debugger ~40 min
12 Calling conventions π
Create a free account to open this chapter.
- What an ABI is and why it exists ~35 min
- System V AMD64 (Linux, macOS on Intel) ~45 min
- Microsoft x64 (Windows) ~45 min
- AAPCS64 and Apple's ARM64 variant ~40 min
- RISC-V LP64D, briefly ~30 min
- More arguments than registers, and returning structs ~45 min
- Calling C++ from assembly and assembly from C++ ~45 min
13 Arrays, strings and structs π
Create a free account to open this chapter.
- Indexing an array ~45 min
- Two-dimensional arrays ~40 min
- C strings in assembly ~50 min
- Structs: field offsets ~40 min
- Alignment and padding ~45 min
- Pointers to structs and arrays of structs ~45 min
14 Recursion and data structures in memory π
Create a free account to open this chapter.
- A recursive function's frames ~45 min
- Stack depth and stack overflow ~40 min
- Linked lists in memory ~45 min
- Binary trees and hash tables ~50 min
- Classes and member functions ~45 min
- Virtual functions and vtables ~50 min
Part 4
Floating point
Chapters 15–16
›
15 Floating-point numbers: IEEE 754 π
Create a free account to open this chapter.
- Fixed point and its limits ~40 min
- Sign, exponent and fraction ~45 min
- Decoding and encoding by hand ~50 min
- Zeros, infinities, NaNs and subnormals ~50 min
- Rounding, ulp and machine epsilon ~50 min
- Why 0.1 + 0.2 is not 0.3 ~45 min
16 Floating-point instructions π
Create a free account to open this chapter.
- Floating-point registers ~40 min
- Arithmetic, conversion and comparison ~55 min
- Floating-point in the calling conventions ~45 min
- Fused multiply-add ~40 min
- Numerical pitfalls at machine level ~55 min
Part 5
From C++ to assembly
Chapters 17–19
›
17 How a compiler works π
Create a free account to open this chapter.
- The compiler pipeline ~40 min
- Reading LLVM IR ~50 min
- Register allocation and spilling ~45 min
- Reading -O0 output systematically ~45 min
18 Optimization and honest measurement π
Create a free account to open this chapter.
- Folding, inlining and dead code ~45 min
- Loop optimizations ~50 min
- What each optimization level turns on ~40 min
- Undefined behaviour and the optimizer ~45 min
- Measuring honestly ~40 min
- Reading optimized output ~50 min
19 Mixing C++ and assembly π
Create a free account to open this chapter.
- Assembly files in a CMake project ~45 min
- Name mangling and extern "C" ~45 min
- Symbol tables, a first look ~40 min
- Inline assembly ~55 min
- Intrinsics instead of inline assembly ~40 min
- Porting between x86-64 and ARM64 ~55 min
Part 6
Executables, linking and the operating system
Chapters 20–23
›
20 Machine code: how instructions become bytes π
Create a free account to open this chapter.
- From mnemonic to bytes ~40 min
- Fixed 32-bit formats on ARM64 and RISC-V ~50 min
- x86-64 variable-length encoding ~55 min
- Decoding bytes back to instructions ~45 min
- Branch offsets are relative ~50 min
21 Object files, symbols and relocations π
Create a free account to open this chapter.
- What an object file holds ~40 min
- Sections ~45 min
- The symbol table in depth ~45 min
- Why the assembler cannot finish ~40 min
- Reading relocation entries ~50 min
- ELF, Mach-O and COFF objects compared ~40 min
22 Linking and libraries π
Create a free account to open this chapter.
- What the linker does ~50 min
- Static libraries and link order ~45 min
- Shared libraries and dynamic linking ~45 min
- Position-independent code ~45 min
- Calling into a shared library ~50 min
- ELF, PE and Mach-O executables compared ~40 min
23 Loading, startup and system calls π
Create a free account to open this chapter.
- The loader and the entry point ~45 min
- From the entry point to main ~45 min
- User mode and kernel mode ~35 min
- System calls on Linux ~50 min
- Why macOS and Windows go through libraries ~45 min
- A freestanding program ~45 min
Part 7
Inside a modern processor
Chapters 24–26
›
24 Pipelining π
Create a free account to open this chapter.
- One instruction set, many processors ~35 min
- A single-cycle processor ~40 min
- The five-stage pipeline ~45 min
- Hazards, stalls and forwarding ~50 min
- Control hazards and flushes ~45 min
- CPI and the performance equation ~45 min
25 Branch prediction and speculation π
Create a free account to open this chapter.
- Why the processor must guess ~40 min
- One-bit and two-bit predictors ~45 min
- Predictors that use history ~45 min
- Misprediction cost and speculation ~40 min
- Branchless code revisited ~45 min
- Measuring branch prediction ~50 min
26 Superscalar and out-of-order execution π
Create a free account to open this chapter.
- More than one instruction per cycle ~40 min
- False dependences and register renaming ~45 min
- Out-of-order execution and the reorder buffer ~50 min
- Dependency chains and multiple accumulators ~50 min
- Micro-ops and fusion ~45 min
- A tour of a real core ~40 min
Part 8
Memory and the operating system
Chapters 27–30
›
27 Caches π
Create a free account to open this chapter.
- Why memory is slow ~35 min
- The memory hierarchy ~45 min
- Cache lines and locality ~45 min
- Direct-mapped caches ~50 min
- Set-associative caches and replacement ~50 min
- Writes and the three kinds of miss ~50 min
28 Writing cache-friendly code π
Create a free account to open this chapter.
- Traversal order and stride ~45 min
- Alignment and cache lines ~40 min
- AoS and SoA revisited ~40 min
- Blocking and tiling ~50 min
- Hardware prefetching ~40 min
- Measuring memory performance ~55 min
29 Virtual memory π
Create a free account to open this chapter.
- Why virtual memory ~35 min
- Pages and address translation ~45 min
- Page table entries and permissions ~45 min
- Multi-level page tables ~45 min
- The TLB and huge pages ~45 min
- Page faults and demand paging ~50 min
30 Exceptions, interrupts and signals π
Create a free account to open this chapter.
- When the processor stops your program ~45 min
- Privilege levels ~40 min
- What the processor does on an exception ~50 min
- The page-fault path ~45 min
- Timer interrupts and preemption ~45 min
- Signals and structured exceptions ~55 min
Part 9
SIMD
Chapters 31–32
›
31 SIMD fundamentals π
Create a free account to open this chapter.
- One instruction, many lanes ~45 min
- Vector registers on x86-64 and ARM64 ~50 min
- Lane-wise arithmetic, compares and blends ~55 min
- Shuffles and horizontal operations ~55 min
- AVX-512 and masked operations ~50 min
- Scalable vectors (SVE and RISC-V V) ~50 min
32 Vectorization in practice π
Create a free account to open this chapter.
- Reading auto-vectorized loops ~45 min
- What stops the vectorizer ~50 min
- Writing SIMD with intrinsics ~50 min
- SIMD image processing ~55 min
- Numerical kernels and the performance laboratory ~55 min
- Dispatching on CPU features ~45 min
Part 10
Concurrency at the machine level
Chapters 33–34
›
33 Cores, threads and atomic instructions π
Create a free account to open this chapter.
- Cores and hardware threads ~40 min
- Threads share memory ~45 min
- Why count++ is not atomic ~50 min
- Atomic instructions on x86-64 ~55 min
- Atomics on ARM64 and RISC-V ~55 min
- CAS loops and a spinlock ~55 min
34 Coherence, ordering and barriers π
Create a free account to open this chapter.
- Cache coherence and MESI ~50 min
- False sharing ~50 min
- Store buffers and x86-TSO ~55 min
- Weak ordering on ARM64 and RISC-V ~55 min
- Acquire, release and C++ atomics ~55 min
- Lock-free ideas and the ABA problem ~50 min
Part 11
Debugging and reverse engineering
Chapters 35–36
›
35 Debugging at the instruction level π
Create a free account to open this chapter.
- How breakpoints work ~45 min
- Watchpoints and debug registers ~40 min
- Reading registers and memory ~45 min
- Stack inspection and unwinding ~45 min
- Debugging optimized code ~45 min
- Four debuggers side by side ~40 min
36 Reading disassembly and crashes π
Create a free account to open this chapter.
- Recognizing compiler idioms ~50 min
- Reconstructing C++ from a listing ~55 min
- Why programs crash ~55 min
- Reading a crash ~55 min
- Core dumps and minidumps ~45 min
Part 12
Security
Chapters 37–37
›
37 Memory safety and its mitigations π
Create a free account to open this chapter.
- Buffer overflows and what they corrupt ~50 min
- Stack canaries ~50 min
- Non-executable memory ~45 min
- ASLR and PIE ~45 min
- Control-flow protection ~55 min
- Sanitizers and memory-safe languages ~50 min
Part 13
Modern machines
Chapters 38–38
›
38 The modern processor landscape π
Create a free account to open this chapter.
- Inside a modern chip ~45 min
- Performance and efficiency cores ~40 min
- Frequency, turbo and power ~45 min
- Memory bandwidth and NUMA ~45 min
- GPUs, NPUs and accelerators ~50 min
- Virtualization and cloud CPUs ~50 min
Part 14
Capstone
Chapters 39–39
›
39 Capstone: an assembler and emulator π
Create a free account to open this chapter.
- The plan and the RV64I subset ~45 min
- Milestone 1: reading assembly text ~50 min
- Milestone 2: the two-pass assembler ~60 min
- Milestone 3: the emulator ~60 min
- Milestone 4: running and testing real programs ~55 min
- The source-to-execution report ~60 min
Appendices
Reference chapters on the tools the course uses. Dip in when a lab or a section sends you here; they are not part of the course exam.
A ISA quick-reference cards π
Create a free account to open this chapter.
- The x86-64 card ~20 min
- The ARM64 card ~20 min
- The RISC-V card ~20 min
- Calling conventions side by side ~20 min
- Instruction encodings at a glance ~20 min
B Toolchains and tools per OS π
Create a free account to open this chapter.
- Installing a toolchain on each system ~15 min
- Build, assemble and link commands ~15 min
- Inspecting object files and executables ~20 min
- Debugger commands on each system ~20 min
- Compiler Explorer and other views of the compiler ~15 min