Post

Components - CPU Registers

Components - CPU Registers

🧠 CPU Registers β€” Types, Roles, and Addressing Modes (Developer Guide)

A practical overview of CPU registers, what they store, and how they support instruction execution, memory access, and addressing.


1️⃣ What Is a Register?

A register is a small, ultra-fast storage location inside the CPU.

Registers store:

  • Instructions
  • Memory addresses
  • Operands (data)
  • Execution state

πŸ“Œ Registers are much faster than cache or RAM and are essential for performance.


2️⃣ Core CPU Registers

🟦 Program Counter (PC)

Holds the address of the next instruction to fetch.

  • Automatically updated after each instruction
  • Changed by jumps, calls, interrupts

🟦 Memory Address Register (MAR)

Stores the memory address being accessed.

Used when:

  • Reading from memory
  • Writing to memory

🟦 Memory Buffer Register (MBR / MDR)

Stores data being transferred between CPU and memory.

  • Can hold instructions or data
  • Temporary transfer buffer

🟦 Instruction Register (IR)

Holds the current instruction being decoded or executed.


🟦 Flag Register (Status Register)

Stores execution state and arithmetic results.

Common flags:

  • Zero (Z)
  • Carry (C)
  • Overflow (V)
  • Sign (S)
  • Parity (P)
  • Interrupt Enable
  • Privilege Mode

🟦 General-Purpose Registers (GPRs)

Used to store temporary values and operands.

Examples:

  • x86: RAX, RBX, RCX, RDX
  • ARM: X0–X30

🟦 Stack Pointer (SP)

Points to the top of the stack.

Used for:

  • Function calls
  • Local variables
  • Return addresses

Stack Addressing Mode

The stack pointer enables stack-based addressing:

1
2
PUSH value β†’ SP moves
POP value β†’ SP moves

🟦 Base Register (BR)

Stores a base address used for effective address calculation.

Used to access:

  • Arrays
  • Structures
  • Memory segments

3️⃣ Addressing Modes Using Registers

Addressing modes determine how effective memory addresses are calculated.


πŸ”Ή Displacement Addressing

1
Effective Address = Register + Offset

Used for structured data and arrays.


πŸ”Ή Relative Addressing

1
Effective Address = PC + Offset

Used for:

  • Branches
  • Loops
  • Position-independent code

πŸ”Ή Base Register Addressing

1
Effective Address = Base Register + Offset

Used in:

  • Stack frames
  • Dynamic memory access

πŸ”Ή Stack Addressing

1
Effective Address = SP + Offset

Used for:

  • Function calls
  • Temporary storage

4️⃣ Why Registers Matter for Performance

βœ” Reduce memory access
βœ” Speed up arithmetic operations
βœ” Enable fast function calls
βœ” Support efficient addressing

More register usage = fewer RAM accesses = faster execution


5️⃣ Registers in Real CPU Architectures

x86-64

  • RAX, RBX, RCX, RDX
  • RSP (Stack Pointer)
  • RIP (Program Counter)
  • RFLAGS

ARM64

  • X0–X30
  • SP, PC, PSTATE

6️⃣ Developer Takeaways

βœ” Registers are the fastest storage in the CPU
βœ” PC controls execution flow
βœ” SP manages function call stacks
βœ” Base registers enable flexible memory access
βœ” Effective addressing reduces instruction complexity


🧩 One-Line Mental Model

Registers are the CPU’s working memory for executing instructions efficiently.

This post is licensed under CC BY 4.0 by the author.