How a CPU Works: The Program Counter, Control Unit and ALU

Diagram of a CPU with the program counter, control unit and ALU running the fetch, decode, execute loop

How a CPU works comes down to one loop. The processor fetches an instruction, decodes it, executes it, saves the result, and then does it again. Every tap, click and frame on your screen is that loop running billions of times per second.

Three parts run the show: the program counter, the control unit and the ALU. This guide walks through each one and traces a single calculator sum through the chip. It also shows why the biggest number on a spec sheet doesn't tell you how fast a processor really is.

TL;DR
  • A CPU is a general-purpose chip: you change what it does by changing software, not hardware.
  • It runs one loop nonstop: fetch, decode, execute, write back, repeat.
  • The program counter says where the next instruction is, the control unit turns it into signals, and the ALU does the math.
  • Registers are tiny, ultra-fast storage slots that keep results close for the next instruction.
  • Clock speed (GHz) and core count alone don't decide real-world speed. Per-clock work and cache matter too.
  1. 0:00Intro
  2. 0:24One chip, any program
  3. 1:08Meet the CPU
  4. 1:48The loop behind everything
  5. 2:26Step 1: Fetch
  6. 3:09Step 2: Decode
  7. 3:49Step 3: Execute
  8. 4:30Watch one instruction run
  9. 5:07If it computes, it's inside
  10. 5:41More GHz isn't always faster
  11. 6:23The loop in five lines
  12. 6:46Fetch, decode, execute, repeat

Why build a general-purpose CPU at all?

Start with the alternative. A hardwired chip does one fixed task. It may do that task well, but it can't learn anything new. Want it to do a different job? You need different hardware, which means designing and building a whole new chip.

A general-purpose CPU flips that around. It runs whatever program you load. The same chip becomes your camera, your maps app or your game, depending on the software running on it. You change what it does by swapping software, not silicon.

That's why new features can ship as code updates overnight instead of waiting years for a new chip. Every app you install runs on the same processor. Without a general-purpose CPU, each one would need its own chip. Flexibility is the whole point.

  • Hardwired chip: one fixed task. A new job means new hardware.
  • General-purpose CPU: runs any program. A new job means new software.
  • Your phone swaps apps without swapping hardware.

What is a CPU, and what's inside it?

CPU stands for central processing unit. Central, because it sits at the heart of the machine. Processing, because it works through instructions. Unit, because it's one self-contained block of logic. Its job is narrow and relentless: execute an instruction, grab the next one, repeat. It does this in order and without a break.

Four key parts share that work, and each has a distinct role. None of them is useful alone. The control unit can't do math. The ALU doesn't know what to do next. The program counter only knows where to look. Together they form the engine every program depends on.

  • Control unit: directs every step.
  • Program counter (PC): tracks where the next instruction is.
  • ALU: does the math and logic.
  • Registers: hold the values the CPU is working on right now.

What is the fetch-decode-execute cycle?

Those parts don't act at random. They run one tight loop, over and over, called the instruction cycle. It has three main steps plus a final step that saves the result. Fetch grabs the next instruction. Decode works out what it means. Execute does the work. Write back stores the result in a register. Then the loop starts again.

A clock drives the whole thing. It ticks billions of times per second, and every tick nudges the loop forward. That's where the famous "billions of cycles per second" comes from. The clock doesn't do any computing itself. It sets the pace for everything else.

Here's the payoff: every program boils down to this same loop. That includes your browser, your games and even the operating system itself. Learn the loop once, and you understand how all of them run at the hardware level.

  • Fetch → Decode → Execute → Write back → repeat
  • Each clock tick pushes the loop forward.

Fetch: What does the program counter do?

Before the CPU can do anything, it needs to know what to do next. The program counter, or PC, holds the address of the next instruction. It sends that address to memory, where the program lives. Memory hands the instruction back, and it lands in the instruction register, ready to be decoded.

A detail people often miss: the PC stores where, not what. It doesn't hold the instruction itself, only its location. Think of it as a bookmark, not the page. After each fetch, it steps forward to the next instruction automatically, so the program runs in order.

Then there are jumps. An if-statement or a loop in your code simply rewrites the PC to point somewhere else. Instead of moving to the next instruction in line, the CPU continues from a new address. That one move is how code makes decisions.

Decode: How the control unit turns bits into signals

Once it has been fetched, the instruction is just bits. Step two, decode, gives those bits meaning, and this is where the control unit earns its name. Take the instruction ADD R3, R1, R2. It means: add the values in registers R1 and R2, and put the answer in R3.

The control unit splits the instruction into fields: an opcode (add), a destination (R3) and two sources (R1 and R2). From those fields it produces signals. The signals tell the ALU to add, read R1 and R2, and write the result to R3. Signals are electrical on-off lines that open the right paths so the right values reach the ALU.

Think of the control unit as the chip's traffic cop. It doesn't do the math. It decides which data moves, where it goes and when. Decode is where meaning turns into action.

  • Instruction: ADD R3, R1, R2
  • Fields: opcode, destination, source 1, source 2
  • Signals: ALU add, read R1, read R2, write R3

Execute: What do the ALU and registers do?

With the signals set, step three is execute, where the actual work happens. The registers send out the values in R1 and R2. The ALU adds them. Register R3 stores the result. Then it's on to the next fetch, and the loop repeats.

ALU stands for arithmetic logic unit. It adds, subtracts, compares two numbers and combines bits with operations like AND and OR. These are simple operations, but they're the raw material for everything else. Every calculation your apps make ends up passing through the ALU.

Registers are tiny, ultra-fast storage slots right inside the CPU, far quicker than main memory. Keeping results there means they stay close, ready for the very next instruction. The CPU doesn't have to make a slower trip out to memory.

  • Registers → ALU → Register R3 → next fetch

Watch one instruction run: tracing 2 + 3

Make it concrete. You open a calculator app and tap 2 + 3 =. It feels instant, but underneath, the CPU is working through the exact loop described above. Here's that single addition, traced step by step from the program counter to the result sitting in a register:

Then the program counter moves on. More instructions carry that 5 to your screen so it can be drawn. One visible answer on a calculator takes many trips around the loop, not just one.

Now scale that up. A whole app is this same pattern, repeated relentlessly, billions of times. Every tap, click and frame you see works this way.

  • Fetch: the program counter points to ADD R3, R1, R2.
  • Decode: the control unit sets the ALU to add.
  • Execute: the ALU computes 2 + 3 = 5.
  • Write back: R3 now holds 5.

Where are CPUs used?

Pretty much everywhere. Some places are obvious: phones, laptops and the servers that run the cloud. Others are easy to forget, like cars, or the microwave counting down your popcorn. If it computes, there's a CPU inside.

The loop is the same in every one. A server CPU and a microwave chip both fetch, decode and execute. What changes is the budget. A phone chip sips battery, while a server chip draws far more power. Size and scale shift from device to device, but the loop stays the same.

  • Phones
  • Laptops
  • Servers
  • Cars
  • Microwaves

Is more GHz always faster? Common CPU mistakes

Understanding the loop protects you from the two biggest mistakes people make when comparing processors. Speed isn't one number on a spec sheet. It comes from how well the whole chip keeps the fetch-decode-execute loop running.

Mistake one: chasing GHz alone. GHz is just tick speed, meaning how often the clock nudges the loop forward. It ignores per-clock work, which is how much a chip finishes on each tick. A chip with a slower clock that does more per tick can come out ahead.

Mistake two: counting cores. Extra cores help when software splits its work across them, but more cores don't make everything faster. A task that runs on a single thread only uses one core, and the other cores sit idle for that job.

The overlooked factor is cache. Fetch stalls when memory is slow, and the whole loop waits. Cache is fast memory close to the CPU that keeps instructions flowing. The lesson: judge the whole chip, not one number.

  • GHz measures tick speed, not work done per tick.
  • Single-threaded tasks use only one core.
  • Cache keeps the loop fed.

The bottom line

  • The CPU executes instructions nonstop, in order.
  • The program counter points to the next instruction. It's a bookmark, not the page.
  • The control unit decodes each instruction into signals that steer data.
  • The ALU does the math, and registers store the results.
  • GHz and core count aren't everything. Cache and per-clock work count too.

Quick-fire questions

What are the main parts of a CPU?

The four key parts are the control unit, the program counter, the ALU and the registers. The control unit directs each step, and the program counter tracks the next instruction. The ALU does the math, and the registers hold the values currently in use.

What is the fetch-decode-execute cycle?

It's the loop every CPU runs. The CPU fetches the next instruction from memory, decodes it into signals, and executes it in the ALU. It then writes the result back to a register and repeats, with each clock tick pushing the loop forward.

What does the program counter do?

The program counter holds the address of the next instruction, not the instruction itself. After each fetch it moves forward automatically. If-statements and loops work by rewriting it to point somewhere else.

What's the difference between the control unit and the ALU?

The control unit is the traffic cop. It decides which data moves, where it goes and when, but it doesn't do the math. The ALU does the actual work: adding, subtracting, comparing and combining bits with AND and OR.

Does a higher GHz CPU always mean better performance?

No. GHz only measures how fast the clock ticks, not how much work the chip finishes per tick. Per-clock work, cache and whether your software can use multiple cores all affect real-world speed.

Why do CPUs use registers instead of main memory?

Registers are tiny storage slots inside the CPU and far faster than main memory. Keeping working values there means results are ready for the very next instruction without a slower trip out to memory.

Watch the full video on YouTube →

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
Ad