As a programmer, these hardcore KNOWLEDGE of CPUS you must know!

The CPU is both familiar and unfamiliar to every programmer, right?

If you only know that the CPU is the central processing unit (CPU), that’s not going to help you, so as programmers, we need to understand how the CPU works, and especially how the registers work, because that will give you an underlying understanding of how the program works.

Come with me and get to know the CPU

Let’s break the CPU apart

For CPU, we first must understand what it is, its internal structure, of course, the CPU so cow a thing, must be a very complicated structure, after all, is a high science and technology, so, of what we said today, is refers to the main, programmers are useful for us, that part of the need to understand, not to wrangle oh

First of all, I want you to remember this:

The CPU is responsible for interpreting and running the content of the program that is eventually translated into machine language

In addition, speaking of CPU, must leave the memory, in today’s introduction, also simple about memory, because later will separate out an article about the memory, after all of these is very important for our programmers, so often in a piece of CPU and memory, they have a lot of transistor, is commonly called the IC, Which means integrated circuit.

Now let’s look at the components of the CPU, and notice what I said before, the components here are usually the major components, because cpus are complex.

We need to know the CPU structure

The important thing to focus on is registers

Arithmetic unit

It is a data processing plant that performs operations on the data in the registers. These operations include basic arithmetic and logic operations.

Then for the computing machine, we need to understand several important roles, and I will introduce each of them:

Arithmetic Logic Unit (ALU)

This is an important component of the arithmetic unit, mainly responsible for the processing of data, so as to realize the arithmetic and logical operation of data

Cumulative register (AC)

It’s usually called an accumulator, it’s a general purpose register, and what does that do? So the ALU, when the ALU does the arithmetic or the logic, it gets a result, and that result is stored in AC

Data Buffer Register (DR)

This DR also stores data, mainly stored data read from memory, will be temporarily stored in this DR, here is equivalent to memory and CPU do data transfer between a transfer station, why there is this, everyone is to CPU and memory speed is very different, in the middle to introduce a DR, To a certain extent, it has a buffer effect on the speed, so that the speed difference is not too fierce

State Condition register (PSW)

First of all, registers are used to store data, so PSW naturally stores a certain kind of data. It mainly stores a variety of conditional code content established by arithmetic instructions and logic instructions running or testing results, generally divided into status flags and control flags, as long as you know

We need to understand all of these components, which are the core of an arithmetic unit, and we need to mention that each of them has an acronym, ALU, which stands for arithmetic logic unit. That’s why we usually talk about ALU rather than arithmetic logic unit, so it’s important to remember their abbreviations as well.

Otherwise, when you communicate with others, they will say ALU, DR and so on, you can easily lose track of the rhythm.

With a basic understanding of the processors in the CPU, let’s look at another important component, the controller

The controller

Control control, that is, to play a control role, the above introduction of the arithmetic unit just played the function of calculation, but the controller is more cattle, it is to control the entire CPU work, for the controller, generally to be able to ensure the correct execution of the program and can handle abnormal events

Similarly, for the controller, we need to focus on the following:

Instruction register (IR)

Registers are for storing things, instruction registers are for storing instructions, so for the CPU to execute an instruction, it has to read it from memory, put it in the data buffer, put it in the DR, put it in the IR, and then on, There’s something called an instruction decoder that generates a lot of micromanipulation instructions based on the IR content to control what other components have done

Program counter (PC)

Should understand this learned Java memory structure, it holds the next instructions to be executed, that is to say, when a program to be executed, will place the program start address to the PC, don’t worry if the instruction is executed, the CPU will automatically modify, makes the PC to store the next instructions to be executed

Address register (AR)

This is to save the CPU access memory on the memory of the memory address, why to save this, because the CPU and memory speed difference is relatively large, so the CPU to save the address of the memory is accessing the memory, until the memory read and write operation is completed, otherwise, can not find before the access is which memory unit ah

Instruction decoder (ID

The simple point is to do an analysis of the instruction, to see what your instruction to do

I don’t know if you have noticed that there is a lot of mention of registers when we introduce arithmetic or controller. This is enough to show the importance of registers. There is a saying that the CPU is a collection of registers, but before we talk about registers, it is necessary to take a simple look at memory.

A quick word about memory (I’ll talk about it separately later)

CPU and memory are always inseparable, here a simple memory to sit down to introduce, convenient for the understanding of CPU, memory is naturally a storage component, what is stored? Generally speaking, it’s data and instructions, and when we talk about memory, we refer to the memory bars in the computer, which are used to store programs, data, instructions, and so on.

Image point, in fact, memory can be seen as a small grid, each grid is used to store data, and each grid has a number, that is, the memory address, according to it can get the corresponding memory storage data, but also need to know that, if the power, the data in memory will be cleared.

Most of the CPU is registers

It says the CPU is composed of arithmetic unit and controller of two parts, in fact both arithmetic unit and controller, cannot leave the registers, it can be said that the CPU is the register of the world, therefore, for the CPU, must grasp the register, the CPU to do some of the operation is done by register, can take a look at this article: How does a program run on a computer?

Remember one sentence:

Java, Python, all of these are high level languages, and you write programs that will eventually be translated into machine language, and then you’ll get into the CPU, and you’re essentially doing it through registers inside the CPU

register

This is what we need to master, registers this thing, different CPU contains the number of registers and the type of registers are different, but generally there are several registers, according to the function of the division

In addition, you can think of the CPU as a collection of registers, and then you need to know that there is only one program counter, accumulator, flag, instruction, and stack register, and there are usually multiple other registers.

Talk about this program counter

Why do I say that? Because that’s what’s going on in the program. Let’s take a look at that

Look at this, for example, this represents the operation of the two Numbers together, about this operation involves data and instructions are stored in the memory, and each data and instructions in the memory units are numbered, also is the memory address, 1, 0100010 is the picture from top to bottom is the performance of the operating sequence.

As mentioned above, a program counter is a store of the instructions executed by the program. To be precise, it is a store of the address value of the instructions executed by the program. What that means is that every step of the program, at the bottom level, has a set of instructions that indicate what that step is going to do, for example, the a+b operation, when it’s loaded into memory, has a set of instructions.

The first is a command, it shows that to do a, this value addition operation, need to make a save to a register, and the instructions are stored in the memory of a memory unit, the memory address is 0100, and 0100 at this point the program counter is to preserve the memory address, to execute a program, The CPU looks at the first address stored in the program counter, and finds that it is 0100. Then it goes to the memory to see what is stored in the 0100 location. It looks like an instruction.

So, the program counter actually manages the execution flow of the program

How do cpus compare

And this is something to understand, because when you compare cpus you’re actually subtracting internally. What does that mean? For example, to compare a and B, it is to do an internal operation a-B, and then compare a and B according to the result obtained. This must be done with the help of the register, for example, the result will be put into the flag register.

What the CPU does is very simple

As we mentioned in the previous article, the CPU actually does four things

  1. Read data from memory and put it in a register
  2. To write data from a register to memory
  3. Perform mathematical AND logical operations (addition, subtraction, multiplication, division, AND,OR)
  4. Execute another instruction (one instruction jumps to another instruction) according to the corresponding condition.

We know that the CPU does things according to instructions, why the CPU only does the above four things, the main thing is to see what machine language instructions the CPU can execute, the main thing is the following

At this point, what a CPU can do is not that complicated.

CPU instruction execution process

After the above explanation, we should know that the CPU is to see instructions to do things, so about the instructions, the CPU is how to operate? What we need to know is that the work of almost all von Neumann cpus can be divided into five steps: fetching instructions, decoding instructions, executing instructions, accessing numbers, and writing results back.

Maybe you don’t understand, no what, in fact, is not the five steps, make sure they are ok, let’s take a look at:

First: fetch instruction

It means to get the instruction that you need, but it has to be in memory, so you need to get the instruction out of memory, and then you need to put it in a register in the CPU, and that’s how you get the instruction.

Second: instruction decoding

This is not very quick to think of our above said instruction decoder, this is not to the previous step to get the instruction to do analysis, why to analysis, I have to see you this instruction to do what ah, otherwise how the CPU work

Third: execute instructions

So it’s a little bit easier to understand, so it’s time to start working, to implement the corresponding function

Fourth: access to the number

Intuitively seems not so good understanding, in fact, that is, we according to the above operations, may need to fetch data from memory, w what you mean, you know, the memory to store data and instructions, such as the implementation of a + b, we get together instructions, for a and b are operands, they are stored in the memory, Do you need to go to memory to get them

Look at the figure above, the command corresponding to 0100 is not to go to the address corresponding to 0105 to get data 123, this is not access to fetch the number.

Fifth: Write back the results

What is this? After the above steps, the calculation must have a result, how to do this result, you have to save where ah, usually saved in the CPU internal register, usually the flag register, this is not the result of writing back

Ok, here, about the KNOWLEDGE of the CPU we have introduced about, of course, the CPU knowledge is much more than that, but as a programmer, you should at least know this ah.

Thank you for reading

Hi, I’m ithuangqing, along the way, a lot of learning experience and methods, learning resources and collected a large number of high-quality goods, now maintains a public 】 【 coding, implied meaning is outside the code should also constantly learning, mainly to share Java technology related to the original article, now mainly in the write data structure and algorithm, Computer foundation, thread and concurrent and virtual machine this piece of the original, in addition to the small white also serialized a set of “small white Java self-study class”, and strive to understand, from the simple to the deep. At the same time, I am also a tool lover and often share some efficient black technology tools and websites.

By the way, the public number also shared a lot of my learning experience, can be discussed together!

Follow the public account, reply “Qingge” in the background, and send the latest Java self-study resources immediately in 2020! More original wonderful in [coding]

Insert the picture description here