Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 55 ยท Inside the Machine

The CPU

Learn how a CPU works: the ALU, control unit and registers, the fetch-decode-execute cycle, pipelines, cores and threads, and what clock speed in GHz really measures.

The CPU (central processing unit, or simply the processor) is the part of a computer that actually runs programs. Every click, every video frame you decode and every line of code you execute eventually becomes a long stream of tiny steps carried out by a CPU: add these two numbers, compare these values, copy that byte, jump to another place in the program.

A modern CPU does billions of such steps per second, yet each step is simple. This lesson opens the box: the components inside, the loop that drives them, how designers make it faster, and why "more GHz" is not the same as "faster". It follows Hardware and Software, and a later lesson, Boolean Logic and Logic Gates, shows the circuits a CPU is built from.

What is inside a CPU

A CPU is one small chip, but it contains several cooperating parts.

  • ALU (arithmetic logic unit). The calculator. It adds, subtracts, compares and performs logical operations such as AND and OR on numbers.
  • Control unit. The conductor. It reads each instruction, works out what it means and tells the other parts what to do and when.
  • Registers. A handful of extremely fast storage slots inside the CPU, each holding one number (typically 64 bits on a modern desktop). The ALU works on values in registers, not directly on memory.
  • Program counter (PC). A special register holding the address of the next instruction.
  • Cache. Small, fast memory on the chip that keeps recently used data close, so the CPU waits less. See Memory: RAM, Cache and the Memory Hierarchy.
flowchart LR
  M["Main memory<br/>(RAM)"] <--> C["Cache"]
  C <--> CU["Control unit"]
  CU --> R["Registers"]
  R <--> A["ALU"]
  CU -.->|"controls"| A

The CPU gets instructions and data from memory through the cache; the control unit steers the registers and the ALU.

Registers are the fastest storage in the whole computer and also the scarcest: a CPU has only a few dozen of them. Everything else is slower, which is why so much of CPU design is about keeping the right values close.

The fetch-decode-execute cycle

A CPU repeats one loop for as long as it has power:

  1. Fetch. Read the instruction stored at the address in the program counter, then advance the program counter.
  2. Decode. The control unit works out which operation is requested and which values it needs.
  3. Execute. The ALU or another unit performs the operation. The result goes into a register, into memory, or into the program counter if the instruction is a jump.

Then the loop begins again. Nothing more magical than that runs your programs: an enormous number of these small cycles. Instructions are stored in memory as numbers (see Machine Code and Assembly for how they look).

The stand-in below is a toy CPU with two registers and four instructions. The real thing is vastly more complex, but the shape of the loop is the same. Each pass through the while loop fetches one instruction, advances the program counter, and executes it.

JS A fetch-decode-execute loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output step by step: the two LOAD instructions fill the registers, ADD makes the ALU compute 7 + 5, and PRINT shows 12. In a real processor the instructions are binary patterns and the registers are circuits, but the logic is identical. You will build a fuller version in Project: A Tiny CPU Simulator.

Clock speed and what GHz does and does not mean

The CPU is driven by a clock, a signal that ticks at a steady rate. Every tick is one cycle, and each stage of the work happens on a tick. The rate is measured in hertz: one hertz is one tick per second, so a 3 GHz (gigahertz) CPU ticks three billion times per second.

It is tempting to rank processors by GHz alone, but clock speed is only one factor. What matters is how much work is done in a given time:

Text
time = (instructions x cycles per instruction) / clock rate

A chip that needs fewer cycles per instruction, or that does several instructions per cycle, can beat a chip with a higher clock. Different CPU families also differ in how much one instruction accomplishes. In the TryIt below, Chip A has the larger number on the box, but Chip B finishes first.

JS Why GHz alone is misleading

Press Run (or Ctrl + Enter) to see the console output here.

No output.

There are practical limits too. Faster clocks mean more heat and more power. That is why laptops and phones lower the clock to save battery and stay cool, and why desktop CPUs "boost" their clock only for short periods. Memory speed, cache size and storage often decide how fast a task feels, not the clock (see Troubleshooting, Performance and Upgrades).

Making a CPU faster

Since raw clock speed hit a heat limit around the mid-2000s, designers use other tricks.

Pipelining

A single instruction needs several stages (fetch, decode, execute, write back). Without a pipeline, the CPU finishes all stages of one instruction before starting the next, leaving most of its hardware idle. With a pipeline, it works like a laundry with a washer and a dryer: while the first instruction is being executed, the next one is already being decoded and a third is being fetched. Each instruction still takes the same number of stages, but one instruction completes each cycle once the pipeline is full.

Cores and threads

A core is a complete processing unit with its own registers and ALUs. A chip with 8 cores can run 8 instruction streams at the same time. Some cores support hardware threads (marketed as hyper-threading or SMT): the core keeps two sets of registers and switches to the other thread when the first one stalls, waiting for memory. Two hardware threads share one core, so they are not worth as much as two real cores.

More cores only help when the work can be divided. A program with a long sequential part cannot run faster than that part allows, a limit known as Amdahl's law: if a fraction p of the work can run in parallel, the best speedup with n cores is 1 / ((1 - p) + p / n).

JS Pipelines and cores: what they can and cannot give

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first table shows that a pipeline helps nothing for a single instruction (5 cycles either way), but for 100 instructions it needs 104 cycles instead of 500. The second shows diminishing returns: with 64 cores, a job that is half sequential is less than twice as fast.

Other ideas

  • Caches keep frequently used data on the chip.
  • Branch prediction guesses which way an if will go so the pipeline does not stall.
  • Specialized units: vector instructions that process many numbers at once, and separate processors such as the GPU for graphics and parallel work (see Motherboard, Buses, GPU and Power).

Architectures and word size

The set of instructions a CPU understands is its instruction set architecture (ISA). The two families you will meet most are x86-64 (most PCs and servers) and ARM (phones, tablets, recent Macs and many servers). A program built for one ISA cannot run on the other without translation, even if the operating system is the same.

You will also see "64-bit" or "32-bit". This is the typical width of the registers and addresses. A 64-bit CPU can address vastly more memory than a 32-bit one, which was limited to about 4 GiB.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which part of the CPU performs arithmetic such as addition and comparison?

    The ALU (arithmetic logic unit) does the calculations. The control unit decodes instructions and directs the other parts.
  2. 2. What does the program counter hold?

    The program counter points at the next instruction. The CPU fetches from that address and then advances it, unless a jump changes it.
  3. 3. Chip X runs at 5 GHz but needs twice as many cycles per instruction as Chip Y at 3 GHz. Which statement is correct?

    Time depends on instructions x cycles per instruction / clock rate. X: 2 / 5 = 0.4 per instruction unit; Y: 1 / 3 = 0.33, so Y is faster.
  4. 4. What is the main benefit of pipelining?

    A pipeline does not shorten one instruction, but it starts the next one before the previous finishes, raising throughput.
  5. 5. Why does adding more cores not make every program faster?

    Sequential parts still run on one core, so the speedup is limited by them (Amdahl's law).

Summary

  • A CPU contains an ALU for calculations, a control unit that directs the work, registers for the fastest storage, and caches.
  • It repeats the fetch-decode-execute cycle, guided by the program counter.
  • Clock speed (GHz) counts ticks per second; real performance also depends on cycles per instruction, design, caches and memory.
  • Pipelines overlap instruction stages; multiple cores run several instruction streams at once, but only parallel work benefits.
  • The instruction set (x86-64, ARM) decides which programs a CPU can run directly.

Next, look at where the CPU gets its data from in Memory: RAM, Cache and the Memory Hierarchy.