As a programmer, you must know this hardcore knowledge of the CPU!

Is CPU a familiar and unfamiliar thing to every programmer?

If you only know that the CPU is a central processing unit, it may not be of much use to you. As programmers, what we must understand is how the CPU runs, especially the registers in it. This will let you understand the operation mechanism of a program from the bottom.

Look at the CPU.

For the CPU, we must first understand what it is all about, its internal structure. A thing as good as the CPU must be extremely complicated. After all, it is a super high-tech gadget, so ah, What we are talking about today are the main ones, which are useful to our programmers, and the part you need to understand, don’t raise the bar.

First of all, I hope you remember this sentence first:

The CPU interprets and runs the content of a program that is converted into machine language.

Also, when talking about the CPU, it must be inseparable from memory. In today’s introduction, we will also talk about memory, because a separate article will explain the memory later. After all, we programmers need to understand these. Yes, so the CPU and memory are often in the same group. They are composed of many transistors. They are called IC, which means integrated circuit.

Next, let’s look at the composition of the CPU. Note what I said before. The composition mentioned here refers to the main part. After all, the CPU structure is complex.

What we need to know about the CPU structure.

Focus needs to be on registers.

Arithmetic unit.

The literal meaning is related to the operation. In short, it is responsible for the operation of reading the data from the memory into the register. We can regard it as a data processing factory. It is the operation of the data in the register. These operations include basic arithmetic and logical operations…

Then for the calculator, we need to understand several important roles, and I will introduce them one by one:

Arithmetic Logic Unit (ALU)

This is an important component of the calculator. It is mainly responsible for the processing of the data, to implement the arithmetic and logical operations on the data.

Accumulation Register (AC)

Usually called an accumulator, it is a general-purpose register. What’s the use of this? The ALU mentioned above, when the ALU finishes arithmetic or logical operations, it will get a result, and the result will be stored in the AC.

Data buffer register (DR)

This DR also stores data. The data stored in it is mainly read from memory and will be temporarily stored in this DR. This is equivalent to a transit station for data transfer between memory and CPU. Why is this? Everyone has a big difference in the speed of the CPU and memory. Introducing a DR in the middle has a buffering effect on the speed to a certain extent, so that the speed difference is not too fierce.

Status Condition Register (PSW)

This seems a bit hard to understand. First, the register still holds data, so PSW naturally holds a certain data. It mainly saves the content of various condition codes established by the results of running or testing arithmetic and logic instructions. Divided into status flags and control flags.

We need to understand the above components, which are the core of the arithmetic unit. Besides, it is necessary to say that each of them has an English abbreviation. For example, ALU means arithmetic logic unit. Why do we say this? Alas, because English abbreviations are simple and compelling, when we communicate, we usually say ALU instead of arithmetic logic units. Therefore, it is important to remember their abbreviations.

Otherwise, if you communicate with others, the other person’s exit is ALU, DR, etc. You can easily keep up with the rhythm.

Have a basic understanding of the CPU operator later, let us look at another important component of the controller.


The controller plays a controlling role. The above arithmetic unit only performs the function of arithmetic, but the controller is more powerful. It controls the work of the entire CPU. For the controller, it is necessary to ensure the correct execution of the program. And the ability to handle exceptions.

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

Instruction Register (IR)

Registers hold things, and instruction registers naturally hold instructions. That is to say, if the CPU wants to execute an instruction, it needs to read the instruction from the memory first, then put it into the data buffer register we said above, and then from DR Put it in the IR, and then, there will an instruction decoder that generates a lot of micro-operation instructions based on the contents of the IR, to control other components to complete the corresponding functions.

Program Counter (PC)

This person who has learned Java memory structure should understand that it holds the next instruction to be executed, that is, when a program is to be executed, the starting address of the program will be stored in the PC. If this instruction is executed, don’t worry, the CPU will automatically change it so that the PC stores the next instruction to be executed.

Address Register (AR)

This is the memory address that holds the memory on which the CPU accesses the memory. Why do you want to save this because the CPU and memory speeds are quite different, so the CPU needs to save the address of the memory that is being accessed until the memory reads and writes? The operation is complete. Otherwise, which memory unit was not found before?

Instruction Decoder.

The simple thing is to analyse the instructions and see what you want to do with this instruction.

I don’t know if you have found that when we introduced the arithmetic unit or the controller; we mentioned a lot of registers, which is enough to show the importance of registers. There is no saying that the CPU is a collection of registers, but we are talking about registers. Before, we still need to take a simple look at the memory.

Briefly talk about memory (will be said separately later)

The CPU and memory are always inseparable. Here is a brief introduction to the memory to facilitate the understanding of the CPU. The memory is naturally a storage component. What is stored? It is data and instructions, and the memory we often refer to is the memory stick in the computer, which is used to store programs, data, instructions, etc.

According to the image, the memory can be regarded as a small grid, each grid is used to store data, and each grid has a number, the memory address, according to it can be obtained with the corresponding memory stored data What you need to know is that if the power is off, the data in the memory will be cleared.

Most of the CPUs are registers.

The above said that the CPU is composed of two parts, the arithmetic unit and the controller. We cannot separate both the arithmetic unit and the controller from the registers. We can be said that the CPU is the world of registers. Therefore, for the CPU, we must master the registers. Some operations that the CPU does are implemented through registers. Look at this article: How does a program run on a computer?

First, remember a sentence:

Java and python are high-level languages. Programs written will eventually be converted into machine language, and then they will enter the CPU operation. Essentially, the corresponding processing is performed through the internal registers of the CPU.


This is what we need to master. Registers are different. The number and type of registers in different CPUs are different, but there are roughly the following registers, which are divided according to functions.

Also, the CPU can be understood as a collection of registers. Then you need to know that there is only one program counter, accumulation register, flag register, instruction register, and stack register. There are usually multiple other registers.

How the CPU compares.

This also needs to be understood. The comparison of CPU is doing subtraction internally. What does it mean? For example, to compare a and b, it is to perform an ab operation internally and then compare a and b according to the obtained result. This must be done with the help of a register. For example, the obtained result will be placed in the flag register…

What the CPU does is very simple.

The CPU mainly does the following four things.

  1. Read data from memory and put it into registers.
  2. Write the data in the register to memory.
  3. Perform mathematical and logical operations (addition, subtraction, multiplication and division, AND, OR)
  4. Jump according to the corresponding conditions and execute other instructions (one instruction jumps to another instruction)

We know that the CPU does things according to instructions. Why does the CPU only do the above four things? It mainly depends on the machine language instructions that the CPU can execute.

CPU instruction execution process.

After the explanation above, we should know that the CPU only depends on the instructions to do things. So how does the CPU operate on instructions? What we need to know is the CPU of almost all von Neumann computer. We can divide its work into 5 steps: instruction fetch, instruction decode, instruction execution, access fetch, and result write back.

Let ’s take a look:

First: Instruction fetch.

The literal meaning is very easy to understand, isn’t it just to get the required instructions, then where is the instruction, it must be in memory, we need to fetch the instructions we need from memory, and then, why not just put the instructions to Registers in the CPU, this instruction fetch is such a process.

Second: Instruction decoding.

Does this quickly come to our instruction decoder as mentioned above? Isn’t this an analysis of the instruction obtained in the previous step? Why should we analyse it? We have to see what this instruction will do.

Third: Execute instructions.

This is not better understood, it is to work and achieve the corresponding function.

Fourth: Access.

Intuitively, it doesn’t seem to be so easy to understand. What we said is that we may need to get data from memory according to the above operation. What does it mean? We know that memory stores data and instructions, such as executing a + for the operation of b, we get the addition instruction. Both a and b are operands. They are also stored in memory. Do we also need to go to memory to get them?

Fifth: Write back the results.

What is this? After doing the above steps, the operation must have a result. This result is tricky. You must not save it. Usually, it is stored in the internal register of the CPU, usually the flag register.

Well, here we are about the CPU knowledge is much more than that, but as a programmer, you must at least know this.




Can read and write!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Corda Accounts library

CCIE EP.16 def SD-WAN (self, efficiency, Experience, Sec):

Inject Properties from external source in Spring Boot

Popular UI architectures compared & how Blazor+Fusion UI fits in there

Scalable Field View, Zoom and 2D UI in a 3D world — Project GoldScript

Gateways — Translating between space and ground

GSoC 2019 Experience — With the Zulip Open Source Project

How to scale DataCamp’s engineering group?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shingai Zivuku

Shingai Zivuku

Can read and write!

More from Medium

Fauna: Embed vs Collection — Why you should choose Embed over Collection

Automating water bill generation for an apartment society

Programming Languages: Pick Your Poison (C++ vs Python vs Java)

How a command is run in shell