Back

The function of the CPU as fetch and execute instructions stored in memory

All programs and files you are currently using are held in RAM. When you open a new piece of software, the operating system decides where to put it in memory, so that it doesn't interfere with all the other programs and files you are using. How does the CPU actually run a piece of software you've just opened?

    • The address of the first instruction in the program is put into the Program Counter (PC) when you open it.
    • The CPU goes to the Program Counter, to get the address of the first instruction in the program.
    • It then increments the Program Counter, ready for next FETCH - DECODE - EXECUTE instruction after the current one has been completed.
    • The CPU then fetches the instruction from RAM, DECODES it and then EXECUTES it.

When the instruction has been completed, the CPU goes to the PC, gets the address of the next instruction, increments the PC, and does a new FETCH - DECODE - EXECUTE cycle. This continues until you have finished with a piece of software and close it.

RAMCPU

How are multiple programs handled by the CPU?
The computer can process billions of instructions in a second, but remember, it can only work on one instruction in one application at any one time. If you have three applications open at once, the computer will be keeping track of where it is up to for each application. More importantly, it will be keeping track of the contents of the Program Counter for each of the applications.

A simple example
Consider the situation where you are working on three applications at the same time on your computer.

    • If the CPU does a FETCH - DECODE - EXECUTE cycle for the first five instructions in Application 1, the PC is now pointing to the address that holds instruction 6.
    • You open a second application. The CPU saves the contents of the PC for Application 1.
    • The address for the first instruction of Application 2 is now loaded into the PC.
    • If the CPU does a FETCH - DECODE - EXECUTE cycle for the first ten instructions in Application 2, the PC is now pointing to the address that holds instruction 11.
    • You open a third application. The CPU saves the contents of the PC for Application 2.
    • The address for the first instruction of Application 3 is now loaded into the PC.
    • If the CPU does a FETCH - DECODE - EXECUTE cycle for the first twenty instructions in Application 3, the PC is now pointing to the address that holds instruction 21.
    • The CPU now switches back to work on Application 1 again.
    • The address for where you got up to in Application 1 is loaded back into the PC. 
    • The CPU does ten more instructions, so the PC is now pointing to the address that holds instruction 16.
    • The CPU now switches back to work on Application 2 again.
    • The address for where you got up to in Application 2 is loaded back into the PC.
    • The CPU does three more instructions, so the PC is now pointing to the address that holds instruction 14.

This process continues for the applications you are running. The computer is constantly saving where each application has got up to by saving the contents of the PC, and then loading that value back into the PC when ready to do some more processing on the application, ready for the next FETCH - DECODE - EXECUTE cycle.

Summary
You might think that this process is very complicated, but if you break it down, the computer is only actually doing a few tasks:

    • saving the contents of the PC
    • loading where a previous application got up to into the PC
    • doing a FETCH - DECODE - EXECUTE cycle
    • switching to the next application

These are exactly the kind of repetitive tasks that a computer is perfect for.

Back