CPU Organisation, Instructions and Pipelining
Learn how a CPU fetches, decodes and executes instructions, what an ISA is, and how pipelining, hazards and branch prediction affect performance.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Number Representation and Digital Logic
You rarely need assembly for SDE work, but understanding what the CPU actually does explains performance puzzles and shows up in core CS rounds at hardware and systems companies.
Organisation
- Von Neumann architecture: CPU, memory and I/O connected by buses, with instructions and data in the same memory.
- The CPU has an ALU, a control unit and registers (program counter, instruction register, general-purpose registers).
- The instruction cycle: fetch, decode, execute, memory access, write back.
- An ISA (instruction set architecture) is the contract between software and hardware. RISC (ARM, RISC-V: simple fixed-length instructions) vs CISC (x86: complex variable-length instructions).
- Addressing modes: immediate, register, direct, indirect, indexed.
Pipelining
- Like an assembly line, different instructions occupy different stages at once, improving throughput (not the latency of one instruction).
- Ideal speedup approaches the number of stages; numericals ask for time with and without pipelining.
- Hazards break the flow:
- Structural: two stages need the same hardware.
- Data: an instruction needs a result not yet written (fixed by forwarding or stalls).
- Control: branches mean the next instruction is unknown (fixed by branch prediction).
- This is why a sorted array can make a branchy loop dramatically faster: the predictor guesses right.
Performance
- CPU time = instruction count x CPI x clock cycle time.
- Multicore processors, hyperthreading and why parallel speedup is limited by Amdahl's law.
- Interrupts and how the CPU switches into a handler.
Good enough to move on: you can explain the five pipeline stages, identify the hazard in a short instruction sequence, and apply Amdahl's law to a speedup question.
Key concepts
- Von Neumann architecture
- Registers, ALU and control unit
- Instruction cycle
- RISC vs CISC
- Addressing modes
- Pipelining and speedup
- Structural, data and control hazards
- Branch prediction
- Amdahl's law
Mini project
Write a tiny CPU simulator for a made-up 8-instruction ISA (LOAD, STORE, ADD, SUB, JMP, JZ, IN, OUT), run a program that sums numbers, and then benchmark summing values above a threshold in a sorted vs unsorted array to observe branch prediction.
Check yourself
Why does pipelining improve throughput but not the latency of a single instruction?
Free resources
- MIT 6.004 Computation Structures (MIT OCW)
- Nand2Tetris (Nand2Tetris)
- GeeksforGeeks: Computer Organization and Architecture (GeeksforGeeks)