Skip to 0 minutes and 1 secondNow let's look at how the CPU can perform calculations using a process known as the fetch, decode, execute cycle.

Skip to 0 minutes and 13 secondsHere we have a representation of a CPU and RAM. The registers are also shown, where data can be temporarily stored. The program counter starts at 0000. This means that the first address in RAM that the computer will look for an instruction is at 0000. The computer needs somewhere to store the current address in RAM that it is looking for. This is what the memory address register is for. 0000 is therefore copied into the MAR. A signal is then sent down to the address bus, to the RAM. The control unit sends out a memory read signal and the contents of the address 0000 are copied through the data bus to the memory data register.

Skip to 0 minutes and 59 secondsAs the contents of address 0000 is an instruction, it is copied into the instruction register. As our first instruction has been fetched, the system is at the end of the fetch stage of the cycle. The program counter can be incremented by one so the system is ready to read the next instruction.

Skip to 1 minute and 22 secondsNow the instruction needs to be decoded. It is sent via the data bus to the control unit. Here, the data is split into two sections. There is the operation code, or op code, which in this example is the first four bits. This is the command that the computer will carry out. The second four bits of the operand. This is the address in RAM where the data to be operated on is stored. The control unit can translate op code into instructions, so here, the control unit translates the op code 0101 into a load from RAM instruction.

Skip to 2 minutes and 7 secondsNow the command will be executed. The operand is copied to the MAR as this is the address of the data that needs to be loaded. The data address 0100 is then fetched from RAM and then passed up the data bus to the MDR. As it is not an instruction but simply data, it is then passed to the accumulator. This is a complete fetch, decode, execute cycle. We'll run through the next cycle a little faster so that you can see the entire program being executed. The instruction op codes and address operand is placed in the IR and the PC increase by one again.

Skip to 2 minutes and 44 secondsNow the instruction is decoded while the address of the data to be acted on is placed in the MAR.

Skip to 2 minutes and 54 secondsThe new data is fetched from the address and eventually ends up in the accumulator, along with the results of the previous cycle. To finish off this stage, the two values in the accumulator are passed in through the ALU, where they can be added together as was instructed by the op code. The results are then placed back into the accumulator. So that's a second cycle complete. The last cycle is for the instruction at 0010. It uses the op code 0011, which is store, and the operand 0110, which is the last address in the RAM shown. So this cycle takes the results of the addition in the accumulator and stores it back into RAM.

Fetch, decode, execute

Now let’s look at how the CPU can perform calculations, using a process known as the Fetch Decode Execute cycle.

Here we have a representation of a CPU and RAM. The registers are also shown, where data can be temporarily stored.

A general bus connects the control unit (represented as a set of filing cards) to the ALU (represented by a calculator), and through another branch to the registers (represented by post-it notes). The RAM, represented by a sheet on a clipboard with a list of addresses and their contents, is connected to the general bus by a control bus, data bus and address bus.

Fetch

The Program Counter (PC) starts at 0000. This means that the first address in RAM where the computer will look for an instruction is at 0000.

The same as previous diagram, except the PC has a value of 0000.

The computer needs somewhere to store the current address in RAM that it is looking for. This is what the Memory Address Register (MAR) is for. 0000 is therefore copied into the MAR.

The same as previous diagram, except the MAR has a value of 0000.

A signal is now sent down through the Address bus to the RAM. The Control Unit sends out a memory read signal, and the contents of the address 0000 are copied through the data bus to the Memory Data Register.

Animation showing 0000 travels down the data bus to the RAM, and a signal travelling from the control unit down the control bus to the RAM. The contents at address 0000, which is 0101 0100, then travel along the data bus to the MDR.

As the data fetched during the fetch stage is an instruction, it is copied into the Instruction Register (IR).

As the end of the previous animation, except the IR now contains 0101 0100.

As the first instruction has been fetched, the system is at the end of the Fetch stage of the cycle. The program counter can be incremented by 1, so the system is ready to read the next instruction when the next Fetch cycle starts.

As the previous image, except PC has been incremented to 0001.

Decode

Now the instruction needs to be decoded. It is sent via the data bus to the control unit, where it is split into two parts. The first part is the operation code or opcode, which in this example CPU is the first 4 bits. This is the command that the computer will carry out. The second part, in this case the second 4 bits, is the operand. This is an address in RAM where data will be read from or written to, depending on the operation.

The Control Unit can translate opcodes into instructions. So here the Control Unit translates the opcode 0101 into a LOAD FROM RAM instruction.

The first half of the contents of IR, in this case 0101, travels along the bus to the control unit, which flips around to display the translation of the instruction "0101 LOAD".

Execute

Now the command will be executed. The operand is copied to the MAR, as this is the address of the data that needs to be loaded.

The data at address 0100 is then fetched from RAM and passed up the data bus to the MDR. As it is not an instruction but simply data, it is then passed to the Accumulator (Acc).

Animation showing the second half of IR, 0100, being copied to MAR. It then travels along the address bus to RAM, and the contents of address 0100, which is 0000 0001, travel along the data bus to the MDR.

This is a complete Fetch Decode Execute Cycle.

We’ll now run through the remaining two cycles of the program. The PC now holds 0001, so we next fetch, decode and execute the instruction at that address.

Animation showing the entire cycle described below.

  • The PC is at 0001, so this is the next instruction to be fetched.
  • The instruction opcode and address operand are placed in the IR, and the PC increased by 1 again.
  • Now the instruction is decoded, while the address of the data to be acted upon is placed in the MAR. The instruction turns out to be ADD, which adds two pieces of data together.
  • The new data is fetched from the address and eventually ends up in the accumulator, along with the results of the previous cycle.
  • To finish off this stage, the two values in the accumulator are passed into the ALU, where they can be added together, as was instructed by the opcode.
  • The result is then placed back into the accumulator.

So that’s a second cycle complete.

The last cycle is for the instruction at 0010. It uses the opcode 0011 which is STORE and the operand 0110, which is the last address in the RAM shown.

Animation showing the last cycle described above.

So this cycle takes the results of the addition in the accumulator and stores it back into RAM at address 0110, as requested.

To recap, our program used three instructions to add two numbers and store the result in memory. The first instruction LOADed a piece of data from a specified address. The second ADDed this to the data found in another address, and the final instruction STOREd the result of the addition back into a specified address in memory.

Share this video:

This video is from the free online course:

How Computers Work: Demystifying Computation

Raspberry Pi Foundation