AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

One step at a time

Week 25 · 4.2 Computer architecture · 3 lessons
3

Fetch, decode, execute

Computing 9: Topic 4.2, pp. 312–319
GoalDescribe the fetch–decode–execute cycle and trace a short program through it.
New words
fetch · the CPU gets the next instruction from RAMdecode · the CPU works out what the instruction meansexecute · the CPU carries out the instructioncycle · a set of steps that repeats again and again
Explanation

Instructions are stored as binary, like all other data, and the instructions of a program that is running are kept in RAM. The CPU follows them with a cycle of three steps. First it fetches the next instruction from RAM. Then it decodes the instruction, which means it works out what it must do. Last it executes the instruction, which means it does it. After that the cycle starts again with the next instruction, and it goes on until no instructions are left. Each instruction contains a short binary code that names the action, such as add or subtract. A short code keeps the amount of data small. The codes in this lesson are invented, because real CPUs use their own.

Fetch the next instruction from RAMDecode: work out what it meansExecute: carry it out
Worked examples
StepPart of the cycleWhat happens
1fetchthe values 11 and 4 are fetched from RAM
2fetchthe code 110 is fetched from RAM
3decode110 means subtract, so the CPU knows what to do
4execute11 − 4 is worked out: the result is 7
One instruction in four steps. In our invented set, the code 110 means subtract and 011 means add. The instruction is to subtract 4 from 11. The CPU first fetches the values, then the code, then decodes the code, and only then executes.
CycleFetchDecodeExecute
1011 6 9add6 + 9 = 15
2110 20 8subtract20 − 8 = 12
3011 30 12add30 + 12 = 42
A program of three instructions stored in RAM, written as code, first value and second value. The cycle runs three times, once for each instruction. After the third result there is no instruction left to fetch, so the cycle stops.
Python
1codes = ["011", "110", "011"]
2firsts = [6, 20, 30]
3seconds = [9, 8, 12]
4for i in range(3):
5    code = codes[i]
6    if code == "011":
7        action = "add"
8        result = firsts[i] + seconds[i]
9    elif code == "110":
10        action = "subtract"
11        result = firsts[i] - seconds[i]
12    print("Cycle", i + 1, code, action, result)
Output
Cycle 1 011 add 15
Cycle 2 110 subtract 12
Cycle 3 011 add 42
A Python model of the cycle. Three lists hold the instructions: the codes, the first values and the second values. The loop is the cycle: each time round it fetches one code and its two values, decodes the code with if and elif, and executes the sum. It is only a model, because a real CPU does this with electronics and not with Python.
Class activity

Be the CPU (no computer). A partner is the RAM. They write four instructions on separate cards in the form code, number, number (for example 011 5 7) and stack them face down. On a code sheet you write 011 = add and 110 = subtract. You are the CPU. Fetch: take the top card. Decode: look the code up on your sheet and say the action aloud. Execute: work out the result and write it on a list. Repeat until the stack is empty. Your partner checks your four results. Then swap roles.

Practice
1
Put these three actions in the order the CPU does them: (a) it finds that the code 011 means add; (b) it adds 4 and 9; (c) it takes the instruction from RAM. Name each part of the cycle.
2
Use 011 = add and 110 = subtract. Complete the table for these two instructions stored in RAM.
CycleFetchDecodeExecute
1110 25 9??
2011 8 17??
3
Write fetch, decode or execute for each description.
DescriptionPart of the cycle
(a) The CPU copies the next instruction from RAM.?
(b) The CPU finds that 110 means subtract.?
(c) The CPU works out 30 − 12.?
(d) The CPU gets the values 30 and 12 from RAM.?
4
A program has 50 instructions. How many times does the cycle run for it? Why is the process called a cycle?