The CPU
Table of Contents
- The Brain That Isn't Very Smart
- What an Instruction Looks Like
- The Fetch–Decode–Execute Cycle
- Registers: The CPU's Hands
- Clock Speed
- Cores
- From Your Code to Machine Code
- CPU Architectures: x86 and ARM
- Try It Yourself
1. The Brain That Isn't Very Smart
The CPU (Central Processing Unit), or processor, is often called the "brain" of the computer. It's the part that actually runs instructions.
But it's a strange kind of brain. It doesn't understand your program, your language, or your intentions. It knows a fixed, fairly small set of basic operations — its instruction set — and it performs them one after another at incredible speed.
A modern CPU is a chip about the size of a postage stamp, containing billions of transistors — the tiny on/off switches from the last chapter — arranged into circuits that can add, compare, and move numbers.
2. What an Instruction Looks Like
CPU instructions are extremely simple. Typical examples:
- Load — copy a number from memory into the CPU
- Store — copy a number from the CPU back into memory
- Add / subtract / multiply — do arithmetic on two numbers
- Compare — check whether two numbers are equal, or which is bigger
- Jump — continue from a different instruction (optionally, only if a comparison was true)
That's roughly it. Every feature of every program — if statements,
loops, function calls, drawing a button — is built from combinations of
these.
For example, a line of code like:
total = price + tax;
turns into something like this sequence of CPU instructions:
LOAD register1, [price] ; copy price from memory into the CPU
LOAD register2, [tax] ; copy tax from memory into the CPU
ADD register1, register2 ; add them together
STORE [total], register1 ; copy the result back to memory
One line for you; four steps for the CPU. A "jump" instruction is what makes a loop possible — "go back to step 1 while the counter is less than 10".
3. The Fetch–Decode–Execute Cycle
The CPU spends its whole life repeating the same three steps:
- Fetch — get the next instruction from memory.
- Decode — work out what that instruction means ("this is an ADD").
- Execute — do it.
Then it moves on to the next instruction and repeats. A special register called the program counter keeps track of where the CPU is in the list — like a bookmark. A jump instruction simply changes the bookmark.
That's all a running program is from the CPU's point of view: a bookmark moving through a list of instructions, sometimes jumping around.
Modern CPUs cheat to go faster — they work on several instructions at once in a pipeline, and even guess which way an
ifwill go before they know. But the result is always as if the instructions ran one by one, in order.
4. Registers: The CPU's Hands
The CPU can't do arithmetic directly on data sitting in RAM. It first has to load values into registers — a small number of storage slots built right into the CPU itself.
Think of a chef: the RAM is the fridge, and the registers are the chef's hands. You can only chop what you're holding. A typical CPU has only a few dozen general-purpose registers, each holding a single number (usually 64 bits), but they are the fastest storage in the entire computer.
This is the first hint of an idea that runs through the whole series: data that is close to the CPU is fast, and data that is far away is slow. We'll explore it properly in the next chapter.
5. Clock Speed
The CPU works to the beat of an internal clock — an electronic pulse that ticks at a steady rate. Each tick is a chance for the CPU to take a step.
Clock speed is measured in hertz (ticks per second):
- 1 GHz (gigahertz) = 1 billion ticks per second
- A modern laptop CPU runs at roughly 2–5 GHz
So in the time it takes you to blink (about a third of a second), a 3 GHz CPU ticks about a billion times.
A higher GHz number is not the whole story, though. How much work gets done per tick matters just as much — newer CPUs do more per tick than older ones. A 3 GHz chip from today can easily beat a 4 GHz chip from ten years ago. That's why comparing CPUs by GHz alone is misleading.
6. Cores
For a long time, CPUs got faster mostly by raising the clock speed. But faster clocks produce more heat, and around the mid-2000s, chip makers hit a wall.
The solution was to put multiple CPUs on one chip. Each one is called a core, and each core can run its own stream of instructions independently.
- A 4-core CPU can genuinely do four things at the same moment.
- Phones commonly have 6–8 cores; laptops 4–16; servers 64 or more.
- Many phones and laptops mix performance cores (fast, power-hungry) and efficiency cores (slower, battery-friendly).
More cores only help if the work can be split up. Eight cores make it faster to run eight programs at once, or one program designed to split its work into pieces. A program that does its work one step at a time uses just one core, no matter how many you have. We'll come back to this in the chapter on threads.
7. From Your Code to Machine Code
The CPU only understands machine code — instructions stored as binary numbers. Nobody writes that by hand anymore. Instead, we write in programming languages like JavaScript, Python, Go, or C, and a tool translates them. There are two main approaches:
Compiled languages (C, C++, Go, Rust) — a compiler translates
your whole program into machine code before it runs, producing an
executable file (like an .exe on Windows). Running it is fast because
the translation is already done.
Interpreted languages (Python, and originally JavaScript) — an interpreter is itself a program that reads your code and carries it out while it runs. This is more flexible but historically slower.
In practice, the line is blurry. Modern JavaScript engines like V8 (used by Chrome and Node.js) use just-in-time (JIT) compilation: they start by interpreting your code, notice which parts run often, and compile those parts to machine code on the fly.
Either way, the end result is the same: machine code running on the CPU, one instruction at a time.
8. CPU Architectures: x86 and ARM
Different CPU families understand different instruction sets. The two you'll hear about constantly are:
| Architecture | Where you find it |
|---|---|
| x86-64 (also called x64 or AMD64) | Most Windows PCs, older Macs, most cloud servers |
| ARM64 (also called AArch64) | Phones, Apple Silicon Macs (M1–M4), Raspberry Pi, a growing number of servers |
Machine code for one doesn't run on the other. That's why software
downloads sometimes ask you to choose "Intel" or "Apple Silicon", and
why Docker images are built for a specific platform like linux/amd64
or linux/arm64. If you ever see an "exec format error", this mismatch
is usually the reason.
9. Try It Yourself
Find out what CPU your own computer has:
- Windows: open Task Manager (
Ctrl + Shift + Esc) → Performance → CPU. You'll see the model, the speed, and the number of cores. - macOS: Apple menu → About This Mac. For more detail, open Activity Monitor → Window → CPU History to see one graph per core.
- Linux: open a terminal and run
lscpu.
While the CPU view is open, play a video or open a heavy web page and watch the usage graph jump. Every spike is billions of fetch–decode–execute cycles.
Now you know what does the work. Next: where the data it works on actually lives.
Buy Me a Coffee
Found this tutorial helpful? Consider buying me a coffee to support more free content!