The CPU, also known as a microprocessor, is the heart and/or brain of a computer. Let’s get into the heart of computers to help us program them effectively.
Tools are usually simpler than machines; It is usually used with the hand, and the machine is often moved by power or steam power
– Charles Babbage
The computer is a machine driven primarily by electricity, but its flexibility and programmability contribute to the simplicity of the tool. The CPU is the core and brain of the computer and executes the set of instructions provided to it. The main job of the CPU is to perform arithmetic and logical operations and to orchestrate sets of operational instructions together. Before diving into the main parts, let’s look at what the main components of a CPU are and what they do: The two main components of the processor
- CU(Control unit)
- Arithmetic and Logic Unit: ALU
Control unit – CU
The CONTROL unit (CU) is a part of the CPU that helps coordinate the execution of instructions. It tells us what to do. According to this instruction, it helps activate the wires 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. The control unit comes in two forms:
- Hardwired control unit.
- Programmable (microprogrammed) control unit
A hardwired control unit is hardware that needs to be changed to modify its work, because a PROGRAMMABLE microcontroller control unit can change its behavior. Hardwired CU is faster at processing instructions, while programmable CU can be more flexible.
Arithmetic Logic Unit – ALU
The arithmetic and Logic Unit (ALU), as its name suggests, performs all arithmetic and logic calculations. For example, the ALU performs operations such as addition and subtraction. An ALU consists of logic circuits or logic gates that perform these operations. Most logic gates have two inputs and one output. A bellows is an example of a semi-adder circuit that receives two inputs and outputs the result. Here A and B are the inputs, S is the outputs, and C is the carry.
Storage – Registers and memory
The main job of the CPU is to execute the instructions provided to it. To process these instructions in most cases, it needs data. Some data are intermediate data, some of which are inputs and some of which are outputs. The data required to execute these instructions is stored in the following stores:
Register (register)
Registers are small groups of places where data can be stored. Registers are a combination of latches. A latch, also known as a trigger, is a combination of logic gates that store 1 bit of information. The latch has two input wires, a write wire and an input wire, and an output wire. We can make the write line change the stored data. When the write line is disabled, the output is always the same.The CPU uses registers to store output data. Because it is intermediate data, it is slow to send to main storage (RAM). The data is sent to other registers connected by BUS. Registers can store instructions, output data, store addresses, or any type of data.
Memory (RAM)
RAM is a set of registers arranged and packed together in an optimized way so that it can store more data. RAM (random access memory) is volatile; its data is lost when we turn off the power. Since RAM is a collection of read/write data registers, RAM receives input for 8-bit addresses, data input for storing the actual data, and ultimately a read/write enabler for latches.
What is an instruction set?
An instruction is the smallest level of granularity a computer can perform. The CPU can process various types of instructions. The instructions include the following:
- Arithmetic operations, such as addition or subtraction
- Logical operations such as and, or, not
- Data instructions such as move, input, output, load and store
- Control flow instructions such as goto, if… Goto, call and return
- Notify the CPU program that Halt has ended
Instructions are provided to the computer in assembly language, either generated by the compiler or interpreted in some high-level language. These instructions are hardwired inside the CPU. The ALU contains arithmetic and logic, 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. A single clock cycle is very small, about 250 * 10 * -12 seconds. The faster the clock cycle of the processor. CPU clock cycles are measured in gHz (gigahertz). 1gHz equals 10⁹Hz (Hertz). Hertz means number two. So 1 gigahertz means 10⁹ cycles per second. The faster the clock cycle, the more instructions the CPU can execute. Clock cycles = 1 / clock rate CPU Time = Number of clock cycles/clock rate This means that by optimizing the instructions we provide to the CPU to improve CPU time, we can increase the clock rate or decrease the number of clock cycles. Some processors offer the ability to increase clock cycles, but because it is a physical change, there may be overheating or even smoke/fire.
How is an instruction executed
The instructions are stored in RAM in sequence. For a hypothetical CPU, an instruction consists of an OP code (operation code) and a memory or register address. The control unit instruction register (IR) has two registers, the OP code for loading the instruction and the instruction address register, which is used to load the address of the currently executing instruction. There are other registers inside the CPU that store the values stored in the last 4 bit address 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, say <1100 1000>. The first four bits are opcodes. This determines the instruction. The instruction is extracted into the IR of the control unit. The instruction is decoded as load_A, which means that it needs to load the data from address 1000 into the last 4 bits of the instruction in register A.
- STEP 2 – LOAD_B 2:
Similar to above, this loads the data in memory address 2 (0010) into CPU register B.
- STEP 3 — ADD B A:
Now the next instruction is to add these two numbers. Here CU tells ALU to perform the add operation and save the result back to register A.
- STEP 4 – STORE_A 23:
This is a very simple set of instructions to help add two numbers. Since then we’ve managed to add up two numbers.
Bus (Bus)
All data between the CPU, registers, memory and IO devices is transmitted through the bus. To load data into the memory just added, the CPU writes the memory address to the address bus, writes the result of the sum to the data bus, and enables the correct signal in the control bus. In this way, data is loaded into memory with the help of the bus.
Cache (Cache)
The CPU also has a mechanism for prefetching instructions into its cache. As far as we know, processors can execute millions of instructions in a second. This means that it takes more time to get instructions from RAM than it does to execute them. As a result, the CPU cache prefetches some instructions and data for faster execution. If the data in the cache is different from the data in the manipulation store, the data is marked as dirty.
Instruction pipeline
Modern cpus use instruction pipelining to perform instruction parallelization. Get, decode, execute. While an instruction is in the decode phase, the CPU can process another instruction for the fetch phase.This can be a problem when one instruction depends on another. As a result, the processor executes instructions that are independent and in a different order.
Multicore computer
It’s basically different cpus, but there are some shared resources, such as caches. Performance The Performance of a CPU is determined by its execution time, Performance = 1/ execution time. Suppose a program takes 20ms to execute. CPU performance is 1/20 = 0.05ms, relative performance = execution time 1/ execution time 2. CPU performance considerations are instruction execution time and CPU clock speed. Therefore, in order to improve the performance of the program, we need to increase the clock speed or reduce the number of instructions in the program. Processor speeds are limited, and modern computers with multiple cores can support millions of instructions per second. But if we write a program that has a lot of instructions, this will degrade overall performance. The big O notation determines how performance will be affected based on a given input. A lot of optimizations were done in the CPU to make it faster and execute as much as possible. When writing any program, we need to consider how reducing the number of instructions we provide to the CPU will improve the performance of the computer program.
The original address
How does a CPU work?