Skip to content

Computer Architecture

What does the program counter do in a CPU?

The program counter (PC) is the register in a processor that holds the address of the next instruction. Each time an instruction is fetched, the PC moves on by one. To jump, the processor loads a new address into it instead. Counting gives you a program that runs in order; loading gives you loops, branches and function calls.

A counter with a load input

At heart the PC is a synchronous counter. Each tick, each bit's flip-flop takes its next value. But in front of every D input sits a chooser, controlled by a LOAD signal:

D = (IN · LOAD) + (next count · NOT LOAD)

With LOAD at 0, the counter's own next value gets through, and the PC counts on: 0, 1, 2, 3. With LOAD at 1, the address on its IN lines gets through instead, and on the next tick the PC jumps there. One gate opens while the other closes, so exactly one of them reaches each D.

Live experiment

Count on, or jump

A program counter running a four-step routine: wave, hop, spin, nap. With LOAD at 0 each tick counts on; with LOAD at 1 it jumps to the address on the switches. Make it jump from 2 back to 0 and the routine loops without ever reaching the nap.

Try it yourself: free lesson

“Jump” is lesson 4 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.

Jumps make loops

Put a program at addresses 0, 1 and 2, and make the instruction at 2 a JUMP 0. The PC counts 0, 1, 2, then loads 0, and round it goes for ever. Every for and while loop you write compiles down to a jump like this at its end.

A branch is a jump with a condition. The instruction at 2 might say: jump back to 1 if the last result wasn't zero. The ALU's zero flag decides whether LOAD is 1. That's a loop that counts down and stops: an if and a loop, made from one counter, one flag and a gate.

Reset: where it all begins

Flip-flops wake up at power-on holding random values, so a PC could start anywhere and run nonsense. Every processor has a reset that forces the PC to a known address, its reset vector, usually 0 or a fixed address near the top of memory. On an Arduino, pressing the reset button sends the PC back to its reset address, and a moment later your sketch starts again from the top, counting, jumping and branching its way through every line.

How far it counts

A PC is as wide as the address it needs. An 8-bit PC reaches 256 instructions, a 16-bit one 65,536. The 8-bit AVR in an Arduino Uno has a PC wide enough for its 32 KB of program memory, while desktop processors use 64-bit addresses. In many processors the PC also steps by more than one: by 2, 4 or more bytes, the length of each instruction in memory.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Jump” is lesson 4 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3In a program, JUMP 1 at address 2 means…
    Show the answer

    Answer: The counter is loaded with 1 instead of counting to 3. The next instruction comes from address 1, not 3. That's how loops go round.

    • Why not “Count twice”? The counter doesn't count twice: it skips the usual next address, 3, and loads 1 instead.
    • Why not “Add 1”? JUMP doesn't do arithmetic on the data; the 1 is the address to go to next.
    • Why not “Stop”? A jump doesn't stop the program. It changes where the next instruction comes from.
  2. Question 2 of 3A program at addresses 0, 1, 2 ends with JUMP 0. It…
    Show the answer

    Answer: Runs 0, 1, 2 over and over: a loop. Jumping back to the start makes a loop. Every for and while loop in your code ends with a jump like this.

    • Why not “Stops”? Nothing in the program stops it. JUMP 0 sends the counter back to the start every time.
    • Why not “Skips to 3”? Address 3 is never reached: the jump at address 2 always loads 0 instead.
    • Why not “Runs once”? It would run once only if the last instruction let the counter carry on. JUMP 0 sends it round again.
  3. Question 3 of 3Why does a processor need a reset at power-up?
    Show the answer

    Answer: Flip-flops wake up in random states, so it must start from a known one. Without a reset, the program counter could start anywhere and run nonsense. Reset puts every register at a known value first.

    • Why not “To save power”? A reset isn't about power. It gives every register a known starting value.
    • Why not “To count faster”? A reset doesn't change speed. It makes sure counting starts from the right place.
    • Why not “It doesn't”? Flip-flops wake up holding random values, so without a reset the program counter could start anywhere.
Keep practising: free lesson

“Jump” is lesson 4 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.

Questions people ask

What is the program counter?

A register in the processor that holds the memory address of the next instruction to run. After each instruction is fetched it moves on to the next address, unless a jump or branch loads a different one.

Is the program counter the same as the instruction pointer?

Yes. Intel's x86 processors call it the instruction pointer (IP, EIP or RIP), and ARM and most textbooks call it the program counter. It does the same job in every processor.

What is the difference between a jump and a branch?

A jump always loads a new address into the program counter. A branch loads it only if a condition holds, such as the last result being zero, and otherwise lets the counter carry on. Branches are how if-statements and loops end.

Every topic