Lesson 1 · Unit 03
Fetch, decode, execute
The one loop every CPU runs
What you practise
- Registers, the program counter, a toy instruction set traced by hand, jumps make loops and ifs
Try a question from this lesson
Pick one
A CPU can only do tiny things, like "add two numbers" or "copy a value". How does it get through a whole program?
Correct answer
It repeats: fetch one instruction, do it, go to the next
Why
A program is a long list of tiny instructions in memory, and the CPU works through them one at a time, very fast. It never sees the program as a whole.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The cycle
The CPU keeps its working values in a few tiny slots called registers (here A and B). One special register, the program counter (PC), holds the address of the next instruction.
Forever: fetch the instruction at PC, decode it, execute it, add 1 to PC.
0: LOAD A, 5 ; A = 5
1: LOAD B, 3 ; B = 3
2: ADD A, B ; A = A + B
3: HALT ; stopConcept
Jumps
A jump instruction overwrites the PC, so the CPU carries on somewhere else.
Jump backward and you have a loop. Jump only when a condition holds (here JNZ: "jump if not zero") and you have an if. Every loop and branch you write turns into jumps.
0: LOAD A, 3 ; A = 3
1: SUB A, 1 ; A = A - 1
2: JNZ A, 1 ; if A != 0, go to 1
3: HALT10 more exercises in the app
- 4× type the answer
- 2× multiple choice
- 1× fill the blanks
- 1× order the lines
- 1× match the pairs
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.