5 / 6 · Intro
What a CPU does
A 3 GHz processor ticks 3 billion times a second, one tick every third of a nanosecond.
Jump to the 10 practice questionsYou will learn
- Follow fetch, decode and execute step by step
- Work out how long a clock tick is
Before this: How gates add numbers
1Explain
Three students play the CPU
Picture a computer class. (This is a made-up scene.) Sixteen numbered boxes are drawn on the board, each with eight bits written in it. Three students play the CPU. One holds a card marked PC, one a card marked IR, one a card marked ACC. The PC card says 0. The IR student walks to box 0 and copies what is there, 0001 1110, and the PC student changes the card to 1. The class looks up the first four bits in a table on the wall: 0001 means LOAD. The last four, 1110, are 14.
2Explain
Twelve moves
Box 1 says ADD 15, so ACC becomes 7 + 5 = 12. Box 2 says STORE 13, and the ACC student writes 12 into box 13. Box 3 says HALT, and everyone sits down. Twelve moves in all. None of the three needed to know that the program "adds 7 and 5". Each one followed one small rule. The boxes are the memory, the cards are registers and the table on the wall is the decoder. A real CPU works the same way, billions of times a second, and nobody inside it understands the program either.
3Explore
The same short loop, billions of times a second
Gates can add, compare and choose, but something has to tell them what to do, in what order and with which numbers. That is the job of the central processing unit, the CPU. It repeats one short loop. The program counter (PC) holds the address of the next instruction. Fetch: the CPU reads the cell the PC points to into the instruction register (IR) and moves the PC on by one. Decode: control circuits look at the opcode bits and set up the right paths, for instance connecting the adder for ADD.
Drag to turn. Scroll or pinch to zoom.
4Problem
The three phases
Put the phases of the instruction cycle in order.
- Execute
- Fetch
- Decode
Hint
Read it, work out what it means, do it.
Solution
Answer:
- Fetch
- Decode
- Execute
Read the instruction, work out what it means, then do it.
5Problem
The PC after a fetch
The CPU fetches the instruction at address 3. What is the PC right after the fetch?
Hint
The PC moves on by one during the fetch.
Solution
Answer: 4
Fetch moves the PC on by one to the next instruction.
6Explain
Phases of the 7 + 5 program
The scene runs the same program the class acted out. Press Step and the PC, IR and ACC change exactly as the cards did: LOAD 14, then ADD 15, STORE 13 and HALT, four instructions of three phases each, twelve phases in all. The multiplication program uses the same few instructions. Its JZ and JUMP send the PC back to 0 twice before the counter in cell 15 reaches zero. So it runs 24 instructions in 72 phases. Six times the instructions of the addition.
7Problem
Instructions run
How many instructions does the 7 + 5 program run, including HALT?
Hint
LOAD, ADD, STORE, HALT.
Solution
Answer: 4
LOAD 14, ADD 15, STORE 13, HALT.
8Problem
Phases in all
Each instruction takes three phases. How many phases does the 7 + 5 program take?
Hint
4 instructions × 3 phases.
Solution
Answer: 12
4 instructions × 3 phases = 12.
9Explain
The program sits in the same memory as the numbers
On the board, the instructions and the numbers 7 and 5 were in the same row of boxes. That is no accident. In June 1945 John von Neumann wrote the "First Draft of a Report on the EDVAC". It described a computer that keeps its instructions in the same memory as its data. Almost every computer since has worked that way. An instruction is just a number whose bits are split into fields.
| Opcode | Name | What it does |
|---|---|---|
| 0000 | HALT | Stop |
| 0001 | LOAD a | ACC = cell a |
| 0010 | ADD a | ACC = ACC + cell a |
| 0011 | SUB a | ACC = ACC − cell a |
| 0100 | STORE a | cell a = ACC |
| 0101 | JUMP a | PC = a |
| 0110 | JZ a | if ACC = 0, PC = a |
10Problem
Decode 0010 1111
In the toy computer, what does 0010 1111 mean?
Hint
0010 is ADD and 1111 is 15.
Solution
Answer: ADD 15
Opcode 0010 is ADD; address 1111 is 15.
11Problem
Encode LOAD 14
Hint
LOAD is 0001 and 14 is 1110.
Solution
Answer: 00011110
LOAD is 0001 and 14 is 1110.
12Problem
STORE 13
What does STORE 13 do?
Hint
STORE writes the accumulator into memory.
Solution
Answer: Copies the accumulator into cell 13
STORE writes from the CPU to memory; LOAD reads the other way.
13Problem
Repeating a loop
Which instruction lets a program repeat a loop until a count reaches zero?
Hint
It jumps only when the accumulator is 0.
Solution
Answer: JZ (jump if zero)
A conditional jump chooses between going round again and leaving the loop.
14Problem
The First Draft
In which year did von Neumann write the First Draft of a Report on the EDVAC?
Hint
June of the last year of the Second World War.
Solution
Answer: 1945
It is dated 30 June 1945.
15Explain
How fast the loop goes
The steps keep time with a clock signal, a voltage that flips on and off at a steady rate. A 3 GHz clock ticks 3,000,000,000 times a second, so one tick lasts 1 ÷ 3,000,000,000 seconds, about 0.333 nanoseconds. Light covers only about 10 centimetres in that time. At one phase per tick, that is enough ticks to run the toy's 7 + 5 program 250 million times a second. Real processors don't finish one whole instruction per tick in the simple way our toy does.
16Problem
One tick at 3 GHz
A clock runs at 3 GHz. How long is one tick, in nanoseconds? (three decimal places)
ns
Hint
1 ÷ 3 nanoseconds.
Solution
Answer: 0.333 ns
One tick lasts 1 ÷ 3,000,000,000 s ≈ 0.333 × 10⁻⁹ s = 0.333 ns. Taking the reciprocal gives seconds per tick, which must then be converted to nanoseconds.
17Practice
The toy CPU in JavaScript
The loop already fetches, decodes, runs LOAD and stops at HALT. Add the two missing rules: ADD a (ACC = ACC + cell a) and STORE a (cell a = ACC). Then cell 13 holds 12 and four instructions have run.
12
4const mem = [0x1E, 0x2F, 0x4D, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5];
let pc = 0, acc = 0, count = 0;
while (true) {
const ir = mem[pc];
pc = pc + 1;
const op = ir >> 4, a = ir & 15;
count++;
if (op === 0) break;
if (op === 1) acc = mem[a];
}
console.log(mem[13]);
console.log(count);
Hint
ADD is opcode 2 and STORE is opcode 4, as in the table.
A solution
const mem = [0x1E, 0x2F, 0x4D, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5];
let pc = 0, acc = 0, count = 0;
while (true) {
const ir = mem[pc];
pc = pc + 1;
const op = ir >> 4, a = ir & 15;
count++;
if (op === 0) break;
if (op === 1) acc = mem[a];
if (op === 2) acc = acc + mem[a];
if (op === 4) mem[a] = acc;
}
console.log(mem[13]);
console.log(count);18Explain
Common mistakes
"The PC counts how many instructions have run."
The PC holds the address of the next instruction. In the 4 × 3 program it is sent back to 0 twice, while the number of finished instructions keeps climbing to 24.
"The CPU reads a program the way we read it."
It reads 8 bits, splits them into an opcode and an address, and follows fixed wiring for that opcode. Like the students with the cards, it never knows that the program adds 7 and 5.
"A higher GHz number always means a faster computer."
The clock sets how often a step can happen, not how much gets done in each tick. Real chips overlap instruction phases and have several cores, so two chips at the same GHz can finish very different amounts of work.
19
Summary
- Programs are stored as numbers in the same memory as data (von Neumann, 1945).
- The CPU repeats fetch, decode, execute; the PC points to the next instruction.
- An instruction's bits split into an opcode and an address.
- The clock sets the pace: 3 GHz is one tick every 0.333 ns.