Registers and the Fetch-Decode-Execute cycle
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 'decode' and then 'execute' the instuctions held in RAM that are part of a program, one instruction at a time.
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.
- Status Register - this holds information about the last operation e.g. whether the least sum done produced a negative result.
- Interrupt Register - this holds details about whether an interrupt has happened.
- Index register - this is a very fast counter, that is used e.g. when you have to work though a block of data and need to keep track of which piece of data you are at.
Using registers to execute an instruction in a program.
Consider the following situation:
Note in the above that we have not used binary either for the RAM address or the contents, to make things easier to understand!
How are the registers used to read an instruction in a program?
- 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.
- 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.
- 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!