Home / Software y Cloud / Processors from the inside: pipelining, cache and how the CPU runs your code

Processors from the inside: pipelining, cache and how the CPU runs your code

When you run a program, your computer is not doing one thing at a time: it breaks every instruction down into a meticulous assembly line that processes millions of operations per second. Understanding the microprocessor from the inside explains why today’s phone is faster than a 1990s supercomputer.

From the clock to the instruction cycle

Everything starts with the system clock, a quartz crystal that vibrates at a fixed frequency and sets the rhythm at which each step runs. That frequency, which you see in MHz or GHz, counts clock cycles per second. Each CPU instruction is carried out as an instruction cycle with four classic phases:

  • Fetch: the processor reads the instruction from memory.
  • Decode: it figures out which operation is being requested.
  • Execute: the arithmetic logic unit (ALU) does the actual computation.
  • Write-back: it stores the result in a register or in memory.

Pipeline: the chip’s assembly line

Doing these four phases one after another would be painfully slow. Modern processors use a pipeline: while one instruction is being executed, the next is already being decoded and the one after that is being fetched. It is like an assembly line where several pieces move forward at once through different stations.

Today’s chips reach pipelines of 14 to 20 stages, and they are also superscalar: they have several parallel execution units, so they can issue multiple instructions per clock cycle.

The branch problem: prediction and speculative execution

There is one enemy of the pipeline: conditional branches (an if in your code). When the CPU reaches a branch it does not know whether it will take path A or B, and an empty pipeline wastes dozens of cycles. The solution is branch prediction: the processor learns from previous runs and guesses the most likely path while it moves forward.

At a deeper level, speculative execution even executes instructions along the guessed path before it is sure. If it was right, it saves time; if it was wrong, it discards the results. That same technique gave rise to the famous Spectre and Meltdown attacks in 2018, which exploited speculation to snoop on data.

The memory hierarchy: caches and registers

RAM is fast, but the processor is much faster: going to RAM can cost hundreds of clock cycles. To avoid that, the CPU uses a memory hierarchy in which speed and size trade off:

  • Registers: the fastest and smallest memory, embedded in the core itself.
  • L1 cache: a few KB per core, with a latency of 2–4 cycles.
  • L2 cache: hundreds of KB, somewhat slower.
  • L3 cache: several MB, shared across cores.
  • RAM: gigabytes, but hundreds of cycles away.

The cache hits the vast majority of the time thanks to locality: if your program used one memory address, it will probably use its neighbours (spatial locality) and the same one again (temporal locality).

Multiple cores and real parallelism

Raising the frequency hit a physical wall of heat and power. That is why the path to more performance went through putting several cores on the same chip, able to run threads truly simultaneously. Techniques such as Hyper-Threading (simultaneous multithreading) double the logical threads per core by reusing units that would otherwise sit idle.

This means talking about cache coherence mechanisms such as the MESI protocol, which keeps copies of a piece of data spread across the different levels and cores in sync so nobody reads a stale value.

From the chip to assembly

All this design is embodied in the instruction set architecture (ISA): the contract between software and hardware. Architectures such as x86 (Intel and AMD) or Arm define which basic operations the processor understands, and they are the point where compiled code meets the silicon.

The next time a program responds instantly, think of the invisible choreography: billions of instructions marching through superscalar pipelines, with predictions that almost always hit and a network of caches that barely lets a single piece of data slip away.