In the previous chapter, The Journey of a computer program to the CPU, we discussed how a program starts its journey to the CPU once it is opened.
In this chapter, I'll cover how CPU fetches, decodes, and executes the instructions of a computer program.
Central Processing Unit*, known as the CPU is where the execution of instructions happens.
The CPU is the brain of your computer and interacts with the memory (RAM) over the system bus to store and retrieve data while executing each instruction.
CPU consists of two main units, Control Unit (CU) and Arithmetic/Logical Unit (ALU).
The CU is a component of the CPU that controls the whole operation within the CPU.
CU is where the instructions are fetched (from memory), decoded, and executed.
ALU is another component of the CPU, which contains electrical circuits to carry out arithmetic and logical operations.
Arithmetic operations include primitive mathematical operations, such as addition, subtraction, division, and multiplication.
Logical operations usually involve operations used to make a decision.
For instance, comparison, which is basically comparing two values based on a comparison operator such as >, =, and <.
Additionally, multiple comparisons can be combined by using the logical operators, such as AND, OR, AND OR, etc
What are CPU registers?
Registers are special storage units, the same as memory blocks but inside the CPU.
The data fetched from the memory is temporarily placed inside the registers, so CPU can pick ’em up instantly.
Imagine a carpenter who keeps a big box of nails on a shelf.
He usually puts a handful of nails into a box and puts it on his working desk – for quicker access.
When he needs to nail a piece of wood he picks a few from the small box and puts them aside.
In this analogy, the nail box on the shelf is a persistent storage device, like the hard disk. The small box on the table is the memory. The area he temporarily puts the nails is a register.
The number of registers inside a CPU depends on the CPU’s architecture, it can be 8, 16, 32, or more.
Some registers are general-purpose and are used to store any value.
However, some registers are specialized for a particular purpose; for example, to store memory addresses, data, or program instructions.
How CPU executes an instruction
CPU performs tasks in cycles, called Fetch-Decode-Execute cycles a.k.a instruction cycles.
You already know the instructions of each process reside in the memory, and the operating system uses scheduling algorithms to determine the next runnable process.
Each instruction cycle consists of three phases: Fetch, Decode, and Execute.
During each cycle, the CPU fetches the next instruction of a process (which is scheduled to run) from the memory.
Then, it decodes the fetched instruction (to know what exactly has to be done), and finally executes it.
This is one cycle.
Let’s go deeper and see what exactly happens during each phase.
The fetch phase
The program counter (PC) is a special CPU register, which always contains the memory address of the next instruction to be executed.
The CPU copies the memory address stored in the PC into another register called Memory Address Register (MAR).
Then, sends this address to the memory.
But what does it mean when we say “it sends the address”?
Here’s an explanation:
In a computer, the data is transmitted across the system in the form of electrical signals over a network of wires on the computer’s motherboard or cables.
This data transmission network is called the system bus.
System bus transmits data between the CPU and other components, including the memory.
The address bus is a component of the system bus, which is used to transmit an address (in binary format) to the memory.
The address bus is used any time the CPU needs to access the memory.
So whatever value is on the address bus, it’s always a memory address.
Along with the memory address, the CPU sends a read signal to the memory as well, to indicate what it wants to do with data stored in that address.
This signal is sent across another component of the system bus, called the control bus; the control bus is also a set of wires to transmit control signals.
Once the memory receives the address from the address bus entry, and the control signal (read) from the control bus entry, it sends the data back to the CPU, over the data bus.
The data bus is used to transmit the actual data.
The returned data in this case is a process instruction.
Consequently, the returned data is copied into another register called Memory Data Register (MDR).
Until now, two different registers have been used, one for keeping a memory address (MAR), and one for keeping the fetched data (MDR).
Now, the instruction stored in the MDR is copied into another register called Instruction Register (CIR).
The fetch phase is done here. The next thing to do is to decode CIR content.
At this point, the PC register is incremented to point to the next memory address, which contains the next instruction of the next cycle.
However, under certain conditions, the PC can also jump over some memory blocks to point to an instruction somewhere else; This usually happens when the CPU encounters a flow control instruction, such as an if
or for
loop in a program.
The Decode Phase
During the decode phase, CU decodes the instruction residing in the CIR register, so that the CPU can tell how many operands it needs to fetch to perform the instruction.
For instance, if the instruction increments a variable value, the CPU should fetch the current value from the memory as well.
The CU then sends signals to other components within the CPU, such as the ALU and FPU if an arithmetic/logical or floating-point operation is needed to be executed.
The Execute phase
The last phase in the instruction cycle is to execute the instruction fetched and decoded by now.
A mentioned earlier, if the decoded instruction involves arithmetic/logical operations, the ALU is used.
To recap, the CPU keeps reading the PC content on every cycle, fetches the instruction stored in that address, decodes it, and executes it.
This cycle is repeated as long as the computer is switched on.
The fetch-decode-execute cycle begins as soon as the system is turned on, with an initial PC value that is predefined by the system’s architecture.
This initial address in PC points to special instructions in the read-only memory (ROM), to start the system’s firmware and boot up the operating system (by using the boot loader).
CPU Clock
Like any digital system, the CPU uses a binary number system (1’s and 0’s) to deal with data. These and zeros are determined by the state of billions of transistors (on/off) in the CPU.
If you’re wondering what binary is, it’s a number system, which uses 1 and 0 to represent a numerical value, as opposed to a decimal system, which uses numbers 0 to 9.
For instance, number 3 in binary would be 011, and 4 would be 100.
These ones and zeros are presented as electric signals with two different states when transmitted across the system; a high voltage to represent 1, and a low voltage to represent 0.
When executing instructions, the transistors that exist in the CPU keep switching between ON and OFF (upon receiving a signal), to represent binary values.
Let’s suppose an arithmetic operating is done, and as a result, a set of transistors should go on and off to represent the result in binary format.
However, transistors do not operate in real-time, meaning it takes a short while for a transistor to change its state.
That said, it takes a while until a certain value is reflected in a set of transistors.
Now, if the CPU refers to this value, while the said transistors are still transitioning, we might get unexpected or even invalid results.
To solve this problem, the CPU manufacturers came up with the idea of CPU clocks to give the CPU a sense of time and make CPU operations synchronous.
This synchronous approach allows the CPU to wait for a period of time before checking the transistors’ state. This period is long enough to ensure all transistors are already in their new state.
This period is called one clock cycle. A fetch-decode execution cycle happens during one clock cycle.
This is the CPU’s perception of time, just like seconds that we use to measure our time.
Clock cycles are in the form of electrical pulses generated by an internal oscillator circuit at a fixed rate.
On the other hand, the time between receiving one pulse and the other is one clock cycle. A beep sound is also generated the same way as a clock pulse.
Clock Rate
The clock rate of a CPU is the number of clock pulses per second.
The higher it is, the more instructions the CPU can fetch, decode, and execute in a second. This is not always the case, though as there are other parameters involved in defining the CPU’s performance – such as its architecture, cache storage capacity, etc.
Overclocking means increasing the CPU’s clock rate (if possible), to get the highest performance out of the current CPU you already have – without purchasing a faster CPU.
I hope, by now, you have a high-level understanding of how the CPU communicates with other system components, such as the memory, to fetch, decode, and execute your programs.
In the next chapter of this series, I'll cover an important feature of the operating system, Multitasking.
Thanks for reading.