Module 4 — Program Counter (PC) / Instruction Pointer (IP)
Learning Objectives
By the end of this module, you should be able to explain:
- What is the Program Counter?
- Why is it needed?
- How does it change during execution?
- How do loops work?
- How do if, switch, and goto work?
- How do function calls change the PC?
- How do interrupts change the PC?
- How does the OS use the PC during context switching?
- ARM vs x86 PC differences
1. What is the Program Counter?
The Program Counter (PC) is a special CPU register that contains the address of the next instruction to execute.
Think of it as the CPU's bookmark.
Example:
Suppose memory contains:
Address Instruction
1000 MOV R1,#10
1004 ADD R2,R1,#5
1008 SUB R3,R2,#2
1012 STORE R3,[2000]
Initially,PC = 1000
The CPU fetches:MOV R1,#10
After fetching,PC = 1004
After the next instruction,PC = 1008
The PC keeps advancing through the program.
Graphical View
Memory
After one instruction:
Memory
The PC simply moves from one instruction to the next during normal execution.
2. Why Do We Need a Program Counter?
Imagine reading a book.
Without a bookmark, every time you closed the book, you would have to start from page 1.
The PC is that bookmark.
It tells the CPU:
"This is where you should continue executing."
Without a PC, the CPU would have no idea which instruction comes next.
3. Sequential Execution
Consider:
int a = 10;
int b = 20;
int c = a + b;
Compiler (simplified):
1000 LOAD a
1004 LOAD b
1008 ADD
1012 STORE c
Execution:
This is called sequential execution.
4. Fetch-Decode-Execute with PC
Every instruction begins with the PC.
Notice the cycle always returns to updating the PC.
5. What Happens Inside the CPU?
SupposePC =1000
- Step 1
CPU readsMemory[1000] - Step 2
Instruction goes into Instruction Register - Step 3
Control Unit decodes it. - Step 4
ALU executes it. - Step 5
PC becomes1004
6. How Does a Loop Work?
Example
for(int i=0;i<5;i++)
{
printf("%d",i);
}
People often think the CPU "understands" a for loop.
It doesn't.
It only understands jump instructions.
Simplified Assembly
1000 MOV i,0
1004 CMP i,5
1008 JGE 1024
1012 PRINT i
1016 INC i
1020 JMP 1004
1024 EXIT
Execution
Notice:
The PC jumps backwards.
That's the loop.
Visualization
↓
1008
↓
1012
↓
1016
↓
1020
↓
↑
└───────────────
A loop is simply the PC repeatedly jumping to an earlier address.
7. How Does an if Statement Work?
Example
if(a>b)
{
x=1;
}
else
{
x=2;
}
Assembly (simplified)
1000 CMP a,b
1004 JLE 1016
1008 MOV x,1
1012 JMP 1020
1016 MOV x,2
1020 Continue
If a>b
Otherwise
The Program Counter decides which block executes.
8. Function Calls
Example
Assembly
1000 CALL add
1004 PRINT
2000 ADD
2004 RETURN
Execution
The return address is saved (in the Link Register on ARM or on the stack on x86), allowing execution to continue correctly.
Visualization
Main
Function
9. Interrupt Example
Suppose
The CPU is executingPC=1500
Suddenly
Camera interrupt occurs.
CPU automatically
Suppose ISR starts 5000
Execution
The CPU resumes exactly where it stopped.
Embedded Example
In your ADAS project:
This happens in microseconds.
Without saving the PC, the interrupted task could never resume correctly.
10. Context Switching
Suppose Process A is running.PC=3000
PC=3000
PC=9000
CPU continues9000
Later
Process A resumesPC=3000
The OS stores the PC in the Process Control Block (PCB) along with other registers.
Visualization
PC=3000
PC=9000
ARM vs x86
| ARM | x86 |
|---|---|
| PC register | RIP (Instruction Pointer) |
| Uses Link Register for many function returns | Typically pushes return address onto stack |
| Fixed-width instructions (common in AArch32; AArch64 uses fixed 32-bit instructions) | Variable-length instructions |
Debugger Example
When you set a breakpoint:
printf("Hello");
The debugger watches the Program Counter.
WhenPC == breakpoint address
Execution pauses.
Single-stepping simply executes one instruction, updates the PC, then pauses again.
Why Is the PC So Important for an OS?
Without the Program Counter:
- No process execution
- No multitasking
- No function calls
- No interrupts
- No scheduler
- No debugging
- No exceptions
- No context switching
The PC represents where a thread or process is currently executing.
Interview Questions
Beginner
- What is the Program Counter?
- What information does it store?
- Why is it needed?
Intermediate
- How does the PC change during sequential execution?
- How do loops use the Program Counter?
- How does an if statement affect the PC?
- What happens to the PC during a function call?
Senior
- How does the CPU save and restore the PC during an interrupt?
- How is the PC used during context switching?
- Why must the PC be saved in the PCB?
- Explain the difference between ARM's PC behavior and x86's RIP.
- How does a debugger implement breakpoints using the Program Counter?
Common Mistakes
- ❌ Thinking the PC stores the current C source line. It stores a machine instruction address.
- ❌ Assuming the PC always increments by a fixed amount. Branches, jumps, calls, returns, exceptions, and interrupts can all change it.
- ❌ Believing loops are special CPU constructs. They are implemented using conditional and unconditional jumps.
- ❌ Forgetting that the OS must save the PC during a context switch.
Summary
The Program Counter is the CPU register that determines the flow of execution. Every instruction begins with the address in the PC, and every change in program flow—loops, branches, function calls, interrupts, and multitasking—is implemented by changing the Program Counter. This makes it one of the most critical registers in both CPU architecture and operating system design.
Mini Quiz
- Why does the CPU need a Program Counter?
- How does the PC behave during sequential execution?
- How is a for loop implemented using the PC?
- What happens to the PC during a function call?
- What happens to the PC when an interrupt occurs?
- Why does the OS save the PC during a context switch?
- How does a debugger use the Program Counter?
Concept Mastery Assessment — Module 04
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.