Back

An example of how the registers are used in the fetch - decode - execute cycle

Understanding how the CPU works, registers and low-level programming.
The Little Man computer and SimCom are available on the Internet and will help you understand how programming (the software) works with the CPU, RAM and the registers (the hardware).

Registers
A Von Neumann CPU (the type of CPU you get in nearly all personal computers) has a number of 'registers'. These are very fast memory circuits. You can think of each register as a box which holds a piece of data useful to the CPU. These pieces of data allow the CPU to quickly fetch and then execute instuctions from RAM, and also to write data to RAM. They hold important information information that enables instructions to be processed. The registers you should know about include:

Program Counter (PC) - this holds the address of the next instruction to be fetched and executed.

Current Instruction Register (CIR) - this holds the current instruction being executed.

Memory Address Register (MAR) - this holds the RAM address you want to read to or write from.

Memory Data Register (MDR) - this holds the data you have read from RAM or want to write to RAM.

Accumulators - these hold the data being worked on and the results of arithmetic and logical operations.

Using registers to execute an instruction in a program.
Imagine there is a program in RAM. The Program Counter is currently pointing to memory location 3254, so 3254 is the address of the next instruction that must be fetched, decoded and executed. At this location, there is an instruction ADD 75567. We can show this as a diagram (although note that we have not used binary anywhere in the example, to make it easier to understand).

 Registers

How are the registers used to fetch, decode and execute an instruction in a program?

FETCH

    • The CPU reads the contents of the Program Counter to find the address of the next instruction to be fetched, decoded and executed. In our case 3254.
    • As soon as it is read, the PC increments. PC = PC + 1, or 3255
    • The contents of 3254 are then put on the MAR.
    • The address in the MAR is then located in RAM.
    • The contents of this address are moved to the MDR.
    • The MDR now holds the instruction that must be executed.
    • The instruction in the MDR is then copied to the CIR, as we will often need to use the MDR again to complete the executation of an instruction.

DECODE

    • The contents of the CIR are divided. Part of the instruction might be an operation (like ADD) and part of the instruction might be data, or in our case, an address where data can be found, like 75567. The ADD part is known as the OPERATOR and the data part is known as the OPERAND.
    • The operator (ADD) is decoded by the Contol Unit in the CPU, so it knows what it has to do (ADD in our case).
    • The operand 75567 is put back on the MAR.
    • The contents of 75567 is then found in RAM and put on the MDR.

EXECUTE

    • The instruction can now be executed. Arithmetic and logical instructions are carried out using the Accumulator(s) in a CPU.
    • Signals are sent out to different parts of the CPU to execute the instruction ADD.
    • In our example, this will result in adding 4500 to whatever is in the Accumulator, and then over-writing the contents of the Accumulator with the result of the addition.

The way registers are used to run programs is often known as the FETCH - DECODE - EXECUTE cycle. This is because that is all the CPU actually does. It fetches instructions, decodes them and then executes them. It does this very quickly indeed, but that is all it does. It is why you sometimes read that computers aren't very clever!

Back