By Milap Neupane
Translation: Crazy geek
Original: www.freecodecamp.org/news/how-do…
Reproduced without permission
The CPU, also known as the microprocessor, is the brain of the computer. Let’s help ourselves write computer programs efficiently by getting to the heart of the computer.
“Tools are often simpler than machines, which can be operated by hand and which need to be powered by animals or steam.”
Charles Babbage
Computers are machines powered by electricity, but their flexibility and programmability help keep the tools simple.
The CPU is the brain of the computer. It executes the instructions provided to it. Its main job is to perform arithmetic and logical operations and orchestrate instructions together. Before diving into the main parts, let’s take a look at the main components of the CPU and what they do:
The two main components of a processor
- Control Unit – CU (Control Unit)
- Arithmetic and Logical Unit — ALU (Arithmetic and Logical Unit)
Control unit – CU
The CONTROL unit CU is the part of the CPU that helps coordinate the execution of instructions. It tells the CPU what to do. Following this instruction, activate the circuits that connect the CPU to different parts of the computer, including the ALU. The control unit is the first component of the CPU that receives processing instructions.
There are two types of control units:
- Hardwired control unit.
- Microprogrammable (microprogrammable) control units.
Hardwired control units are hardware that need to be changed to modify the way it works; Programmable control units can be programmed to change their behavior. Hardwired CU is faster at processing instructions, while programmable CU is more flexible.
Arithmetic and logic unit – ALU
The arithmetic and logic unit ALU does all arithmetic and logic calculations. The ALU performs addition, subtraction, and other operations. The ALU consists of logic circuits or gates that perform these operations.
Most logic gates have two inputs and one output.
Below is an example of a half-adder circuit that takes two inputs and outputs the result. A and B are inputs, S is output, and C is carry.
Storage – Registers and memory
The CPU’s main job is to execute the instructions given to it. Usually to process these instructions, it requires data. Some data is intermediate data, some of which is input and some of which is output. The data and instructions are stored in the following storage:
register
A register is a small group of places where data can be stored. A register is a combination of latches. A latch, also known as a trigger, is a combination of logic gates that can store 1 bit of information.
The latch has two input lines: write and input lines, and an output line. We can make write lines change the stored data. When the write line is disabled, the output is always the same.
An SR latch consisting of a pair of cross-coupledOr not (NOR)The doorcomposition
The CPU uses registers to store output data. Because it is intermediate data, getting it to main memory (RAM) is slow. This data is sent to other registers connected by ** BUS **. Registers can be used to store instructions, output data, storage addresses, or other types of data.
Memory (RAM)
RAM is a collection of registers that are tightly arranged in an optimized way so that it can store more data. RAM (random access memory) is volatile and its data is lost when the power is turned off.
What is an instruction
Instructions are granular computations that a computer can perform. The CPU can handle various types of instructions.
The instructions include:
- Arithmetic operations, such as addition and subtraction
- Logical instructions, such as and, or, or not
- Data instructions such as move, input, output, load and store
- Process control instructions, such as GOto, if… Goto, Call, return
- A HALT instruction that informs the CPU that the program has ended
Instructions are provided to the computer using assembly language, can also be generated by a compiler, or interpreted in some high-level language.
These instructions are hardwired inside the CPU. The ALU contains arithmetic and logical operations, where the flow of control is managed by the CU.
Computers can execute one instruction in a clock cycle, but modern computers can execute multiple instructions.
A set of instructions that a computer can execute is called an instruction set.
CPU clock
Clock cycle
The speed of a computer is determined by its clock cycle. It is the number of computer clock cycles per second. The single clock period is very small, about 250 * 10^-12 seconds. The shorter the clock cycle, the faster the processor.
The CPU clock cycle is measured in gHz (gigahertz). 1gHz is equal to 10⁹Hz. Hertz means cycles per second. So 1 gigahertz is 10⁹ cycles per second.
The higher the clock cycle, the more instructions the CPU can execute. Clock period = 1 / Clock rate CPU time = Clock cycles/clock rate
This means you can increase the clock rate and optimize the speed at which the CPU executes instructions by improving CPU time. Some processors offer the ability to increase the clock cycle, but because it is a physical change, there can be problems with overheating or even burning the CPU.
How to execute instructions
Instructions are stored sequentially in RAM. Suppose we have a CPU whose instructions include an OP code (operation code) and a memory or register address.
There are two instruction registers (IR) in the control unit, which is used to load the OP code of the instruction and the instruction address register, which is used to load the address of the currently executed instruction. There are other registers inside the CPU that hold the values stored in the address of the last four bits of the instruction.
Let’s take an example of an instruction that adds two numbers. Here are the instructions and instructions:
STEP 1 — LOAD_A 8:
The instruction is initially stored in RAM, such as <1100 1000>. The first four bits are the opcode, which determines what the instruction is. This command is extracted into the IR of the control unit. The instruction is decoded to load_A, which means it needs to load data in address 1000, the last four bits of the instruction in register A.
STEP 2 — LOAD_B 2
Similar to the above, this loads the data from memory address 2 (0010) into CPU register B.
STEP 3 — ADD B A
Now the next instruction is to add the two numbers. Here CU tells ALU to add and save the result back to register A.
STEP 4 — STORE_A 23
This is a very simple set of instructions for adding two numbers.
We have successfully added two numbers!
The bus
All data between the CPU, registers, memory, and IO devices is transferred over the bus. To load the previous addition data into memory, the CPU writes the memory address to the address bus, writes the result of the operation to the data bus, and enables the correct signal in the control bus. In this way, data is loaded into storage with the help of the bus.
The cache
The CPU also has a mechanism for prefetching instructions to its cache. As far as we know, processors can complete millions of instructions in a second. This means that it takes more time to get instructions from RAM than to execute them. So the CPU cache prefetches instructions and data to make execution faster.
If the data in the cache and the operation memory are different, the data is marked as dirty bits.
Instruction pipeline
Modern cpus use an instruction pipeline to perform instruction parallelization. Acquisition-decode-execute. While one instruction is in the decode phase, the CPU can process another instruction for the fetch phase.
But there are problems when one instruction depends on another. So the processor executes instructions without dependencies and in a different order.
Multicore computer
It’s basically different cpus, but with some shared resources, such as caches.
performance
A CPU’s performance is determined by its execution time. Performance = 1 / Execution time
Suppose it takes 20ms to execute a program. CPU performance is 1/20 = 0.05ms Relative performance = Execution time 1/ execution time 2
CPU performance takes into account instruction execution time and CPU clock speed. Therefore, to improve the performance of the program, we need to increase the clock speed or reduce the number of instructions in the program. Processors are limited in speed, and modern computers with multiple cores can support millions of instructions per second. However, if we write a program with many instructions, this will reduce overall performance.
The Big O notation determines how performance is affected based on a given input.
Engineers have done a lot of optimization in the CPU to make it faster and execute as many instructions as possible. When writing any program, we need to consider how reducing the number of instructions provided to the CPU will improve the performance of the computer program.