Article count:1382 Read by:1966155

Account Entry

Bottom layer | Knowledge points about CPU

Latest update time:2024-08-21
    Reads:

Click on the blue " Linux " in the upper left corner and select " Set as Star "

As a programmer, you have been dealing with computers for countless days. No matter you are playing with hardware or doing software, your world is naturally inseparable from the core of the computer - the CPU.
01
What is CPU?
The relationship between CPU and computer is equivalent to the relationship between brain and human. It is a small computer chip that is 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 needed to run programs stored in the system's memory, so it can be said that the CPU determines the computing power of your computer.
02
What does the CPU actually do?
The core working principle of the CPU is to take instructions from a program or application 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 contents of the instruction, and finally the relevant part of the CPU executes the instruction.
03
Internal structure of CPU
We have just mentioned the importance of CPU, so what is the internal structure of 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 program's running mechanism.
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: 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 the computer.
It receives data input, executes instructions, and processes related information. It communicates with input/output (I/O) devices, which send and receive data to and from the CPU.
From a functional point of view, the CPU consists of four parts: registers, controllers, arithmetic units and clocks, and each part is connected by electrical signals.
Next, let me briefly introduce memory. Why do we need to talk about memory when talking about CPU?
Because memory is the bridge for communication with the CPU, all programs in the computer are executed in the memory.
Memory is generally referred to as main memory. 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 will transfer the data that needs to be calculated to the main memory for calculation.
After the calculation is completed, the CPU transmits the result. The operation of the main memory also determines the stable operation of the computer.
The main memory is generally connected to the CPU through a control chip and consists of readable and writable elements, 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 according to the address. Please note that when the computer is turned off, the instructions and data in the memory will also be cleared.
04
The CPU is a collection of registers
Among the four structures of the CPU, the importance of registers is far higher than the other three. Why? Because programs usually describe registers as objects.
When talking about registers, we have to talk about assembly language, and when talking about assembly language, we have to talk about high-level language, and when talking about high-level language, we have to mention the concept of language.
05
Computer Language
The oldest and most direct medium of communication between people is language, but when communicating with computers, words must be exchanged according to computer instructions, which involves language issues.
Initially, assembly language appeared 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 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 into machine language, and 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 programming in assembly language, which uses mnemonics to program. Each machine language instruction, which is originally an electrical signal, will have a corresponding mnemonic.
For example, mov and add are abbreviations for data storage (move) and addition (addition) respectively.
Assembly language and machine language correspond one to one, which is different from high-level languages. We usually call the process of converting programs written in assembly language into machine language assembly.
Conversely, 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 represent registers, which are the names of the CPU's internal registers.
Therefore, it can be said that the CPU is a collection of a series of registers.
Generally, storage in memory is represented by address numbers, and register types are distinguished by names.
Different types of CPUs 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, accumulator 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 PC is used as the address of the first instruction of the program. When the program is executed sequentially, the controller first takes out an instruction from the memory according to the instruction address indicated by the program counter, then analyzes and executes the instruction, and at the same time increases the value of PC by 1 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 starts, after compilation and parsing, the program in the hard disk will be copied to the memory through the operating system.
The above example program adds 123 and 456 and then outputs the result to the display. Because it is difficult to describe using machine language, these are all 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 of the program. After Windows and other operating systems 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 value of the program counter will increase by 1, or directly point to the address of the next instruction.
Subsequently, the CPU will read the instructions from the memory and execute them according to the value of the program counter. In other words, the program counter controls the flow of the program.

08

Conditional branching and looping mechanisms
My 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 the execution of commands in the order of the address contents.
  • Conditional branches are instructions that execute at any address based on a condition.
  • A loop is the repetition of instructions at the same address.
Generally speaking, the sequential execution situation is simpler, and the value of the program counter is +1 each time an instruction is executed.
Conditional and loop branches cause the value of 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 take 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 executing commands from 0100, and executes sequentially in 0100 and 0101. The value of PC is incremented by 1. When the instruction at address 0102 is executed, it determines that the value of register 0106 is greater than 0, jumps to the instruction at address 0104, inputs the value to the display, and then ends the program. The instruction at address 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 PC is not directly increased by 1 during execution, but is the address of the next instruction.

09

Flag register
Conditional and loop branches will use jump instructions, and will determine whether to jump based on the current instruction. We mentioned the flag register above. Regardless of whether the current accumulator register's calculation result is positive, negative, or zero, the flag register will save it.
When the CPU is performing calculations, the value of the flag register is 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 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 XXX stored in the accumulator register is compared with YYY stored in the general register. Behind the comparison, the CPU's calculation mechanism will perform a subtraction operation.
Regardless of whether the result of the subtraction operation is positive, zero, or negative, it will be saved in the flag register.
A positive result indicates that XXX is greater than YYY, a zero result indicates that XXX and YYY are equal, and a negative result indicates that XXX is less than YYY. The comparison instruction of the program actually performs subtraction operation inside the CPU.

10

Function call mechanism
Function calls and conditional branches have different loop mechanisms, and simple jump instructions cannot implement function calls.
The function call needs to be processed inside the function, and then the processing flow returns to the function call point (the next address of the function call instruction).
The function call process is implemented by setting the value of the program counter to the storage address of the function.

11

Implementing arrays through addresses and indices
Next are the base register and the index register . These two registers can be used to divide specific areas on the main memory to implement array-like operations.
First, you can use hexadecimal numbers to divide the addresses 00000000 - FFFFFFFF on the computer memory.
In this way, all memory addresses in this range can be viewed as long as there is a 32-bit register.
However, if you want to split a specific memory area like an array to achieve the purpose of continuous viewing, it will be 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 construction of an array. An array refers to data of the same length that is arranged continuously in memory.
The array name is used to represent all the values ​​in the array, and the index is used to distinguish the various data elements of the array, for example: a[0] - a[4], 0 - 4 in [] is the subscript of the array.

12

CPU instruction execution process
So, how does the CPU execute instructions one by one? The CPU of almost all von Neumann computers can be divided into five stages: fetching instructions, decoding instructions, executing instructions, accessing memory, and writing results back.
The instruction fetch stage is the process of reading instructions from the memory into the registers in the CPU. The program registers are 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-set instruction format, and identifies and distinguishes different instruction categories and various methods of obtaining operands;

  • The task of the instruction execution stage is to complete the various operations specified by the instruction and specifically realize the function of the instruction;
  • The task of the access data fetching 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 calculation;
  • The result write-back stage is the last stage, which "writes back" the running result data of the instruction execution stage to some storage form: the result data is often written to the internal registers of the CPU so that it can be quickly accessed by subsequent instructions.


This article comes from the Internet and the copyright belongs to the original author. If there is any copyright issue, please contact me to delete it.

end



A bite of Linux


Follow and reply【 1024 】 to get a large amount of Linux information


Collection of wonderful articles

Recommended articles

【Album】 ARM
【Album】 Fan Q&A
【Album】 All original
Album Getting started with Linux
Special Computer Network
Album Linux Driver



Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号