Repository navigation
Computer Architecture 02 fundamentals
Relevant source files
The following references were used as context for generating this wiki page:
- Wikipedia: Von Neumann architecture
- Wikipedia: Instruction set architecture
- Wikipedia: Memory hierarchy
- Arm: Arm Architecture Reference Manual for A-profile architecture (DDI 0487)
- Book: Patterson and Hennessy, Computer Organization and Design (Arm edition), chapters 1-2
A computer is a machine that follows a list of very small instructions such as "add these two numbers" or "copy this number from memory". This page explains the parts of a computer, what an instruction looks like, the loop the processor runs forever, and how to measure how fast it is.
This page covers the von Neumann model (processor, memory, input/output), registers, the ALU and control unit, the instruction set, the fetch-decode-execute cycle, RISC versus CISC, and the basic performance equation. Overlapping instructions is the subject of Pipelining.
Think of a cook in a kitchen:
| Kitchen | Computer | Notes |
|---|---|---|
| The cook | Processor (CPU) | Does the work |
| The recipe book | Program in memory | A numbered list of steps |
| A bookmark | Program counter (PC) | Which step is next |
| The cook's two hands | Registers | A few things held right now; instant to use |
| The worktop | Cache | Small, close, fast |
| The pantry | Main memory (RAM) | Large, but a walk away |
| The supermarket | Storage (SSD) | Huge, very slow to reach |
The cook reads a step, does it, moves the bookmark, and repeats. A processor does exactly that, billions of times per second.
Almost every computer follows the design described by John von Neumann in 1945: one memory holds both the program and its data, and the processor reads both from it over a shared path.
flowchart LR
subgraph CPU["Processor (CPU)"]
CU["Control unit<br/>decides what to do"]
ALU["ALU<br/>arithmetic and logic"]
REG["Registers<br/>X0..X30, SP, PC"]
CU --> ALU
REG <--> ALU
end
MEM["Memory<br/>program + data"] <-->|"address, data"| CPU
IO["Input / output<br/>keyboard, screen, network"] <--> CPU
| Part | Job |
|---|---|
| Registers | Tiny, very fast storage inside the CPU. Arm A64 has 31 general-purpose 64-bit registers, X0 to X30
|
| ALU (arithmetic logic unit) | Adds, subtracts, compares, does AND/OR/shift |
| Control unit | Reads each instruction and tells the other parts what to do |
| Program counter (PC) | Holds the memory address of the next instruction |
| Memory | A long array of bytes, each with a numbered address |
Because instructions and data share the path to memory, the processor often waits for memory. This is called the von Neumann bottleneck, and caches (Caches and Coherence) exist mainly to ease it.
Sources: Wikipedia: Von Neumann architecture
An instruction is a number, usually 4 bytes on Arm, that encodes one small operation. Written in assembly language it is readable:
// illustrative Arm A64 assembly for: total = price * qty + tax
LDR X1, [X0] // load price from memory address in X0 into X1
LDR X2, [X0, #8] // load qty from the next 8 bytes
MUL X3, X1, X2 // X3 = X1 * X2
LDR X4, [X0, #16] // load tax
ADD X5, X3, X4 // X5 = X3 + X4
STR X5, [X0, #24] // store total back to memory
Instructions fall into a few groups:
| Group | Examples | What it does |
|---|---|---|
| Arithmetic / logic |
ADD, SUB, MUL, AND, LSL
|
Compute on registers |
| Memory |
LDR (load), STR (store) |
Move data between memory and registers |
| Control flow |
B (branch), B.EQ, CBZ, BL (call), RET
|
Change which instruction runs next |
| Compare | CMP |
Set condition flags used by conditional branches |
Arm is a load/store architecture: arithmetic only works on registers, so data must be loaded first and stored afterwards. That is why the example above has more LDRs than calculations.
Sources: Wikipedia: Instruction set architecture
The processor runs this loop forever:
flowchart TD
F["1. Fetch<br/>read instruction at PC from memory"] --> D["2. Decode<br/>what operation? which registers?"]
D --> E["3. Execute<br/>ALU does the work"]
E --> M["4. Memory<br/>load or store if needed"]
M --> W["5. Write back<br/>put result in a register"]
W --> P["6. Update PC<br/>next instruction, or branch target"]
P --> F
Following ADD X5, X3, X4 through the loop:
| Step | What happens |
|---|---|
| Fetch | Read the 4 bytes at the address in PC |
| Decode | It is an ADD; sources X3 and X4; destination X5
|
| Execute | ALU computes X3 + X4
|
| Memory | Nothing to do: ADD does not touch memory |
| Write back | Result goes into X5
|
| Update PC | PC = PC + 4 |
A simple processor does all six steps for one instruction before starting the next. Pipelining overlaps them.
| RISC (reduced instruction set) | CISC (complex instruction set) | |
|---|---|---|
| Examples | Arm, RISC-V | x86 |
| Instruction size | Fixed (Arm A64: 4 bytes) | Variable (x86: 1 to 15 bytes) |
| Memory access | Only loads and stores | Many instructions can read memory directly |
| Decoding | Simple | Complex |
| Instructions per program | More | Fewer |
In practice modern x86 chips translate their complex instructions into simple RISC-like internal operations ("micro-ops"), so inside, big RISC and CISC cores look much alike.
The time a program takes is:
Execution time = Instruction count × CPI × Clock period
| Term | Meaning | Decided by |
|---|---|---|
| Instruction count | How many instructions run | Program, compiler, ISA |
| CPI (cycles per instruction) | Average clock cycles per instruction | Microarchitecture: pipeline, caches, prediction |
| Clock period | Length of one cycle (1 / frequency) | Circuit design and power |
IPC (instructions per cycle) is 1 / CPI. Big modern cores aim for IPC above 1 by running several instructions per cycle (ILP).
A program runs 2 billion instructions on a 2 GHz processor (clock period 0.5 ns).
| Design | CPI | Time |
|---|---|---|
| Simple, non-pipelined, 5 cycles per instruction | 5 | 2×10⁹ × 5 × 0.5 ns = 5.0 s |
| Pipelined, ideal | 1 | 2×10⁹ × 1 × 0.5 ns = 1.0 s |
| Pipelined with stalls (cache misses, branches) | 1.5 | 1.5 s |
| Superscalar, 2 per cycle | 0.5 | 0.5 s |
The rest of this wiki is mostly about lowering CPI.
Fast memory is small and expensive; big memory is slow. So computers use layers:
flowchart TD
R["Registers<br/>~1 KB, under 1 ns"] --> L1["L1 cache<br/>32-64 KB per core, ~1 ns"]
L1 --> L2["L2 cache<br/>0.5-2 MB, ~3-5 ns"]
L2 --> L3["L3 / system cache<br/>several MB shared, ~10-20 ns"]
L3 --> RAM["Main memory (DRAM)<br/>GBs, ~80-100 ns"]
RAM --> SSD["Storage (SSD)<br/>TBs, ~100 µs"]
Sizes and times are typical orders of magnitude, not figures for a particular chip. The trick that makes this work is locality: programs tend to reuse recent data (temporal locality) and use data next to recent data (spatial locality). See Caches and Coherence.
Sources: Wikipedia: Memory hierarchy
- A CPU has registers, an ALU and a control unit; memory holds both program and data.
- The processor loops: fetch, decode, execute, memory, write back, update PC.
- Time = instructions × CPI × clock period; most architecture tricks reduce CPI.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look