How the Program Counter Controls Execution Flow

02 Mins

Most programs do not execute strictly line by line forever.

They repeat code, skip instructions, call functions, and make decisions.
At the hardware level, all of this is controlled by a tiny CPU register called the Program Counter (PC).

The Program Counter determines which instruction executes next, making it one of the most important components

program-counter-sequential-flow


What is the Program Counter?

The Program Counter (PC) is a special-purpose register inside the CPU that stores the memory address of the next instruction to be fetched.

For example: PC = 200

This means the CPU will fetch the instruction stored at memory address 200.

During the instruction cycle:

  1. The CPU reads the instruction from memory.
  2. The instruction is decoded and executed.
  3. The Program Counter is updated.

The PC continuously changes while a program runs.


Default Sequential Execution (PC + 1)

Normally, programs execute sequentially. This means after executing one instruction, the CPU automatically moves to the next instruction in memory.

This automatic movement is often represented as: PC=PC+1

Note - Actual increment size depends on instruction length and architecture.

How the CPU Executes Instructions Sequentially

program-counter-seuqntial-example

Consider the following instructions stored in memory:

100: LOAD R1, [200]
101: ADD R1, R2
102: SUB R3, R4
103: MUL R5, R6

Execution will proceed like -

StepCurrent PCExecuted InstructionNext PC
1100LOAD R1, [200]101
2101ADD R1, R2102
3102SUB R3, R4103
4103MUL R5, R6104

Non Sequential

program-counter-non-sequential

JUMP Instructions

A JUMP instruction changes the Program Counter to a completely new address. This allows programs to skip instructions or move to different parts of memory instantly.

Conditional Branching

Conditional branches allow the CPU to make decisions. These instructions modify the Program Counter only if a condition is true.

CALL Instruction

Functions require the CPU to temporarily jump to another part of memory and later return back. This is handled using the CALL instruction.

When executed:

  • The CPU saves the return address.
  • The PC jumps to the function address.
  • The function executes.
  • A RET instruction restores the previous PC value.

Conclusion

Modern CPUs contain advanced optimizations, for example jumps and branches can disrupt instruction pipelines, so processors use techniques such as: branch prediction, pipeline flushing etc

program-counter-use-case

The Program Counter is far more than a simple register. By controlling the next instruction address, it enables every major programming construct - loops, conditions, branches, functions, and recursion.