The fetch-execute cycle
The basic operation of a computer is called the 鈥榝etch-execute鈥 cycle. The CPU is designed to understand a set of instructionA single action that can be performed by a computer processor. - the instruction set. It fetches the instructions from the main memoryThe part of a computer that stores data. and executeTo run a computer program. them. This is done repeatedly from when the computer is booted up to when it is shut down.
- The CPU fetches the instructions one at a time from the main memory into the registers. One register is the program counter (pc). The pc holds the memory address of the next instruction to be fetched from main memory.
- The CPU decodes the instruction.
- The CPU executes the instruction.
- Repeat until there are no more instructions.
A single piece of program code might require several instructions. Look at this Python (3.x) code:
area = length * width
First, the computer needs to load in the value of the variable length into the immediate access store (registers). Next it needs to load in the value of the variable width. Then it needs to multiply the two numbers together, and finally it needs to store the result in the variable area.