The relationship between the CPU and the computer is equivalent to the relationship between the brain and the human being. It is a small computer chip, usually embedded on the motherboard of the computer. CPUs are built by placing billions of tiny transistors on a single computer chip. These transistors enable it to perform the calculations required to run programs stored in the system's memory. So, it can also be said that the CPU determines the computing power of your computer.
The core of the CPU's work is to fetch instructions from programs or applications and perform calculations. There are three key stages in this process: extraction, decoding, and execution. The CPU fetches the instruction from the system's RAM, then decodes the actual content of the instruction, and finally executes the instruction by the relevant part of the CPU.
Just mentioned a lot of the importance of the CPU, so what is the internal structure of the CPU? What is it composed of? The following figure shows the running process of a general program (taking C language as an example). Generally speaking, understanding the running process of a program is the basis and premise of mastering the running mechanism of a program.
The general program operation flow
In this process, the CPU is responsible for interpreting and running the content that is finally converted into machine language. The CPU is mainly composed of two parts: the control unit and the arithmetic logic unit (ALU).
Control unit: extracts instructions from memory and decodes them for execution;
Arithmetic Logic Unit (ALU): handles arithmetic and logical operations.
Both the CPU and memory are electronic components made up of many transistors, which can be likened to the heart and brain of a computer. It is capable of receiving data input, executing instructions, and processing related information, and it communicates with input/output (I/O) devices that send and receive data to and from the CPU.
From a functional point of view, the content of the CPU is composed of four parts: registers, controllers, arithmetic units, and clocks, and each part is connected by power-on signals.
Next, let’s briefly introduce memory. Why do we need to talk about memory when it comes to CPUs? Because the memory is the bridge to communicate with the CPU. All programs in the computer are run in the memory. Memory is generally called main memory, and its function is to store the operation data in the CPU and the data exchanged with external storage devices such as hard disks.
When the computer is running, the CPU will transfer the data that needs to be calculated into the main memory for operation. After the operation is completed, the CPU transmits the result, and the operation of the main memory also determines the stable operation of the computer. The main memory is generally connected to the CPU through the control chip and is composed of readable and writable elements, and each byte has an address number.
The CPU reads data and instructions from the main memory through the address, and can also write data according to the address. Note that when the computer is turned off, the instructions and data in the memory will also be cleared.
Among the four structures of the CPU, the importance of registers is much higher than the other three, why do we say that? Because programs usually describe registers as objects. When it comes to registers, we have to talk about assembly language, when it comes to assembly language, we have to talk about high-level languages, and when we talk about high-level languages, we have to mention the concept of language.
The most ancient and direct communication medium between people is language, but to communicate with a computer, it must be exchanged according to computer instructions, which involves the issue of language. At the earliest, in order to solve the problem of communication between computers and humans, assembly language appeared. However, assembly language is obscure and difficult to understand, so there are high-level languages such as C, C++, and Java. Therefore, computer languages are generally divided into low-level languages and high-level languages. Programs written in high-level languages can only be run after they are compiled and converted into machine language, while assembly language can be converted into machine language by assemblers.
Let's first look at a code listing in assembly language:
This is part of writing programs in assembly language. Assembly language uses mnemonics to write programs. Each machine language instruction that is originally an electrical signal has a corresponding mnemonic. For example, mov, add are shorthand for data storage (move) and addition, respectively.
Assembly language and machine language have a one-to-one correspondence, which is different from high-level languages. We usually convert programs written in assembly language into machine language. This process is called assembly. In contrast, the process of converting machine language into assembly language is called disassembly.
Assembly language can help you understand what the computer does. Programs at the machine language level are processed through registers. The eax and ebp in the above code are all registers, which are the names of the internal registers of the CPU. Therefore, it can be said that the CPU is a collection of a series of registers.
Generally, the storage in the memory is represented by the address number, and the type of the register is distinguished by the name. Those different types of CPUs have different types and numbers of internal registers, as well as the range of values stored in the registers.
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 of the program. When executing the program sequentially, the controller first fetches an instruction from the memory according to the instruction address indicated by the program counter, and then analyzes and executes the instruction. At the same time, the value of PC is incremented by 1 to point to the next instruction to be executed.
We can take a closer look at the execution of the program counter through an example: