Fetch-Decode-Execute Cycle
Computer Function
the basic function of a computer is program execution
when a program is run the executable binary file is copied from the disk drive
into memory
the process of program execution is the retrieval of instructions and data from
memory, and the execution of the various operations
program execution stops only when the computer is switched off; while the
machine is on, the cycle is continuous! (an infinite loop)
program execution is performed by Control Unit (CU) of the CPU
the task of the Control Unit is the instruction cycle (sometimes called the
fetch-decode-execute cycle, or just the fetch-execute cycle)
the instruction cycle consists of the tasks: the fetch cycle, the indirect cycle,
the execute cycle, and the interrupt cycle
the sequence of actions of the instruction cycle can be seen in the following
diagram:
Micro-Operations
the lowest level atomic operations that a computer performs are micro
operations
at each stage during the instruction cycle, a series of micro operations are
performed
for example, the execution cycle has various different sets of micro operations
to perform the various arithmetic and logic operations
The Fetch Cycle
the first phase of the instruction cycle
special purpose CPU registers are involved:
o MAR: the Memory Address Register specifies the address in memory
for a read or write operation
o MBR: the Memory Buffer Register is used to contain the value to be
stored in memory or the last value read from memory
o PC: the Program Counter holds the address of the next instruction to be
fetched
o IR: the Instruction Register is used to contain the the op code of the
last instruction
the sequence of micro code actions of the Fetch cycle are:
move the contents of PC into MAR
move the contents of the memory address given by the value of MAR,
and store the data in MBR
3. increment the value of PC
4. move the contents of MBR into IR
special note: it is possible that some op code may be stored in more than a
single memory address (thus requiring several fetches...)
1.
2.
The Indirect (Decode) Cycle
the fetch cycle is responsible for setting up the instructions - next, the
operands of the instruction must be fetched from memory
the process can be quiet involved, as an instruction may have several operands
spanning several memory cells
basically this step involves converting "indirect" addresses (like variables) to
"direct" addresses specifying the exact location in memory at which to find the
data
in short, the actions of the Indirect cycle are:
o for each of the operands which need to be decoded:
perform a series of memory reads (in the style of the fetch
cycle)
replace the indirect addresses with direct addresses
The Execute Cycle
for each op code, a particular sequence of micro operations are performed (the
sequence will be different for each op code)
for example, there may be an op code for the assembly instruction: ADD R0,
X
o the fetch cycle & the indirect cycle would have already loaded the
instruction and converted the address of X to a direct address (if
necessary)
o next, the micro code for the add instruction would be performed:
move the address of X into MAR
read the value at MAR from memory and store in MBR
compute R0 + the contents of MBR and store the result in R0
the micro code for a particular instruction could be rather complex (eg. micro
code to compute two's-compliment binary division)
The Interrupt Cycle
after the execute cycle is completed, a test is made to determine if an interrupt
was enabled (e.g. so that another process can access the CPU)
if not, instruction cycle returns to the fetch cycle
if so, the interrupt cycle might performs the following tasks: (simplified...)
o move the current value of PC into MBR
o move the PC-save-address into MAR
o move the interrupt-routine-address into PC
o move the contents of the address in MBR into indicated memory cell
o continue the instruction cycle within the interrupt routine
o after the interrupt routine finishes, the PC-save-address is used to reset
the value of PC and program execution can continue
Example Assembly code
these are a simplified set of actual assembly language instructions
a particular computer might have a set of op codes representing each of the
assembly instructions
LOAD register address
Loads the value from memory at the address into the register
STORE register address
Stores to memory at the address the value from the register
SET register value
Sets the register to the value
ADD0 register
Adds the contents of the register to the register R0, the result is stored in R0
SUBTRACT0 register
Subtracts the contents of the register from the register R0, the result is stored
in R0
JUMP address
Sets the PC to the address
JUMP0ZERO address
END
Sets the PC to the address if the value of register R0 is 0
Returns control to the OS
Example C++ code fragment:
int Foo, Blah;
Foo = 27;
Blah = Blah - Foo;
if (Blah == 0)
Foo++
The assembly code version:
%---- Foo = 27;
0 SET
R0 27
1 STORE
R0 14
%---- Blah = Blah - Foo;
2 LOAD
R1 14
3 LOAD
R0 15
4 SUBTRACT0 R1
5 STORE
R0 15
%---- if (Blah == 0)
6 LOAD
R0 15
7 JUMP0ZERO
9
8 JUMP
13
%---- Foo++
9 SET
R1 1
10 LOAD
R0 14
11 ADD0
R1
12 STORE
R0 14
13 END
%---- int Foo, Blah; debug information ints take 1 byte each
14 "Foo"
15 "Blah"
(Note that an compiler optimisation would remove line 6!)
Another example: what is happening here??
%---- Index = 3;
0 SET
R0 3
1 STORE
R0 17
%---- Total = 0;
2 SET
R0 0
3 STORE
R0 16
%---- while (Index != 0) {
4 LOAD
R0 17
5 JUMP0ZERO
15
%---- Total += Index;
6 LOAD
R0 16
7 LOAD
R1 17
8 ADD0
R1
9 STORE
R0 16
%---- Index--;
10 LOAD
R0 17
11 SET
R1 1
12 SUBTRACT0 R1
13 STORE
R0 17
%---- }
14 JUMP
4
15 END
%---- int Total,Index; debug information ints take 1 byte each
16 "Total"
17 "Index"
Self-Assessment Questions
1. What are the basic stages of the fetch-execute cycle?
2. List 2 registers that are critical to the fetch-execute cycle.
3. Which computer components are important in the fetch-execute cycle? For
each, state the reasons why.
4. Can any computer component become involved in the fetch-execute cycle?
5. Describe what happens when an instruction is fetched.
6. Describe what happens while an instruction is decoded.
7. Describe what happens during the execution of an instruction.
8. Given the following abstract computer, manually perform the fetch-execute
cycle to run the program already loaded into main memory. Keep track of the
state of the registers while the program is running.
The op codes and the actions are:
9999
1231
1356
2546
5647
6435
stop
if R0 > 0 PC = PC - 3
R0 = R1
R2 = R0 + R1
R0 = 4
R1 = R0 - 1