Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Following a processor's steps (fetch-decode-execute)

After this lesson: I can follow the fetch-decode-execute cycle.

50 min lesson10 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

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

WordWhat it meansExample
Instructionone small step a processor can doadd a0, a0, a1
Memory addressthe number of one place in memory
Registera tiny, fast store inside the processora0
Program counterthe register holding the address of the next instruction
Assemblya processor's instructions written as short words
Cyclethe 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

PartIts job
MemoryHolds the program and its data
Program counterHolds the address of the next instruction
Control unitWorks out what an instruction means
ALUDoes the adding, subtracting and logic
Registers such as a0 and a1Hold 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

Command
cd ~/arch

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read a five-instruction program

Command
cat add.s

Shown 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

Command
riscv64-linux-gnu-as add.s -o add.o

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make it into a program

Command
riscv64-linux-gnu-ld add.o -o add

Shown 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

Command
riscv64-linux-gnu-objdump -d add

Shown 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 counterInstructionWhat execute doesa0a1
100b0li a0,2puts 2 in a02?
100b4li a1,3puts 3 in a123
100b8add a0,a0,a1the ALU adds them into a053
100bcli a7,93picks the call that ends a program53
100c0ecallasks Linux to end it, status a053

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

Command
sed 's/a1, 3/a1, 10/' add.s > ten.s

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Assemble your copy

Command
riscv64-linux-gnu-as ten.s -o ten.o

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Link your copy

Command
riscv64-linux-gnu-ld ten.o -o ten

Shown 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

Command
qemu-riscv64 ./ten

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the answer it left

Command
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.

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.