Show contents
Chapter 4 of 7 · 20 min
The processor and memory
By the end of this chapter, you'll be able to
- Describe the role of the processor, RAM and storage
- Explain the fetch, decode, execute cycle
- Simulate a simple processor with an accumulator
- Compare the orders of magnitude of the speed of different memories
The machine's parts
Any computer is organised around three elements:
- the processor (CPU) executes the instructions. It contains a few registers, tiny ultra-fast memories where it keeps the values being computed;
- RAM (random-access memory) holds the running programs and their data. It is fast, but it is wiped when the power is cut;
- storage (SSD, memory card, hard disk) keeps files durably, even when switched off, but it is much slower.
The speed gaps are huge. In orders of magnitude, and depending on the hardware:
| Memory | Typical access time |
|---|---|
| processor register | less than a nanosecond |
| RAM | around a hundred nanoseconds |
| SSD | in the order of 100 microseconds |
| mechanical hard disk | in the order of 10 milliseconds |
A nanosecond is a billionth of a second. Between a register and a hard disk, the ratio exceeds a million: this is why a program that constantly reads the disk is so slow, and why computers keep in RAM what they use often.
The processor cycle
A program, in memory, is a list of instructions. The processor keeps in a special register, the program counter, the position of the next instruction. Then it repeats, endlessly, the same cycle:
- fetch the instruction at the position given by the counter;
- decode: which operation, on which values?
- execute, then move to the next instruction.
Let's simulate a minimal processor, with a single computing register, the accumulator:
program = [
("LOAD", 7),
("ADD", 5),
("MULTIPLY", 3),
("PRINT",),
("STOP",),
]
acc = 0
counter = 0
while True:
instruction = program[counter]
operation = instruction[0]
counter += 1
if operation == "LOAD":
acc = instruction[1]
elif operation == "ADD":
acc += instruction[1]
elif operation == "MULTIPLY":
acc *= instruction[1]
elif operation == "PRINT":
print("accumulator:", acc)
elif operation == "STOP":
break
accumulator: 36
The program computes (7 + 5) × 3. Each turn of the while loop is a cycle: fetch program[counter], decode with the ifs, execute. A real processor does the same thing in electronics, billions of times per second.
Cycle 5: STOP. The loop ends.
Text description of the animation
- At start-up, the accumulator and the counter are 0.
- Cycle 1: fetch instruction 0, LOAD 7. The accumulator gets 7.
- Cycle 2: ADD 5. The accumulator becomes 12.
- Cycle 3: MULTIPLY 3. The accumulator is 36.
- Cycle 4: PRINT. The result leaves the machine.
- Cycle 5: STOP. The loop ends.
Jumps: deciding and repeating
With only straight-line calculations, it is impossible to write an if or a loop. Processors therefore have jump instructions: they change the program counter, to go elsewhere in the program, possibly under a condition. Let's add DECREMENT (subtract 1) and JUMP_IF_POSITIVE to our machine, to count down:
program = [
("LOAD", 3),
("PRINT",),
("DECREMENT",),
("JUMP_IF_POSITIVE", 1),
("STOP",),
]
acc, counter = 0, 0
while True:
operation, *arguments = program[counter]
counter += 1
if operation == "LOAD":
acc = arguments[0]
elif operation == "PRINT":
print(acc)
elif operation == "DECREMENT":
acc -= 1
elif operation == "JUMP_IF_POSITIVE":
if acc > 0:
counter = arguments[0]
elif operation == "STOP":
break
3
2
1
JUMP_IF_POSITIVE 1 sends the counter back to instruction 1 (PRINT) as long as the accumulator is positive: this is a loop. All the if, while and for of your programs end up, in the end, as jumps of this kind.
Mini-lab: program the machinePython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: one more instruction
Add a SUBTRACT n instruction to the machine, then write a program that computes the price of a meal for 4 people at 2,500 FCFA, minus a 1,500 FCFA voucher, and prints it.
Hint 1
elif branch in the decoding.Hint 2
Show the solution and common mistakes
def run(program):
acc, counter = 0, 0
while True:
operation, *arguments = program[counter]
counter += 1
if operation == "LOAD":
acc = arguments[0]
elif operation == "ADD":
acc += arguments[0]
elif operation == "SUBTRACT":
acc -= arguments[0]
elif operation == "MULTIPLY":
acc *= arguments[0]
elif operation == "PRINT":
print(acc)
elif operation == "STOP":
return
run([("LOAD", 2500), ("MULTIPLY", 4), ("SUBTRACT", 1500), ("PRINT",), ("STOP",)])
8500
Putting the loop in a run function lets you launch several programs on the same machine: this is the principle of an interpreter.
Common mistakes:
- Writing
acc = arguments[0] - acc: the subtraction is reversed, and you get −8,500 instead of 8,500. - Forgetting
STOPat the end of the program: the counter goes past the last instruction and Python raises anIndexError. A real processor would carry on executing whatever follows in memory.
Summary
- 1The processor executes instructions, RAM keeps the work in progress, storage keeps the files.
- 2Memories range from registers (less than a nanosecond) to disks (up to milliseconds).
- 3The processor repeats a cycle: fetch the instruction, decode it, execute it, move to the next.
- 4Jump instructions change the program counter: this is how conditions and loops are born.
- 5An interpreter is a program that reads and executes the instructions of another program.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.