How the CPU Runs a Program
Fetch, decode, execute, repeat. One loop, a few billion times a second, and why your phone gets warm instead of getting faster.
- Explain why instructions and data live in the same memory
- Name the three phases of the instruction cycle and what each one does
- Follow the program counter and accumulator through a short program
- Explain what a clock speed measures and why more cores appeared instead of more gigahertz
Finish first
Topic contents
Lesson 1 of 3
Instructions are just numbers lying in memory
Memory is a long row of numbered boxes, and every box holds a number. Some numbers are data your program works on. Some numbers are the program itself, written as numbers. The boxes look the same, which is a clever trick and also why security lessons matter later.
That trick is why one machine can run many programs. Early computers were rewired for each new job. Today you download a game, open a school app, or load a video editor, and the same chip runs them all because programs are just numbers stored like anything else.
The processor does not ask whether a number is data or an instruction. It reads the box and follows the rule. That is why you should only run programs from places you trust.
Fetch, decode, execute
A four-instruction program running one phase at a time, with memory, the program counter and the accumulator all visible.
Fetch, decode, execute
one instruction per cycle- ▸0LOAD 4
- 1ADD 5
- 2STORE 6
- 3HALT
- 412data
- 530data
- 60data
cycles run: 0
Program loaded
Memory holds both the program and its data, which is why a computer can run anything: instructions are just numbers in the same store. This one will read one number, add a second to it, and write the answer back to memory. The program counter says which address to run next, and it starts at 0.
Lesson 2 of 3
The loop that never stops
The processor keeps a program counter, which is just the address it should read next. From there it repeats three phases forever. Fetch: copy whatever is at that address into the processor. Decode: work out what that pattern of bits means. Execute: do it, and move the program counter on.
Step through the lab beside this lesson. LOAD copies a value from memory into the accumulator, which is the processor's single scratch register. ADD adds another value to whatever the accumulator holds, using the adder circuit from the previous topic. STORE writes the accumulator back out to memory, and until that happens the result exists only inside the processor. HALT stops the loop.
Notice how little the processor knows at any moment. It has one address, one instruction, and one value in hand. It cannot see the rest of your program, has no idea what the program is for, and holds no memory of what it did two instructions ago. Everything a computer appears to understand is built out of this loop being extremely fast and extremely obedient.
Lesson 3 of 3
Clock speed, heat, and why cores appeared
A clock signal keeps every part of the processor in step, ticking billions of times a second. Clock speed in gigahertz counts those ticks. For a while, faster clocks meant snappier phones and laptops, so the number went up every year.
Then it stopped rising. Switching faster makes the chip hotter. Past roughly 4 GHz the heat is hard to get rid of, so chip makers put several complete processors, called cores, on one chip instead of chasing one super-fast core.
That is why your phone may list eight cores. A game that only uses one core cannot make the other seven help. Sharing work across cores is a puzzle for programmers, which you will meet again in older classes.
Worked examples
Read the code, then change it
Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.
# Memory holds the program and the data together, exactly like the real thing.
memory = [
("LOAD", 4), # address 0
("ADD", 5), # address 1
("STORE", 6), # address 2
("HALT", None),# address 3
12, # address 4, data
30, # address 5, data
0, # address 6, where the answer will go
]
program_counter = 0
accumulator = None
while True:
opcode, operand = memory[program_counter] # fetch
print("PC", program_counter, "->", opcode, operand) # decode
if opcode == "HALT":
break
if opcode == "LOAD":
accumulator = memory[operand]
elif opcode == "ADD":
accumulator = accumulator + memory[operand]
elif opcode == "STORE":
memory[operand] = accumulator
program_counter += 1 # execute done
print(" accumulator:", accumulator)
print("memory[6] is now", memory[6])
# Try it: change STORE 6 to STORE 0 and predict what happens.
# The processor will happily overwrite its own first instruction.Practice
Work these out yourself
No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.
0/3 attemptedAssessment
Check your understanding
Answer each question, then read the explanation. That is where the learning is.
Finished this topic?
0 of 3 lessons marked done. Marking the topic complete ticks the rest.
