Module 3 — CPU Registers
Learning Objectives
After completing this module, you should be able to answer:
- What is a register?
- Why are registers the fastest memory?
- How many registers does a CPU have?
- What are General Purpose Registers?
- What is Program Counter?
- What is Stack Pointer?
- What is Instruction Register?
- What is Flag Register?
- What is Link Register (ARM)?
- What is Frame Pointer?
- How are registers used during function calls?
- How does the OS save registers during a context switch?
1. What is a Register?
A register is a very small storage location inside the CPU.
It stores:
- Data
- Addresses
- Instructions
- CPU state
Think of registers as the CPU's working desk.
RAM is like a bookshelf.
SSD is like a warehouse.
The CPU prefers to keep frequently used values on its desk instead of walking to the bookshelf every time.
Memory Hierarchy
Fastest
Slowest
Why are Registers Fast?
Registers are physically inside the CPU core.
CPU
There is almost no distance for data to travel.
Access time is typically one CPU cycle.
RAM may take tens to hundreds of cycles.
Register Sizes
Different CPUs have different register widths.
- 8-bit CPU: Register = 8 bits
- 16-bit CPU: Register = 16 bits
- 32-bit CPU: Register = 32 bits
- 64-bit CPU: Register = 64 bits
Example
32-bit register:
00000000 00000000
00000000 00001010
Stores 10
Types of Registers
The CPU contains different registers for different purposes.
CPU
We'll focus on the most important ones first.
2. General Purpose Registers (GPR)
These are the CPU's "scratchpad."
They store:
- Variables
- Intermediate results
- Function arguments
- Addresses
Example
int a = 10;
int b = 20;
int c = a + b;
Machine execution
Registers
- R1 = 10
- R2 = 20
- R3 = 30
The ALU performs operations on register values.
ARM Registers
ARM Cortex typically has:
Where R0-R12 are General Registers.
x86 Registers
Each has traditional roles, but modern compilers use them flexibly.
3. Program Counter (PC)
One of the most important registers.
The Program Counter stores the address of the next instruction to execute.
Example Program
| Address | Instruction |
|---|---|
| 1000 | MOV R1,#10 |
| 1004 | ADD R2,R1,#20 |
| 1008 | SUB R3,R2,#5 |
- Initially:
PC = 1000 - CPU fetches:
MOV - After execution:
PC = 1004 - Next:
PC = 1008 - And so on.
Visualization
The Program Counter keeps moving through the program.
Branch Example
Suppose if(a>10)
The PC no longer goes to the next sequential instruction. Instead:
This is how loops, if, switch, and function calls work.
4. Stack Pointer (SP)
The Stack Pointer points to the top of the stack.
Every function call changes the Stack Pointer.
Example
void fun()
{
int a;
}
Entering the function
Returning
Stack Growth
On most architectures, the stack grows downward.
5. Instruction Register (IR)
The Instruction Register stores the current instruction being executed.
Suppose memory contains ADD R1,R2
Flow
The IR acts like the CPU's "currently open page."
6. Status Register (Flags Register)
The CPU needs to know the result of operations.
Example: 5-5=0
How does it know the result is zero? Through status flags.
Typical Flags
- Zero Flag (Z)
- Carry Flag (C)
- Negative Flag (N)
- Overflow Flag (V)
Example: 10-10
Result: 0
The CPU sets: Zero Flag = 1
Then if(result==0) can use that flag to decide the branch.
7. Link Register (LR) — ARM
ARM has a special register called the Link Register.
Purpose: Store the return address during a function call.
Example
Before jumping to fun():
When fun() finishes, BX LR returns to the caller.
This reduces stack accesses for simple function calls.
8. Frame Pointer (FP)
Frame Pointer identifies the base of the current function's stack frame.
Debuggers rely heavily on the Frame Pointer to reconstruct call stacks.
9. Register Usage During Function Calls
Example
int add(int a, int b)
{
return a+b;
}
ARM calling convention
Small arguments stay in registers.
Large structures are passed via memory.
10. Context Switching
This is where registers become critical for the OS.
Suppose Process A is running.
Registers: PC, SP, R0, R1, R2, ...
A timer interrupt occurs.
The OS must save all CPU state.
Then restore Process B's saved state.
This is why a process can resume exactly where it stopped.
Embedded Example
In an ADAS camera pipeline:
The task resumes because its registers (PC, SP, GPRs, flags) were saved and restored.
ARM vs x86 Register Comparison
| ARM | x86 |
|---|---|
| R0-R12 | RAX, RBX, RCX, RDX... |
| SP | RSP |
| LR | Return address stored on stack (typically) |
| PC | RIP |
| CPSR | RFLAGS |
ARM's dedicated Link Register is a notable difference.
Interview Questions
Beginner
- What is a register?
- Why are registers faster than RAM?
- What is the Program Counter?
- What is the Stack Pointer?
- What is the Instruction Register?
Intermediate
- Explain how a function call uses registers.
- Why does the Stack Pointer change?
- What are CPU flags?
- Why is the Program Counter important?
- What is the purpose of the Frame Pointer?
Senior
- How does the OS perform a context switch?
- Which registers must be saved during an interrupt?
- How does ARM use the Link Register?
- Explain register allocation by the compiler.
- Why are calling conventions important?
Common Mistakes
- ❌ Thinking registers are part of RAM.
- ❌ Assuming the Program Counter points to the current instruction (it generally points to the next instruction to fetch, though exact behavior depends on the architecture and pipeline).
- ❌ Believing the Stack Pointer always increases. On most CPUs, it decreases as the stack grows.
- ❌ Assuming all variables remain in registers. The compiler spills values to memory when registers are insufficient or when required.
Summary
Registers are the CPU's fastest storage and hold the immediate execution state of a program. Specialized registers such as the Program Counter, Stack Pointer, Instruction Register, and Status Register enable instruction execution, function calls, branching, interrupts, and multitasking. Operating systems rely on saving and restoring these registers during context switches, making them fundamental to process and thread management.
Mini Quiz
- Why are registers faster than cache and RAM?
- What does the Program Counter store?
- Why is the Stack Pointer needed?
- What information does the Status Register contain?
- What is the purpose of ARM's Link Register?
- Which registers must an operating system preserve during a context switch?
- How does the Frame Pointer help a debugger?
Concept Mastery Assessment — Module 03
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.