The Plain Picture#
Imagine a very fast but very literal cook. The cook:
has a tiny countertop with room for only a few ingredients at once,
reads one recipe step at a time ("take ingredient from shelf 7", "add these two", "if the dough is too thick, jump to step 12"),
does exactly what the step says, then moves to the next step,
never gets creative. If the recipe is wrong, the cook will make the wrong dish perfectly.
The cook is fast because each step is tiny, and the cook does billions of them per second.
The Real Name#
The cook is the CPU (Central Processing Unit), also called the processor.
The recipe steps are instructions. A list of instructions is a program.
The countertop is a set of registers: a few dozen super-fast storage slots inside the CPU, each holding 64 bits on a modern machine.
The set of instructions a CPU understands is its instruction set. The two you'll hear about:
x86-64 β Intel and AMD chips (most Windows PCs, most cloud servers).
ARM β phones, Apple M-series Macs, and more and more cloud servers (e.g. AWS Graviton).
A program built for one does not run directly on the other. You'll meet this again with Docker images in DevOps (coming soon).
Under the Hood#
The fetchβdecodeβexecute cycle#
Everything a CPU does is this loop, repeated forever:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β
βΌ β
1. FETCH read the next instruction from memory β
β β
βΌ β
2. DECODE work out what it asks for β
β ("add register 1 and register 2") β
βΌ β
3. EXECUTE do it (add / compare / move / jump) β
β β
βββββββββ move to the next instruction βββββββββThe CPU keeps a special register called the program counter that holds the memory position of the
next instruction. Normally it just moves forward by one instruction. A jump instruction changes
it β that is how if statements and loops work at the lowest level.
What instructions look like#
Real instructions are tiny. Adding a bonus of 50 points to our score of 200 might look like this in assembly language (a human-readable way to write CPU instructions):
mov rax, 200 ; put 200 into register rax
add rax, 50 ; rax = rax + 50 β 250
mov [score], rax ; copy rax out to the memory slot named "score"Each line becomes a few bytes of machine code β the actual bits the CPU reads.
Clock speed#
The CPU has a clock: an electronic pulse that ticks at a steady rate, and each tick lets the CPU move forward. A 3 GHz CPU ticks 3 billion times per second (1 GHz = 1 billion ticks per second). One tick lasts about a third of a nanosecond (a nanosecond, ns, is one billionth of a second).
Modern CPUs finish several simple instructions per tick using tricks like working on the next
instruction before the current one is done (pipelining) and guessing which way an if will go
(branch prediction).
Cores#
One cook can only do one thing at a time. So modern CPUs contain several complete cooks on one chip. Each is a core. A laptop with 8 cores can truly run 8 streams of instructions at the same moment.
This is the reason concurrency matters, which you'll study in Operating Systems (threads) and again in Python (Python Concurrency (coming soon)).
Gotchas
A faster clock is not automatically faster. Clock speeds stopped rising around 2005 (around 3β5 GHz) because faster chips got too hot. Since then, performance has come from more cores and better memory use (next chapter). So a program that only uses one core can't benefit from most of a modern CPU.
The CPU only understands its own machine code. Everything else β Java, Python, JavaScript β must be turned into machine code somehow (Chapter 8).
Check yourself
4 questionsName the three steps the CPU repeats forever.
Show answerHide answer
Fetch, decode, execute.
What is a register, in the cook analogy?
Show answerHide answer
The small countertop β a few super-fast storage slots inside the CPU.
What does 3 GHz mean?
Show answerHide answer
The clock ticks 3 billion times per second.
Why did CPUs start adding more cores instead of getting faster clocks?
Show answerHide answer
Faster clocks produced too much heat, so extra speed had to come from doing work in parallel.