The fetch-decode-obey cycle


next up previous contents
Next: Stored programs
Up: No Title
Previous: Machine code
Back: to main list of student notes

The fetch-decode-obey cycle

The idea is that the computer keeps running through a cycle which consists of three stages:

  1. Fetch. Copy into the instruction register, the contents of the memory location addressed by the program counter.

  2. Decode. This contents will be a number. Separate the part which describes the instruction itself (first two digits in our example) from the rest.

  3. Obey. Obey the instruction. Then, unless it has already changed the program counter, add one to it so it points at the following location in memory.

Assume the contents of memory is as follows:

0
. 4107.
1
. 4200.
2
. 4805.
3
. 4607.
4
. 4701.
5
. 4307.
6
. 4900.
7
. 3458.

Assume that the program counter initially contains zero. We now repeat the fetch-decode-obey cycle as follows, where ACC = accumulator, IR = instruction register and PC = program counter.

Fetch 1. Put into IR the contents of the memory location addressed by the PC. So IR = 4107.

bf Decode 1. What does the IR mean? Understood as an instruction, it means ``clear location 7''.

Obey 1. Location 7 is now zero. Add 1 to the PC, so it's now 1.

Fetch 2. Put into IR the contents of the memory location addressed by the PC. So IR = 4200.

bf Decode 2. What does the IR mean? Understood as an instruction, it means ``read a number from keyboard into the accumulator''.

Obey 2. Do this, and assume the user typed 9. So ACC=9. Add 1 to the PC, so it's now 2.

Fetch 3. Put into IR the contents of the memory location addressed by the PC. So IR = 4805.

bf Decode 3. What does the IR mean? Understood as an instruction, it means ``If ACC=0, then copy 5 into the PC''.

Obey 3. Since ACC isn't zero, don't do the copy. Add 1 to the PC, so it's now 3.

Fetch 4. Put into IR the contents of the memory location addressed by the PC. So IR = 4607.

bf Decode 4. What does the IR mean? Understood as an instruction, it means ``Add ACC to memory location 7''.

Obey 4. Do this. Memory location 7 is now 9. Add 1 the PC, so it's now 4.

Fetch 5. Put into IR the contents of the memory location addressed by the PC. So IR = 4701.

bf Decode 5. What does the IR mean? Understood as an instruction, it means ``Copy 01 into the PC''.

Obey 5. Do this. The PC is now 1. Don't add 1, since it's just been changed.

Fetch 6. Put into IR the contents of the memory location addressed by the PC. So IR = 4200.

...and so we can go on. If you assume that the user types (say) 11, then 13, and then 0, you'll see that this program adds the non-zero numbers and stops when the user types zero.

This demo uses a very simple architecture. Real computers have many more instructions, much bigger memory, more accumulators, etc. They do not usually have one instruction just to print and another to read. Also, as I said above, real computers will always encode their instructions in binary, so that instead of splitting off the first two decimal digits, you split off (say) the first five binary ones. But the principle is the same.


next up previous contents
Next: Stored programs
Up: No Title
Previous: Machine code
Back: to main list of student notes



Jocelyn Ireson-Paine
Wed Feb 14 23:46:11 GMT 1996