Fetch-execute cycle
The fetch-execute cycleThe basic operation of a computer, in which an instruction is fetched from the memory and then executed. is the steps a CPU goes through in order to process an instruction.
At the start of the fetch-execute cycle, both the data and the instruction are loaded into the computer's main memory so they are ready to be processed.
What is an instruction?
An instruction is an action that a program wants the CPU to carry out. For example, this could be to move specific data from one memory location (registerThe section of high speed memory within the CPU that stores data to be processed.) to another. The table below shows three instructions. Each instruction is made up of an operator and two operandData that is manipulated by the CPU/processor according to the given opcode, eg in the instruction ADD 3, the operand is 3..
The CPU first fetches an instruction from the RAM. It then decodes this instruction to work out what operation it needs to do (the operator), and what the operands are (the data). Finally, the CPU will execute the instruction.
| Instruction | Operator | Operand | Execution |
| move, #161, r0 | move | #161 | Place value 161 into register 0 |
| move, #16, r1 | move | #16 | Place value 16 into register 1 |
| add, r0, r1 | add | - | Add the values in registers 0 and 1 |
| Instruction | move, #161, r0 |
|---|---|
| Operator | move |
| Operand | #161 |
| Execution | Place value 161 into register 0 |
| Instruction | move, #16, r1 |
|---|---|
| Operator | move |
| Operand | #16 |
| Execution | Place value 16 into register 1 |
| Instruction | add, r0, r1 |
|---|---|
| Operator | add |
| Operand | - |
| Execution | Add the values in registers 0 and 1 |
In this example, the CPU places 161 into register 0, places 16 into register 1 and adds both numbers together. This would require three cycles of fetch-execute.
Fetching an instruction
The Program Counter (PC) copies the address of the next instruction it contains into the Memory Address Register (MAR)This stores the memory address of the current instruction being executed.
The MAR places the address to be used on to the address busThis is a one-way bus which requests data from an address in memory..
The instruction on the data busThis bus moves data in both directions as data is both read and written to memory is loaded into the Memory Data Register (MDR).
The MDR copies the instruction into the instruction register (IR)The current instruction is stored in this register in binary.
Executing an instruction
The instruction in the IR is carried out (executed) by the CPU. This will generally involve the ALU carrying out a calculation.
Now that the CPU is executing an instruction, the Program Counter can now be reset to point to the next instruction.
Summary of the fetch-execute cycle
- The processor checks the program counter to see which instruction to run next
- The Program Counter gives an address value in the memory of where the next instruction is
- The processor fetches the instruction value from this memory location
- Once the instruction has been fetched, it needs to be decoded and executed. For example, this could involve taking one value, putting it into the ALU, then taking a different value from a register and adding the two together
- Once this is complete, the processor goes back to the Program Counter to find the next instruction
- This cycle is repeated until the program ends