Location:Home Page > Archive Archive

12 hardcore haberdashery about processor


A programmer spends countless days working with computers. Whether you're playing with hardware or creating software, core of a computer - CPU - is indispensable in your world.

01 What is a processor?

The relationship between a processor and a computer is similar to relationship between a brain and a human. This is a small computer chip, usually built into a computer's motherboard. Processors are built by placing billions of tiny transistors on a single computer chip. These transistors allow it to perform calculations needed to run programs stored in system memory, so you could also say that processor determines your computer's processing power.

02 What is CPU actually doing?

The core of CPU is to receive instructions from programs or applications and perform calculations. There are three key steps in this process: fetch, decode, and execute. The CPU first fetches instruction from system RAM, then decodes actual content of instruction, and finally executes instruction with appropriate part of CPU.

03 CPU internal structure

I just mentioned importance of a large number of processors. So, what is internal structure of a processor? And what is it made of? The figure below shows process of executing a general program (taking C language as an example). Generally speaking, understanding process of program execution is basis and prerequisite for mastering mechanism of program.

12 hardcore haberdashery about processor

In this process, CPU is responsible for interpreting and executing content, which is eventually translated into machine language. The CPU mainly consists of two parts: a control unit and an arithmetic logic unit (ALU).

Control unit: retrieving instructions from memory, decoding them and executing them;

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

The CPU and memory are electronic components made up of many transistors that can be compared to heart and brain of a computer. It receives input, executes instructions and processes related information, and communicates with input/output (I/O) devices that send and receive data to and from CPU.

From a functional point of view, content of a CPU consists of four parts: registers, controllers, arithmetic units, and clocks, with various parts connected by electrical signals.

Next, let's briefly introduce memory. Why do we need to talk about memory when talking about processor? Since memory is bridge to communicate with CPU, all programs on computer run in memory. Memory is commonly referred to as main memory and its function is to store computational data in CPU and exchange data with external storage devices such as hard drives.

When computer is running, CPU transfers data to be calculated to main memory for calculation. After operation is completed, CPU transmits result, and operation of main memory also determines stable operation of computer. Main memory is usually connected to CPU via a control chip and consists of read/write elements, and each byte has an address number.

The CPU reads data and instructions from main memory at an address, and can also write data at an address. Note: When computer is turned off, instructions and data in memory will also be cleared.

04 The CPU is a collection of registers

Among four CPU structures, registers are much more important than other three, why do you say that? Because programs usually describe registers as objects. When it comes to registers you should be talking about assembly language and when you are talking about assembly language you should be talking about high level languages ​​and when you are talking about high level languages ​​you should be talking about concept of a language. .

05 Computer language

The oldest and most direct means of communication between humans is language, but in order to communicate with a computer, it is necessary to exchange it according to computer instructions, which is associated with language problems. The earliest to solve problem of communication between computers and humans was assembly language. However, assembly language is difficult to understand, so high-level languages ​​such as C, C++ and Java have emerged, so programming languages ​​are usually divided intoLow-level languages ​​and high-level languages. . A program written in a high-level language can only be run after being compiled and converted to machine language, while assembly language can only be converted to machine language through assembler.

06 Assembly Language

Let's first look at assembly code listing:

12 hardcore haberdashery about processor

This is part of writing assembly language programs that uses mnemonics to write programs, and each machine language instruction, which is originally an electrical signal, has a corresponding mnemonic. For example, mov and add are shorthand for storing data (moving) and appending (adding).

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, which is called assembler. In contrast, process of converting machine language into assembly language is called disassembly.

Assembly language can help you understand what a computer is doing. Programs at machine language level are processed using registers. eax and ebp in above code are registers which are names of internal CPU registers. Thus, we can say that processor is a set of registers.

Typically, storage in memory is denoted by an address number, and register types are distinguished by names. For these different types of CPU, type and number of internal registers and range of values ​​stored in registers also differ. However, according to different functions, we can divide registers into following categories:

12 hardcore haberdashery about processor

Among them, there is only one program counter, flag register, accumulation register, instruction register, and stack register, and usually there are several other registers.

07 Program counter

The program counter is used to store address of device where next instruction resides. When executing program, initial value of PC is used as address of first instruction of program. When program is sequentially executed, controller first selects instruction from memory according to instruction address specified by program counter, and then parses and executes instruction, and at same time adds 1 to value PC to point to next instruction to be executed.

We can take a closer look at process of running program counter with an example:

12 hardcore haberdashery about processor

This is an append operation. When program is run, after compilation and analysis, program on hard disk will be copied into memory via operating system.

The example program above is to add 123 and 456 and then display result because it's hard to describe in machine language, so these are translated results.

Actually, each instruction and data can be allocated to different addresses, but for better illustration, memory and data that make up an instruction are located at a memory address.

Address 0100 is starting position of program. After Windows and other operating systems copy program from hard disk to memory, program counter will be set to starting position of 0100, after which program will be executed. one instruction at a time, after which value of program counter will increase by 1, or directly point to address of next instruction.

The CPU then reads instruction from memory and executes it according to value of program counter. In other words, program counter controls flow of program.

08 Conditional branching and loop mechanism

All my friends have learned high-level languages. Conditional control processes described in high-level languages ​​are mainly divided into three types: sequential execution, conditional branching, and loop evaluation.

Sequential execution consists of executing commands according to order of contents of address.

● Conditional jumps are instructions that execute arbitrary addresses according to conditions.

Loops are instructions that repeatedly execute same address.

Under normal circumstances, sequential execution case is relatively simple, and program counter value is +1 each time instruction is executed. Conditional and loop jumps cause program counter to point to an arbitrary address, so that program can return to previous address to repeat same instruction or jump to any other instruction.

Below, we will use conditional branch as an example to illustrate flow of program execution:

12 hardcore haberdashery about processor

The program startup process is same as sequence flow, and program sequence flow is same as startup process. The CPU starts executing instructions at 0100 and executes them sequentially at 0100 and 0101, and PC value is +1. When executing instruction at address 0102, it detects that value of register 0106 is greater than 0 and jumps to instruction at address 0104. Then enter value at display and then end program and instruction 0103 will be skipped. This is same as if() decision in our program: if condition is not met, statement is usually skipped immediately. Therefore, PC execution process is not directly +1, but address of next instruction.

09 Mark Registration

The loop conditions and jumps will use a jump (jump instruction) that will determine whether to jump according to current instruction. We mentioned flag register above, no matter if result of operation of current accumulation register is positive, negative or zero, Registers flag will keep it. When CPU is performing calculations, value of flag register will be automatically set according to result of current calculation. The positive, negative, and zero states of calculation result are represented by three bits of flag register. When results of first byte bit, second byte bit, and third byte bit of flag register are 1, they represent positive numbers, zeros, and negative numbers, respectively.

12 hardcore haberdashery about processor

The CPU execution mechanism is quite interesting. Let's assume that XXX stored in accumulation register is compared with YYY stored in general register. After comparison is done, CPU working mechanism will perform subtraction operation. . Whether result of subtraction operation is a positive number, zero, or a negative number, it will be stored in 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 less than YYY. The program comparison instruction is actually a subtraction operation within CPU.

10 Mechanism for calling a function

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

12 hardcore haberdashery about processor

12 hardcore haberdashery about processor

11 Array by address and index

Next are base address register and index register. With these two registers, a specific area of ​​main memory can be divided in order to perform an array-like operation. First, you can use hexadecimal numbers to separate addresses 00000000 - FFFFFFFF in computer memory. So for any memory address in that range, if there is a 32-bit register, all addresses can be looked up. However, if you want to divide a certain area of ​​memory for continuous viewing, like an array, it is more convenient to use two registers. For example, we use two registers to represent a memory value.

12 hardcore haberdashery about processor

This view is very similar to array structure. An array refers to a data structure in which data of same length are contiguous in memory. Use an array name to represent all array values, and distinguish each array data element via an index, for example: a[0] - a[4], 0 - 4 in [] is index of array.

12 The process of executing processor instructions

Not to mention how does CPU execute instructions one after other? 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 results. The instruction fetch stage is process of reading an instruction from memory into a CPU register, and program register is used to store address of next instruction;

After instruction fetch is complete, instruction decode phase immediately begins. In instruction decoding step, instruction encoder parses and interprets received instruction according to format preceding instruction. distinguish between different categories of instructions and different methods for obtaining operands;

The task of instruction phase is to perform various operations specified in instruction and implement function of instruction;

The task of access stage is: by address code of instruction, get address of operand in main memory, and read operand from main memory to work;

● Result writeback stage In last step, result data of operation of execution instruction step is "written back" to some form of storage: result data is often written to an internal CPU register so that it can be quickly read by subsequent access instructions.