075582814553
How much do you know about CPU?

FREE-SKY (HK) ELECTRONICS CO.,LIMITED / 06-09 14:42

01 What is 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 the system's memory. So, it can also be said that the CPU determines the computing power of your computer.

02 What does the CPU actually do?

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.

03 Internal structure of 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.

General program operation flow

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.

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

05 Computer 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.

06 Assembly language

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

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.

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 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:

program counter execution

program counter execution

This is an operation of adding, the program starts, and after compiling and parsing, the program in the hard disk will be copied to the memory through the operating system.

The above example program is to perform the addition operation of 123 and 456 and then output the result to the display. Because it is difficult to describe in machine language, these are the results after translation.

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

The address 0100 is the starting position of the program running. 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 execution of an instruction, the value of the counter will increase by 1, or directly point to the address of the next instruction.

Then, 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 conditional control flow summarized by the high-level language is mainly divided into three types: sequential execution, conditional branching, and loop judgment.

  • Sequential execution is the execution of commands in the order of the contents of the addresses.

  • Conditional branching is the execution of an instruction at 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 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:

Use conditional branch as an example to illustrate the execution process of the program

The starting process of the program is the same as the sequential flow, and the sequential flow of the program is the same as the starting process. The CPU starts to execute the command from 0100. It is executed sequentially in 0100 and 0101. The value of the PC is in the order of +1. When executing the instruction at the address of 0102, it is judged that the value of the 0106 register is greater than 0, and jumps to the instruction at the address of 0104. Then input the value to the display, 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 of the PC is not directly +1, but the address of the next instruction.

09 Flag register

Conditional and loop branches will use jump (jump instruction), which will determine 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 performs an operation, the value of the flag register will be automatically set according to the result of the current operation, and the positive, negative, and zero states of the operation result are represented by the three bits of the flag register.

When the respective 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.

flag register

flag register

The execution mechanism of the CPU is interesting. Suppose the XXX stored in the accumulation register is compared with the YYY stored in the general-purpose register. Behind the comparison, the CPU's operation mechanism will perform a subtraction operation. Regardless of whether the result of the subtraction operation is positive, zero, or negative, it will be stored in the flags register. A positive result indicates that XXX is larger than YYY, a result of zero indicates that XXX and YYY are equal, and a negative result indicates 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 and conditional branch, the loop mechanism is different, 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 function call processing is realized by setting the value of the program counter to the memory address of the function.

11 Implementing an Array by Address and Index

Next are the base address register and the index register, through which a specific area on the main memory can be divided to achieve array-like operations. First, the addresses on the computer's memory from 00000000 - FFFFFFFF can be divided out with hexadecimal numbers. In this way, for any memory address in this range, as long as there is a 32-bit register, all addresses can be viewed. However, if you want to divide a specific memory area for continuous viewings, 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 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.

12 CPU instruction execution process

Having said so much, how does the CPU execute instructions one by one? Almost all CPUs of von Neumann -type computers can work in five stages: instruction fetching, instruction decoding, executing instructions, accessing data, and writing back 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 fetch is completed, immediately enter 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 methods to get operand;

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

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

  • Result write-back stage: As the last stage, the operation result data of the execution instruction stage is "written back" 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.

  • The relationship between the CPU


Processed in 0.070228 Second , 23 querys.