A piece of hard core knowledge about CPU

Whether you play hardware or software, your world cannot do without the core of the computer - the CPU.

01What is a CPU?

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 system memory, so, it can also be said that the CPU determines the computing power of your computer.

02 What does the CPU actually do?

At the heart of a CPU's work is getting instructions from a program or application and performing calculations. There are three key stages in this process: fetch, decode, and execute. The CPU first 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.

03CPU internal structure

I just mentioned the importance of a lot of CPUs, so what is the internal structure of the CPU? And what is it made of? The figure below 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 prerequisite for mastering the operating mechanism of a program.

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: extract instructions from memory and decode and execute them;
  • Arithmetic Logic Unit (ALU): Handles arithmetic and logic 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 receives data input, executes instructions, and processes related information, and it communicates with input/output (I/O) devices that send data to and receive data 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 the various parts are connected by electrical signals. Next, let's briefly introduce the memory. Why do we need to talk about the memory when we talk about the CPU? Because the memory is a bridge to communicate with the CPU, all programs in the computer are run in the memory. The memory is generally called the main memory, and its function is to store the calculation data in the CPU and the data exchanged with external storage devices such as hard disks. When the computer is running, the CPU transfers the data that needs to be calculated to the main memory for calculation. 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: when the computer is turned off, the instructions and data in the memory will also be cleared.

04CPU is a collection of registers

Among the four structures of the CPU, the importance of registers is much higher than that of the other three. Why do you say that? Because programs usually describe registers as objects. When it comes to registers, you have to talk about assembly language, and when you talk about assembly language, you have to talk about high-level languages, and when you talk about high-level languages, you have to mention the concept of language.

05 Computer language

The oldest and direct communication medium between people is language, but to communicate with computers, they must exchange according to computer instructions, which involves language issues. At the earliest, in order to solve the problem of communication between computers and humans, assembly language appeared. However, assembly language is difficult to understand, so high-level languages ​​such as C, C++, and Java have appeared. Therefore, computer languages ​​​​are generally divided into low-level languages ​​and high-level languages. A program written in a high-level language can only be run after being compiled and converted into machine language, while assembly language can only be converted into machine language through an assembler.

06 assembly language

Let's first look at a code listing expressed in assembly language:

This is part of writing programs in assembly language, which uses mnemonics to write programs, and each machine language instruction that is originally an electrical signal has a corresponding mnemonic. For example, mov and add are shorthand for data storage (move) and addition (addition). Assembly language and machine language correspond one-to-one, which is different from high-level languages. We usually convert programs written in assembly language into machine language, which 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 registers, which are the names of the internal registers of the CPU. Therefore, it can be said that the CPU is a collection 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. For those different types of CPUs, the type and number of internal registers and the range of values ​​stored in the registers are also different. However, according to different functions, we can divide registers into the following categories:

Among them, there is only one program counter, flag register, accumulation register, instruction register and stack register, and there are generally several other 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 of the program. When the program is executed 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. And at the same time, add 1 to the value of PC to point to the next instruction to be executed.

We can take a closer look at the execution process of the program counter through an example:

This is an addition operation. When the program is started, after compiling and analyzing, the program in the hard disk will be copied to the memory through the operating system. The above sample program is to add 123 and 456, and then output the result to the display. Because it is difficult to describe in machine language, these are the translated results. In fact, each instruction and data may be distributed in different addresses, but for better illustration, the memory and data that make up an instruction are placed in a memory address. Address 0100 is the starting position of the program. After Windows and other operating systems copy the program from the hard disk to the memory, it will set the program counter as the starting position 0100, and then execute the program. After each instruction is executed, the program The value of the counter will increase by 1, or directly point to the address of the next instruction. Subsequently, the CPU will read the command from the memory and execute it according to the value of the program counter. In other words, the program counter controls the flow of the program.

08 Conditional branch and loop mechanism

The friends have all learned high-level languages. The conditional control processes summarized in high-level languages ​​are mainly divided into three types: sequential execution, conditional branching, and loop judgment.

  • Sequential execution is to execute commands in order according to the content of the address.
  • A conditional branch is an instruction that executes an arbitrary address based on a condition.
  • A loop is an instruction that executes the same address repeatedly.

In general, the case of sequential execution is relatively simple, and the value of the program counter is +1 each time an instruction is executed. Conditional and loop branches cause the program counter to point to an arbitrary address, so that the program can return to a previous address to repeat the same instruction, or jump to any other instruction.

Below, we use the conditional branch as an example to illustrate the execution process of the program:

The program start process is the same as the sequence flow, and the program sequence flow is the same as the start process. The CPU starts to execute commands from 0100, and executes them sequentially in 0100 and 0101, and the value of PC is +1. When executing the instruction at address 0102, it judges that the value of register 0106 is greater than 0, and jumps to the instruction at address 0104. Then input the value to the display, and then end the program, and the instruction of 0103 is skipped. This is the same as the if() judgment in our program. If the condition is not met, the instruction will generally be skipped directly. Therefore, the execution process of the PC does not directly +1, but the address of the next instruction.

 

 Information through train: Linux kernel source code technology learning route + video tutorial kernel source code

Learning through train: Linux kernel source code memory tuning file system process management device driver/network protocol stack

09 flag register

Conditional and loop branches will use jump (jump instruction), which will judge whether to jump according to the current instruction. We mentioned the flag register above. No matter whether the operation result of the current accumulation register is positive, negative or zero, the flag register will be its saved. When the CPU is performing calculations, the value of the flag register will be automatically set according to the result of the current calculation. The positive, negative and zero states of the calculation result are represented by the three bits of the flag register. When the results of the first byte bit, the second byte bit, and the third byte bit of the flag register are all 1, they represent positive numbers, zeros, and negative numbers, respectively.

The execution mechanism of the CPU is quite interesting. Suppose the XXX stored in the accumulation register is compared with the YYY stored in the general-purpose register. Behind the execution of the comparison, the CPU's operation mechanism will perform a subtraction operation. Regardless of whether the result of the subtraction operation is a positive number, zero or a negative number, it will be stored in the flag register. A positive result means that XXX is greater than YYY, a zero result means that XXX and YYY are equal, and a negative result means that XXX is smaller than YYY. The program comparison instruction is actually a subtraction operation inside the CPU.

10 function call mechanism

The function call is different from the conditional branch and loop mechanism, and the simple jump instruction cannot realize the function call. After the function call needs to be processed inside the function, the processing flow returns to the function call point (the next address of the function call instruction). The call processing of the function is realized by setting the value of the program counter as the storage address of the function.

11 Implementing arrays by address and index

Next is the base address register and the index register. Through these two registers, a specific area on the main memory can be divided to achieve an array-like operation. First, you can use hexadecimal numbers to divide the addresses of 00000000 - FFFFFFFF on the computer memory. In this way, as long as there is a 32-bit register for any memory address in this range, all addresses can be viewed. However, if you want to divide a specific memory area for continuous viewing like an array, it is more convenient to use two registers. For example, we use two registers to represent the value of the memory.

This representation is very similar to the structure of an array. An array refers to a data structure of the same length that is arranged consecutively in memory. Use the array name to represent all the values ​​of the array, and distinguish each data element of the array through the index, for example: a[0] - a[4], 0 - 4 in [] is the subscript of the array.

12CPU instruction execution process

Having said so much, how does the CPU execute instructions one by one? The CPU of almost all von Neumann-type computers can be divided into five stages: fetching instructions, decoding instructions, executing instructions, accessing data, and writing back the results. The instruction fetch stage is the process of reading the instruction in the memory to the register in the CPU, and the program register is used to store the address of the next instruction;

  • After the instruction fetching is completed, it immediately enters the instruction decoding stage. In the instruction decoding stage, the instruction encoder splits and interprets the fetched instructions according to the pre-instruction format, and recognizes and distinguishes different instruction types and various acquisitions. method of the operand;
  • The task of executing the instruction stage is to complete various operations specified by the instruction, and to realize the function of the instruction;
  • The task of the access stage is: according to the instruction address code, obtain the address of the operand in the main memory, and read the operand from the main memory for calculation;
  • As the last stage, the result write-back stage "writes back" the operation result data of the instruction execution stage to some storage form: the result data is often written into the internal register of the CPU for quick access by subsequent instructions.

Author of the original text: [ Learn Embedded Together ]

 

Guess you like

Origin blog.csdn.net/youzhangjing_/article/details/132049436