Introduction
A computer is a machine which takes an input, processes the data, and produces an output. To process the data, the computers follow a set of instructions, known as a program.
The program is stored in the memory, the CPU executes it.
Fetch
The Program Counter (PC) is checked - it holds the memory address of the next instruction that will be executed
The Memory Address Register (MAR) copies the PC's output
The address is sent along the address bus to the memory where it awaits a read signal by the Control Unit (CU)
The CU will send along the control bus a read signal
The contents of the main memory at the specified memory address can be sent along the data bus to the MDR
If the CPU is fetching an instruction, the data in the MDR gets copied to the Current Instruction Register (CIR)
Now it's been fetched, the PC increments to the next instruction address to be executed
Decode
CPU instructions are made up of two parts: the op-code and the operand
The op-code is the actual instruction, such as LDA
(load
from
memory address) or ADD
The operand is the actual data, such as 0xf0
If it is an instruction that is loaded, it gets sent to the ALU's accumulator
Execute
Now that we have our instruction, we can actually do something with it
The memory address is sent to the MAR
If there is an instruction, it awaits a read signal from the control bus
The data is sent along the data bus back to the CPU MDR
The contents is copied to the accumulator
The cycle is now complete
Branching
The PC normally increments by 1 every time an instruction is executed
However, this is not always the case - if () {} else {}
statements would cause some lines to be skipped
The CIR has some instructions for branching:
BRA
BRZ
BRP
These replace the values in the PC with the contents of the operand in the CIR
When each fetch-execute cycle begins, these are checked
The program would "jump" to the memory address specified