Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CPU Architecture

Overview

The Central Processing Unit (CPU) is the brain of a computer system. It fetches instructions from memory, decodes them, executes them, and writes back results. Understanding CPU architecture is fundamental to systems programming, performance optimization, and answering placement interview questions.

This section covers the foundational models (Von Neumann and Harvard), instruction set architectures, the CISC vs RISC debate, and the internal components—registers, ALU, control unit, and microcode—that make a CPU work.

CPU Block Diagram

graph TB
    subgraph "CPU Core"
        subgraph "Frontend"
            FETCH[Fetch Unit]
            DECODE[Decode Unit]
            RENAME[Register Rename]
        end
        subgraph "Backend"
            ISSUE[Issue/Dispatch]
            subgraph "Execution Units"
                ALU0[ALU 0]
                ALU1[ALU 1]
                FPU[FPU]
                LSU[Load/Store Unit]
                BRU[Branch Unit]
            end
            ROB[Reorder Buffer]
            RET[Retire Unit]
        end
        subgraph "Registers"
            ARF[Architectural Register File]
            PRF[Physical Register File]
        end
        subgraph "Cache"
            IL1[I-L1 Cache]
            DL1[D-L1 Cache]
            TLB[TLB]
        end
    end
    L2[L2 Cache] --> DL1
    L2 --> IL1
    MEM[Main Memory] --> L2

    FETCH -->|Instruction Bytes| DECODE
    DECODE -->|Micro-ops| RENAME
    RENAME --> ISSUE
    ISSUE --> ALU0
    ISSUE --> ALU1
    ISSUE --> FPU
    ISSUE --> LSU
    ISSUE --> BRU
    ALU0 --> ROB
    ALU1 --> ROB
    FPU --> ROB
    LSU --> ROB
    BRU --> ROB
    ROB --> RET
    FETCH -->|Request| IL1
    LSU -->|Data Request| DL1
    FETCH -->|Virtual Address| TLB

Core CPU Components

1. Control Unit (CU)

The control unit is the orchestrator of the CPU. It directs the operation of all other components.

FunctionDescription
Instruction FetchSends PC to memory, receives instruction bytes
Instruction DecodeDetermines opcode, operands, and control signals
SequencingGenerates control signals in correct order
Exception HandlingResponds to interrupts, traps, and faults

Hardwired vs Microprogrammed Control:

TypeSpeedFlexibilityUsed In
HardwiredFasterFixed, hard to modifyRISC CPUs
MicroprogrammedSlowerEasy to updateCISC CPUs (x86)

2. Arithmetic Logic Unit (ALU)

The ALU performs all arithmetic and logical operations.

┌─────────────────────────────────┐
│              ALU                │
│  ┌─────────┐  ┌─────────────┐  │
│  │ Adder   │  │ Logic Unit  │  │
│  │(Add/Sub)│  │(AND/OR/XOR) │  │
│  └─────────┘  └─────────────┘  │
│  ┌─────────┐  ┌─────────────┐  │
│  │ Shifter │  │ Comparator  │  │
│  │(<<>>)  │  │ (< > =)     │  │
│  └─────────┘  └─────────────┘  │
│                                 │
│  Inputs: A, B, ALUControl      │
│  Outputs: Result, Flags (Z,C,N,V)│
└─────────────────────────────────┘

ALU Operations:

OperationALUControlResult
ADD0000A + B
SUB0001A - B
AND0010A & B
OR0011A | B
XOR0100A ^ B
SLL0101A << B
SRL0110A >> B (logical)
SLT0111(A < B) ? 1 : 0

3. Registers

Registers are the fastest storage in the CPU, accessed in a single clock cycle.

Register Categories

CategoryExamplesPurpose
General PurposeRAX, RBX, RCX (x86-64); X0-X30 (ARM)Store operands and results
Program CounterRIP (x86-64); PC (ARM)Address of next instruction
Stack PointerRSP (x86-64); SP (ARM)Top of call stack
Flags/StatusRFLAGS (x86-64); CPSR (ARM)Condition codes (Z, C, N, V)
Floating PointXMM0-15 (x86-64); V0-V31 (ARM)Floating-point operands
ControlCR0-CR4 (x86-64)CPU configuration (paging, protection)
SegmentCS, DS, SS, ES (x86)Memory segmentation (legacy)

Register Count Comparison

ArchitectureGPRsFP/VectorTotal
x86-641616 (SSE/AVX) or 32 (AVX-512)48 (with AVX-512)
ARM64 (AArch64)3132 (V/NEON/SVE)63
RISC-V (RV64I)3132 (F/D)63
MIPS323264

4. Fetch-Decode-Execute Cycle

The fundamental cycle of CPU operation:

graph LR
    F[Fetch] --> D[Decode]
    D --> E[Execute]
    E --> M[Memory Access]
    M --> W[Write Back]
    W --> F

Detailed steps:

StageActionComponents Used
FetchRead instruction from memory at PC; PC += instruction_sizePC, I-Cache, Fetch Unit
DecodeParse opcode, identify operands, generate control signalsDecode Unit, Control Unit
ExecutePerform ALU operation, compute address, evaluate branchALU, Branch Unit
MemoryRead/write data from/to memory (if load/store)Load/Store Unit, D-Cache
Write BackWrite result to destination registerRegister File

5. Addressing Modes

How instructions specify operand locations:

ModeExampleDescription
ImmediateMOV R1, #42Operand is in the instruction
RegisterADD R1, R2, R3Operand is in a register
DirectMOV R1, [0x1000]Memory address in instruction
IndirectMOV R1, [R2]Memory address in register
IndexedMOV R1, [R2 + offset]Base + displacement
PC-RelativeBEQ labelPC + offset (branches)

CPU Performance Factors

What Makes a CPU Fast?

CPU Performance = f(Pipeline Depth, Width, Cache, Branch Prediction, OoO)

Pipeline Depth:    Deeper = higher clock rate, but higher branch mispredict penalty
Superscalar Width: Wider = more IPC, but harder to keep fed
Cache Hierarchy:   Larger/faster caches = fewer memory stalls
Branch Prediction: Better prediction = fewer pipeline flushes
Out-of-Order:      More reorder capacity = better ILP extraction

Pipeline Depth Tradeoff

DepthClock RateBranch PenaltyExample
Shallow (5-10)Lower~5 cyclesARM Cortex-M
Medium (12-15)Medium~16-19 cyclesIntel Skylake
Deep (20+)Higher~20 cyclesIntel Pentium 4 (NetBurst)

Interview Focus

  • Explain the Von Neumann bottleneck and how Harvard architecture addresses it
  • Compare CISC and RISC with real-world examples (x86 vs ARM)
  • Describe the fetch-decode-execute cycle step by step
  • Explain why registers are the fastest storage and how many a typical CPU has
  • Differentiate between the ISA (what the CPU can do) and microarchitecture (how it does it)
  • Describe the role of the ALU, control unit, and register file
  • Explain how addressing modes affect instruction encoding and flexibility

Cross References