What is a CPU?
The relationship between CPU and computer is equivalent to the relationship between the brain and humans. It is a minicomputer chip, usually embedded in the motherboard of the computer. The CPU is built by placing billions of tiny transistors on a computer chip. These transistors enable them to perform the calculations required to run programs stored in system memory. So, it can also be said that the CPU determines the computing power of your computer.
What does the 02 CPU actually do?
The core of CPU work is to obtain instructions from programs or applications and execute calculations. This process has three key stages: extraction, decoding, and execution. The CPU obtains instructions from the system's RAM, decodes the actual content of the instructions, and finally executes the instructions by the relevant parts of the CPU.
03 CPU internal structure
I just mentioned the importance of many CPUs, so what is the internal structure of CPUs? What is it composed of? The following figure shows the running process of a general program (using C language as an example). Generally speaking, understanding the running process of a program is the foundation and prerequisite for mastering the running mechanism of the program.

General program operation process
During this process, the CPU is responsible for interpreting and running the content ultimately converted into machine language. The CPU is mainly composed of two parts: a control unit and an arithmetic logic unit (ALU).
Control unit: Extract instructions from memory and decode them for execution;
Arithmetic Logic Unit (ALU): Handles arithmetic and logical operations.
CPU and memory are electronic components composed of many transistors, which can be compared to the heart and brain of a computer. It can receive data input, execute instructions, and process related information, and communicate with input/output (I/O) devices that send data to and receive data from the CPU.
From a functional perspective, the content of the CPU consists of four parts: registers, controllers, arithmetic units, and clocks, each connected by a power on signal.
Next, let's briefly introduce memory. Why do we need to talk about memory when it comes to CPUs? Because memory is the bridge for communication with the CPU. All programs in the computer run in memory. Memory is usually referred to as the main memory, and its function is to store operational data in the CPU and exchange data with external storage devices such as hard drives.
When the computer is running, the CPU will transfer the data that needs to be calculated to the main memory for operation. After the operation is completed, the CPU sends the results, and the operation of the main memory also determines the stable operation of the computer. The main memory is usually connected to the CPU through a control chip and consists of read-write components, with each byte having an address number.
The CPU reads data and instructions from the main memory through the address, and can also write data based on the address. Please note that when the computer shuts down, the instructions and data in the memory will also be cleared.
04 CPU is a set of registers
Among the four structures of CPU, the importance of registers is much higher than the other three. Why do we say that? Because programs typically describe registers as objects. When it comes to registers, we must talk about assembly language. When it comes to assembly language, we must talk about high-level programming language. When we talk about high-level programming language, we must mention the concept of language.
05 computer language
The oldest and most direct medium of communication between people is language, but in order to communicate with computers, it is necessary to exchange computer instructions, which involves language issues. At the beginning, assembly language emerged to solve communication problems between computers and humans. However, assembly language is obscure, so there are high-level programming language such as C, C++and Java. Therefore, computer language is usually divided into low-level language and high-level programming language. Programs written in high-level programming language can run only after being compiled and converted into machine language, while assembly language can be converted from assembly program to machine language.
06 Assembly Language
This is a part of programming in assembly language. Assembly language uses mnemonics to write programs. Each machine language instruction that was originally an electrical signal has a corresponding mnemonic. For example, mov and add are abbreviations for data storage (move) and addition, respectively.
Assembly language and machine language have one-to-one correspondence, which is different from high-level programming language. We usually convert programs written in assembly language into machine language. This process is called assembly. On the contrary, the process of converting machine language into assembly language is called disassembly.
Assembly language can help you understand the functions of computers. Machine language level programs are processed through registers. The eax and ebp in the above code are both registers, which are the names of internal registers in the CPU. Therefore, it can be said that the CPU is a set of registers.
sually, the memory in the memory is represented by an address number, and the type of register is distinguished by its name. These different types of CPUs have different types and numbers of internal registers, as well as a range of values stored in the registers.
07 Program Counter
The program counter is used to store the address of the unit where the next instruction is located. When the program is executed, the initial value of the PC is used as the address of the first instruction in the program. When sequentially executing a program, the controller first retrieves the instruction from memory based on the instruction address indicated by the program counter, and then analyzes and executes the instruction. At the same time, the value of PC increases by 1, pointing to the next instruction to be executed.
We can carefully observe the execution of program counters through an example:

Program Counter Execution
This is an add operation, the program starts, and after compilation and parsing, the program in the hard drive will be copied to memory through the operating system.
The above example program performs the addition operation of 123 and 456, and then outputs the results to the display. Because it is difficult to describe in machine language, these are all translated results.
In fact, each instruction and data can be distributed at different addresses, but for better explanation, the memory and data that make up the instructions are placed at a single memory address.
Address 0100 is the starting location for program execution. After Windows and other operating systems copy a program from the hard drive to memory, they set the program counter to the starting position 0100 and then execute the program. After each instruction execution, the counter value will increase by 1 or directly point to the address of the next instruction.
Then, the CPU will read the command from memory and execute it based on the value of the program counter. In other words, the program counter controls the flow of the program.
08 Conditional branching and loop mechanisms
The conditional control flow summarized by high-level programming language is mainly divided into three types: sequential execution, conditional branching, and circular judgment.
Sequential execution is the execution of commands in the order of address content.
Conditional branching is the execution of instructions at any address based on conditions.
Loop refers to the repeated execution of instructions at the same address.
Usually, sequential execution is relatively simple, with a program counter value of+1 each time an instruction is executed. Conditional branches and loop branches make the program counter point to any address, so that the program can return to the previous address to repeat the same instruction, or jump to any other instruction.
Below, we will use conditional branching as an example to illustrate the execution process of a program:

The startup process of the program is the same as the sequential process, and the sequential process of the program is the same as the startup process. The CPU starts executing commands from 0100. It executes sequentially in 0100 and 0101. The value of PC is in the order of+1. When executing the instruction at address 0102, it is determined that the value of register 0106 is greater than 0 and jumps to the instruction at address 0104. Then input the value into the display, end the program, and skip the command of 0103. This is the same as the if() judgment in our program. If the condition is not met, the instruction is usually skipped directly. Therefore, the execution of the PC is not directly+1, but the address of the next instruction.
09 Flag Register
Conditional and looping branches will use a jump (jump instruction), which will determine whether to jump based on the current instruction. We mentioned the flag register above. Regardless of whether the operation result of the current cumulative register is positive, negative, or zero, the flag register will be saved. When the CPU performs an operation, the value of the flag register will be automatically set based on the current operation result, and the positive, negative, and zero states of the operation result are represented by the three bits of the flag register.
When the corresponding results of the first, second, and third byte bits of the flag register are all 1, they represent positive, zero, and negative numbers, respectively.

Flag register
The execution mechanism of the CPU is very interesting. Assuming that XXX stored in the accumulation register is compared to YYY stored in the general register. Behind the comparison, the operating mechanism of the CPU will perform subtraction operations. Regardless of whether the result of the subtraction operation is positive, zero, or negative, it will be stored in the flag register. A positive result indicates that XXX is greater than YYY, a result of zero indicates that XXX and YYY are equal, and a negative result indicates that XXX is less than YYY. The program comparison instruction is actually a subtraction operation within the CPU.
10 Function Call Mechanism
Function calls and conditional branches have different loop mechanisms, and simple jump instructions cannot achieve function calls. Function calls need to be processed internally before the processing process returns to the function call point (the next address of the function call instruction). Function call processing is achieved by setting the value of the program counter to the memory address of the function.
11 Implementing Arrays through Address and Index
Next are the base register and index register, which can be used to partition specific areas on the main memory to achieve array like operations. Firstly, the address of 000000 FFFFFFFFFF in computer memory can be divided into hexadecimal numbers. In this way, for any memory address within this range, as long as there is a 32-bit register, all addresses can be viewed. However, if you want to partition a specific memory area for continuous viewing, such as an array, it is more convenient to use two registers. For example, we use two registries to represent memory values.
This representation is very similar to the structure of an array. An array refers to a data structure that sequentially arranges data of the same length in memory. Use the array name to represent all values of the array, and distinguish each data element of the array by index. For example, a [0] - a [4], 0-4 in [] is the subscript of the array.
12 CPU instruction execution process
Having said so much, how does the CPU execute instructions one after another? Almost all CPUs in von Neumann computers can work in five stages: instruction acquisition, instruction decoding, instruction execution, data access, and result writing back. The instruction acquisition stage is the process of reading instructions from memory into registers in the CPU, where program registers are used to store the address of the next instruction.
After the instruction extraction is completed, it immediately enters the instruction decoding stage. In the instruction decoding stage, the instruction encoder splits and interprets the extracted instructions based on the pre instruction format, and identifies and distinguishes different instruction categories and various methods to obtain operands;
he task of the execution instruction stage is to complete various operations specified by the instruction, and specifically implement the function of the instruction;
The task of the access fetch stage is to obtain the address of the operand in the main memory based on the instruction address code, and read the operand from the main memory for operation;
Result Writeback Stage: As the final stage, the operation result data of the execution instruction stage is "written back" to a certain storage form: The result data is usually written to the internal register of the CPU, so that subsequent instructions can access it quickly.
Tel
