Computer | 12 knowledge points about CPU (detailed explanations with pictures and texts)

What is a CPU?

The relationship between the CPU and the computer is the same as the relationship between the brain and the human being. It is a small computer chip, usually embedded in 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 your computer's computing power.

What does the CPU actually do?

The core of the CPU's work is to obtain instructions from programs or applications and perform calculations.

There are three key stages in this process: extraction, decoding and execution.

The CPU first fetches the instruction from the system's RAM, then decodes the actual content of the instruction, and finally the relevant part of the CPU executes the instruction.

Internal structure of CPU

I just mentioned a lot of the importance of the CPU, so what is the internal structure of the CPU? What does it consist 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 running mechanism of the program.

In this process, the CPU is responsible for interpreting and running the content that is ultimately converted into machine language. The CPU is mainly composed of two parts: the control unit and the arithmetic logic unit (ALU).

  • Control unit: retrieve instructions from memory and decode them for execution;

  • Arithmetic Logic Unit (ALU): handles arithmetic and logical operations.

Both 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. 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. Each part is connected by power signals.

Next, let’s briefly introduce memory. Why do we need to talk about memory when talking about CPU?

Because the memory is the bridge that communicates with the CPU, all programs in the computer are run in the memory.

Memory is generally called main memory. Its function is to store computing data in the CPU and 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 calculation.

After the operation is completed, the CPU transmits the results, and the operation of the main memory also determines the stable operation of the computer.

Main memory is generally connected to the CPU through a control chip and is composed of readable and writable elements. 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 shut down, the instructions and data in the memory will also be cleared.

CPU is a collection of registers

Among the four structures of the CPU, the importance of registers is much higher than the other three. Why is this said? Because programs usually describe registers as objects.

When talking about registers, we have to talk about assembly language. When talking about assembly language, we have to talk about high-level languages. When talking about high-level languages, we have to mention the concept of language.

Computer Languages

The oldest and direct communication medium between people is language, but to communicate with a computer, you must exchange according to computer instructions, which involves language issues.

At the earliest, assembly language appeared in order to solve the problem of communication between computers and humans.

However, assembly language is obscure and 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.

Programs written in high-level languages ​​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.

Assembly language

Let’s first look at a code listing expressed 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 will have a corresponding mnemonic.

For example, mov and add are the abbreviations of data storage (move) and addition (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. Machine language level programs are processed through registers. eax and ebp in the above code are all represented registers. They are the names of the internal registers of the CPU.

Therefore, it can be said that the CPU is a collection of registers.

Generally, storage in memory is represented by address numbers, and register types are distinguished by names.

Different types of CPUs also have different types and quantities of internal registers, as well as the range of values ​​stored in the registers.

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.

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 pointed out by the program counter, and then analyzes and executes the instruction. And at the same time, the value of PC is increased by 1 to point to the next instruction to be executed.

You 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 compilation and analysis, the program in the hard disk will be copied to the memory through the operating system.

The above example program is to add 123 and 456, and then output the result to the display. Because it is difficult to describe using machine language, these are the translated results.

In fact, each instruction and data may be distributed at different addresses, but for better explanation, the memory and data that make up an instruction are placed at one memory address.

Address 0100 is the starting position for program execution. After operating systems such as Windows copy the program from the hard disk to the memory, they will set the program counter to the starting position 0100, and then execute the program. Each time an 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 reads the command from the memory and executes it according to the value of the program counter. In other words, the program counter controls the flow of the program.

Conditional branching and looping mechanisms

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 means executing commands in order according to the contents of the address.

  • A conditional branch is an instruction that executes an arbitrary address based on conditions.

  • A loop is the repeated execution of instructions at the same address.

Under normal circumstances, the situation of sequential execution is relatively simple. Each time an instruction is executed, the value of the program counter is +1.

Conditional and loop branches cause the program counter to point to an arbitrary address, so that the program can return to the previous address to repeat the same instruction, or jump to any other instruction.

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

The starting process and sequential flow of the program are the same, and the sequential flow and starting process of the program are the same.

The CPU starts executing commands from 0100. Both 0100 and 0101 are executed sequentially. The value of PC is sequentially + 1. When the instruction at address 0102 is executed, it is judged 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, then end the program, and the instruction 0103 will be 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 PC does not directly +1, but the address of the next instruction.

flag register

Conditional and loop branches will use jump (jump instruction), and will judge whether to jump based on the current instruction. Above we mentioned the flag register. Regardless of whether the operation result of the current accumulation register is a positive number, a negative number, or zero, the flag register will its preservation.

When the CPU performs an operation, the value of the flag register is automatically set according to the result of the current operation. The positive, negative, and zero states of the operation 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, zero, 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 register. Behind the execution of the comparison, the CPU's computing mechanism will perform a subtraction operation.

Regardless of whether the result of the subtraction operation is a positive number, zero, or negative number, it will be saved in the flag register.

A positive result means that XXX is greater than YYY, a zero result means that XXX and YYY are equal, a negative result means that XXX is smaller than YYY. The program comparison instructions actually perform subtraction operations inside the CPU.

Function calling mechanism

Function calls, conditional branches, and loop mechanisms are different, and simple jump instructions cannot implement function calls.

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).

Function call processing is implemented by setting the value of the program counter to the storage address of the function.

Implementing arrays by address and index

Next is the base address register and index register. Through these two registers, specific areas on the main memory can be divided to achieve array-like operations.

First, you can use hexadecimal numbers to divide the addresses 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 like an array for the purpose of continuous viewing, it will be more convenient to use two registers. For example, we use two registers to represent the memory value.

This representation is very similar to the structure of an array. An array refers to a data structure in which data of the same length is continuously arranged in memory.

Use the array name to represent all the 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.

CPU instruction execution process

Having said all that, how does the CPU execute instructions one by one? The work of almost all von Neumann computer CPUs can be divided into five stages: instruction fetching, instruction decoding, instruction execution, memory access, and result writing back.

The instruction fetching stage is the process of reading the instructions in the memory into the registers in the CPU. The program register is used to store the address of the next instruction;

  • After the instruction is fetched, 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 identifies and distinguishes different instruction categories and various acquisitions. Operand method;

  • The task of the instruction execution phase is to complete various operations specified by the instruction and specifically realize the function of the instruction;

  • The task of the access phase is to: obtain the address of the operand in the main memory according to the instruction address code, and read the operand from the main memory for operation;

  • The result write-back stage, as the last stage, "writes back" the running result data of the instruction execution stage to some storage form: the result data is often written to the internal register of the CPU so that it can be quickly accessed by subsequent instructions.

Guess you like

Origin blog.csdn.net/m0_59795797/article/details/129956241