Billions of tiny steps
When you tap an app on your phone, its processor starts running the app's instructions. It runs billions of them every second.
Each instruction is very small, such as "add these two numbers". And every one goes through the same three steps. Today you watch them, one at a time.
By the end of this lesson
Note
I can follow the fetch-decode-execute cycle.
Key words
| Word | What it means | Example |
|---|---|---|
| Instruction | one small step a processor can do | add a0, a0, a1 |
| Memory address | the number of one place in memory | |
| Register | a tiny, fast store inside the processor | a0 |
| Program counter | the register holding the address of the next instruction | |
| Assembly | a processor's instructions written as short words | |
| Cycle | the same steps, repeated again and again |
The cells students fill in are left blank in this preview.
What is inside a processor
A program waits in memory as a list of instructions, each one at its own memory address.
The processor has a control unit, which reads each instruction and works out what to do. It has an arithmetic logic unit (ALU), built from gates like last lesson's, which adds and compares. And it has registers, tiny stores that hold the numbers it is working on.
The parts and their jobs
| Part | Its job |
|---|---|
| Memory | Holds the program and its data |
| Program counter | Holds the address of the next instruction |
| Control unit | Works out what an instruction means |
| ALU | Does the adding, subtracting and logic |
| Registers such as a0 and a1 | Hold the numbers being worked on |
The cells students fill in are left blank in this preview.
Fetch, decode, execute, again
Fetch: the processor copies the instruction at the address in the program counter. The program counter moves on to the next address.
Decode: the control unit works out what the instruction asks for. Execute: the processor does it, for example the ALU adds two registers. Then the cycle starts again.
Before you start
Note
Press Start the lab on the bar at the top and wait for the machine to say it is running. The program you read is written for a RISC-V processor. The lab machine is not RISC-V, so an emulator runs it.
Go to the arch folder
cd ~/archShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read a five-instruction program
cat add.sShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Turn the words into machine code
riscv64-linux-gnu-as add.s -o add.oShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make it into a program
riscv64-linux-gnu-ld add.o -o addShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
See each instruction at its address
riscv64-linux-gnu-objdump -d addShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: tracing the program
| Program counter | Instruction | What execute does | a0 | a1 |
|---|---|---|---|---|
| 100b0 | li a0,2 | puts 2 in a0 | 2 | ? |
| 100b4 | li a1,3 | puts 3 in a1 | 2 | 3 |
| 100b8 | add a0,a0,a1 | the ALU adds them into a0 | 5 | 3 |
| 100bc | li a7,93 | picks the call that ends a program | 5 | 3 |
| 100c0 | ecall | asks Linux to end it, status a0 | 5 | 3 |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think the program counter counts how many instructions have run. It holds the address of the next one. Here it goes up by 4 each time, because each instruction is 4 bytes. A jump puts a new address in it instead.
Copy the program with 10 for 3
sed 's/a1, 3/a1, 10/' add.s > ten.sShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Assemble your copy
riscv64-linux-gnu-as ten.s -o ten.oShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Link your copy
riscv64-linux-gnu-ld ten.o -o tenShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Predict, then run it on the emulator
qemu-riscv64 ./tenShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the answer it left
echo $?Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further: watch the program counter
Press Open terminal and type gdb ./loop after building it with gcc -O2 -o loop loop.c. Type break main, then run.
Type info registers pc to see the program counter. Type stepi to run one instruction, then look at pc again. By how much did it move? Type quit, then y.