The Central Processing Unit (CPU) is the brain of the computer because it is what makes all the decisions and it is where all programs are executed. The purpose of the CPU is to process data. The CPU works by following a process known as ‘fetch, decode and execute’. The CPU fetches an instruction from memory, decodes this instruction and then executes it. The CPU carries out this cycle continuously, millions of times per second. Whatever software is being used on computers, the CPU processes the data being used.
Before this process can begin, both the program and data are loaded into Random-access memory (RAM) by the operating system. This is then loaded into a temporary memory area inside the CPU called a memory address register (MAR). The CPU is now ready to carry out the fetch decode execute cycle.
STEP 1. Fetch
The Control Unit sends a signal to the RAM in order to fetch the program and data, which is then stored in one of the CPU’s register. To do so, the CPU makes use of a vital hardware path called the ‘address bus’ along which the program and data travels.
The Control Unit then increments the Program Counter (PC). The PC is an important register that keeps track of the running order of the instructions and shows which instruction in the program is due to be executed next.
The CPU then places the address of the next item to be fetched on to the address bus. Data from this address then moves from main memory into the CPU by travelling along another hardware path called the ‘data bus’.
STEP 2. Decode
The ‘instruction set’ of the CPU is designed to understand a specific set of commands, which serves to make sense of the instruction it has just fetched. This process is called ‘decode’.
A single piece of program code might require several instructions. For example, look at this Python 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.
STEP 3. Execute
This is the part of the cycle when data processing takes place, and the instruction is executed. In the example above, the values from the two variables, length and width would be multiplied together. The result of this processing is then stored as the variable area in yet another register.
Once the execute stage is complete, the CPU begins the cycle all over again.