L1 · HardwareComputer Science 101 · part 1 of 2

How a CPU Actually Runs Your Code

Your C or Go program is just numbers in memory, and the CPU reads them one instruction at a time. Here's the loop that does it.

Takumi Yamada
· 5 min read
cpuarchitecturefundamentals

You write x := a + b. The (Central Processing Unit) never sees that line.

By the time your program runs, that line has become a few numbers in memory. The processor reads them with a short loop that repeats over and over. This post explains that loop, step by step.

The CPU is a loop

A CPU repeats three steps: fetch, decode and execute. It does this billions of times per second.

loop:
    instruction = memory[PC]   # fetch
    op, operands = decode(instruction)
    execute(op, operands)      # do the work
    PC = PC + size(instruction) # advance
    goto loop

PC is the program counter. It's a small storage slot inside the CPU that holds the address of the next instruction. Everything else follows from that one number.

MEMORYinstruction Ainstruction Binstruction Cinstruction DPCread memory[PC]FETCHwhich operation?DECODEdo the workEXECUTEPC moves on to the next instruction
One turn of the loop. The program counter decides which instruction comes next.

The CPU doesn't know about the functions or loops in your source code. It only keeps reading whichever instruction the program counter points at. A jump instruction, which is how loops and if statements work, does nothing more than write a new address into the program counter.

Registers: the CPU's hands

A CPU can't do arithmetic directly on values in memory. It first copies them into registers, which are a few small storage slots built into the chip itself.

On x86-64, the kind of processor in most desktop PCs, there are sixteen general purpose registers of 64 each, plus special ones such as the program counter. That's all of them. Together they hold a few hundred , and they are the fastest storage in the machine. Reading one takes less than one .

That's why instructions follow a pattern. First, load a value from memory into a register. Then do the arithmetic between registers. Then store the result back into memory.

MEMORYlarge, and slowerINSIDE THE CHIPAXBXCX…R15sixteen of them, 64 bits each1 load3 store2 operateALUadds, compares
The three things an instruction can do. The ALU only works on values that are already in registers.

The clock

A clock signal sets the pace of everything. It's a signal that switches between 0 and 1 at a steady rate, for example 3.5 billion times per second, which is 3.5 GHz. On each tick, the machine moves forward one step.

A small oval metal case soldered to a green circuit board, stamped 25.000.
The timing of the clock starts at a quartz crystal like this one, which vibrates 25 million times per second. Circuits on the board and inside the processor multiply that rate up to 3.5 billion ticks per second.Photo: Slick, CC0 1.0, via Wikimedia Commons (original). Changes: resized.

At 3.5 GHz, one tick, or cycle, lasts about 0.29 nanoseconds. A simple addition finishes in one cycle. Reading a value that isn't in any , so it has to come from (Random Access Memory), can take about 100 nanoseconds. That's 350 cycles in which the CPU waits for memory.

AN ADD1 cycle, about 0.29 nsA LOAD THAT MISSES EVERY CACHEabout 100 ns, which is 350 cycles050100150200250300350clock cycles
Both drawn to the same scale: one cycle for the add, 350 for the load from RAM.

That difference, 1 cycle against 350, explains most of what makes code fast or slow.

From your code to instructions

Here is a real Go function.

func add(a, b int) int {
    return a + b
}

The Go compiler turns it into machine code for amd64, which is another name for x86-64. If we disassemble it, which means turning the machine code back into readable instruction names, we get roughly this.

add:
    MOVQ a+0(FP), AX   ; load arg a into register AX
    ADDQ b+8(FP), AX   ; add arg b to AX
    MOVQ AX, ret+16(FP); store AX as return value
    RET                ; pop return address into PC

That's four instructions. The CPU fetches each one, decodes it, executes it, and moves on. The addition itself happens in the ALU (Arithmetic Logic Unit), the part of the CPU that does arithmetic.

INSTRUCTIONSTACK SLOTREGISTER AXMOVQ a+0(FP), AXaaloadADDQ b+8(FP), AXba + baddMOVQ AX, ret+16(FP)reta + bstoreRETreturn addrPCjump back to the caller
What each of the four instructions moves. Values go from memory into a register, are worked on there, and go back.

RET is the interesting one. When a function is called, the address to come back to is saved on the . RET loads that address into the program counter, so the CPU continues right after the place where the function was called. Calling and returning are just changes to the program counter.

C gives the same result. The same function written in C also becomes a few moves and a return. Programming languages exist for people. The CPU only understands these numbered instructions.

Pipelining: the assembly line

Suppose the CPU did fetch, decode and execute strictly one instruction at a time. While the worked, the fetch hardware would wait with nothing to do, and the other way around.

So modern CPUs pipeline the work. While one instruction executes, the next one is being decoded, and the one after that is being fetched. It works like an assembly line in a car factory, where each station works on a different car at the same time.

cycle 1cycle 2cycle 3cycle 4cycle 5INSTR AFETCHDECODEEXECUTEINSTR BFETCHDECODEEXECUTEINSTR CFETCHDECODEEXECUTEfrom cycle 3 on, one instruction finishes every cycle
Three instructions, each one stage behind the one before.

Once the pipeline is full, the CPU finishes close to one instruction every cycle, even though each instruction takes several cycles from start to finish.

Real chips go further. A superscalar CPU has several ALUs, so it can finish several instructions per cycle. An out-of-order CPU changes the order of instructions that don't depend on each other, to keep all those ALUs busy.

Pipelines have one problem, and that's branches. When the CPU reaches an if, it doesn't know yet which way the program will go. But stopping the pipeline to wait would waste time. So the CPU predicts which way the branch will go, and starts working on that path before it knows.

If the guess is right, nothing is lost. If the guess is wrong, the CPU throws away the work it started, which costs fifteen or more cycles.

RIGHT GUESSnothing lostifABCDEWRONG GUESSpipeline flushedifABCXYstarted on a guess, then discardedthe other pathtime
The instructions after a branch, when the prediction is right and when it is wrong.

That's why code with many unpredictable if statements is slow. It's also why sorting data before looping over it can make the loop faster: the branches become predictable, and the pipeline stays full.

Putting it together

Here are all the steps for x := a + b.

  1. The compiler turns your source code into when you build the program. It decides which variables go in and which go on the stack.
  2. When the program runs, the program counter points at the bytes of the add instruction in memory.
  3. Fetch reads those bytes, and decode works out that this is an addition.
  4. The ALU adds the two registers in a single clock tick.
  5. The result is stored and the counter moves on. By then, the pipeline has already fetched and decoded the next few instructions.

That's everything that happens. A clock ticks, a counter goes up, and an ALU adds. It repeats so fast that the addition seems instant.

The Bottom Line when you write Go

Once you see the CPU as this loop, common performance advice starts to make sense.

Loops over slices are fast. A keeps its elements next to each other in memory. The CPU notices the pattern and loads the next values before they are needed, and the pipeline stays full.

Map lookups and following pointers are slower. Each step to a new place in memory might miss the cache, and every miss is one of those 350-cycle waits.

IN ORDERa slice, an arrayone cache line, loaded by the first readBY POINTERa list, a treeeach hop lands somewhere else, so each one can miss the cache
Memory read in order, and by following pointers. Each miss in the second row is one of the 350 cycle stalls.

Unpredictable if statements are slow, because every wrong guess empties the pipeline.

You don't need to write assembly. But knowing that your for loop becomes a few instructions moving through a pipeline tells you why the profiler points where it does.

Check yourself

3 questions · nothing is saved
  1. 1.What does a jump instruction, the thing loops and if statements are made of, actually do?
  2. 2.At 3.5 GHz, roughly how many cycles does the CPU wait when a value has to come from RAM instead of a cache?
  3. 3.Why can sorting data before looping over it make the loop faster?

Who wrote this

Author profile and background

Takumi Yamada

Takumi Yamada

· Backend Engineer

Backend engineer building services, databases, Linux tooling, and NEGEN SYSTEMS. Writing about low-level mechanics, distributed systems, and real-world security.

End of post
NEGEN SYSTEMS
Receipt · post 1 of 31
Written
Oct 16, 2025
Length
1,136 words · 5 min
Built
…
Served by
…
Delivered
…
Over the wire
…
Go's share
…
Next in Computer Science 101 · part 2 of 2

Memory: The Stack, the Heap, and Pointers

Where your variables actually live, why the heap costs more than the stack, and what a pointer really is.

5 min read