# How Does the Cpu Understand Code

Date: Nov 1, 2022

I recently read such a good article. From the perspective of the underlying hardware, I analyzed the CPU's recognition and reading of codes. The content is very exciting. After reading it, I feel that many things I learned in college are instantly connected. I will share it with you here. Hope to read it carefully and gain something.

To start this topic first, let’s talk about semiconductors. What is a semiconductor? A semiconductor is actually a thing between a conductor and an insulator, such as a diode.

Current can flow from terminal A to terminal C, but not the other way around. You can think of it as something that prevents the current from flowing backwards.

When the C terminal is 10V and the A terminal is 0V, the diode can be regarded as disconnected.

When the C terminal is 0V and the A terminal is 10V, the diode can be regarded as a wire, the result is that the current of the A terminal continuously flows to the C terminal, and the final result is that the A terminal = the C terminal = 10V

Wait, isn't it said that the C terminal is 0V, and the A terminal is 10V? It turns out that the result is that A terminal = C terminal = 10V? You can understand this as the initial state, and when it finally stabilizes, it will become A terminal = C terminal = 10V.

I'm sorry for the children's shoes of the liberal arts. I really don't know how to ask the high school physics teacher. Anyway, if you can't understand it, just remember that it is equivalent to a wire in this case.

Using semiconductors, we can make some interesting circuits, such as [AND gates]

At this time, as long as one of the A terminals and the B terminals is 0V, the Y terminal will be directly connected to the 0V place, causing the Y terminal to become 0V. Only when both ends of AB are 10V, there is no current flow between Y and AB, and the Y end is also 10V.

We call this device an "AND gate", and count the place with voltage as 1, and the place with 0 voltage as 0. As for the specific V voltage, it doesn't matter.

That is, AB must input 1 at the same time, and the output terminal Y is 1; AB has a 0, and the output terminal Y is 0.

Others include [OR gate], [NOT gate] and [XOR gate], which are similar to this. The OR gate is that if one of the inputs is 1, the output is 1, and if 00 is input, the input is 0.

The NOT gate is also easy to understand, that is, input 1 outputs 0, and input 0 outputs 1.

The XOR gate is a bit more difficult to understand, but that's the way it is. Input 01 or 10 and output 1, and input 00 or 11 and output 0. (that is, if two identical values ​​are input, 0 is output, and if two different values ​​are input, 1 is output).

These kinds of doors can be made of diodes, and we will not demonstrate how to do them. Interested children's shoes can try it out for themselves. It is also a hassle to draw diodes every time, so we simplify the gate circuit into the following symbols.

Then we can use the gate circuit to make the CPU. Of course, making a CPU is still quite difficult. Let's start with the simple one: the adder.

The adder, as the name suggests, is a circuit used for addition. The simplest one is the following one.

AB can only input 0 or 1, that is, the adder can calculate 0+0, 1+0 or 1+1.

The output S is the result, and C represents whether a carry has occurred, binary 1+1=10. At this time C=1, S=0

After spending most of the day, is it a sense of accomplishment to count 1+1?

Let's further calculate 1+2 (binary 01+10), and then we find a new problem: the second bit needs to deal with the problem that the first bit may carry, so we have to design a full adder.

It's too troublesome to draw like this every time, let's simplify it

That is, there are 3 inputs and 2 outputs, respectively input the two numbers to be added and the carry of the previous digit, and then input the result and whether to carry.

Then we string up this full adder

We have a 4-digit adder, which can calculate the addition of 4 digits, that is, 15+15. It has reached the level of the kindergarten middle class. Is it particularly powerful?

After completing the adder, let's make a multiplier. Of course, multiplying any decimal number is a bit troublesome. Let's make a multiplication by 2 first.

Multiplying by 2 is very simple. For a binary number, we add a 0 after it to multiply by 2, for example:

5=101 (2)

10=1010 (2)

So we only need to move the input forward by one, and then add a zero to the lowest bit, which is a multiplication of 2. I won't draw the specific logic circuit diagram, you just need to know what's going on.

What about multiplying by 3? Simple, first shift once (multiply by 2) and then once again. What about multiplying by 5? First shift twice (by 4) and then once. High CPU, frequent GC, how to troubleshoot? This one tells you.

Therefore, there is no multiplication in general simple CPUs, and multiplication is implemented by software through a combination of displacement and addition. This is a bit far, let's continue to do CPU.

Now suppose you have an 8-bit adder, and a module that shifts by 1 bit. String together and you can count:

(A+B) X2

! Exciting, almost to the level of a quasi-primary school student. Then if I want to count:

AX2+B

Woolen cloth? It's simple, you just need to change the wiring between the adder module and the displacement module, and change the input A to pass the displacement module first, and then enter the adder.

What? ? ? ? what do you say? ? ? You mean I have to rewire if I change the program?

So what do you think? Programming is just plugging the wire back and forth.

Surprise or not? Accidental or not? Early computers were programmed in a few minutes but plugged in for days. And plugging in is a meticulous and patient work, so the programmers at that time were all beautiful girls in uniforms, just like the photos. Is there a feeling of being out of time?

Although it is a joy to be with beautiful women, plugging wires is also a tiring job. So we need to improve it so that the CPU can add or multiply by 2 according to the instruction.

Two more modules are introduced here, one is called flip-flop, abbreviated as FF, which seems to be called flip-flop in Chinese.

The function of this module is to store 1bit data. For example, in the above RS-type FF, R is Reset, and input 1 will clear it. S is Set, enter 1 to save 1. When all RS inputs are 0, the content just saved will always be output.

We use FF to save the intermediate data of the calculation (it can also be the intermediate state or something else), 1 bit is definitely not enough, but we can use 4 or 8 in parallel to save 4 or 8 bits of data. This we call the register (Register).

The other one is called MUX, which is called selector in Chinese.

This is simple, sel input 0 to output the data of i0, whatever i0 is, output whatever, 01 can be. Similarly, if sel is input 1, the data of i1 will be output. Of course, the selector can be very long, such as this four-in-one-out

I won't go into detail about the specific principles. In fact, you can understand it by looking at the logic diagram and pondering it. It is enough to know that there is such a thing. With this we can design an activation pin for the adder and the multiply-by-2 module (displacement).

Input 1 of this activation pin activates the module, and input 0 deactivates it. This way we can control whether the data flows into the adder or the displacement module.

So we first designed 8 input pins for the CPU, 4 bits of instructions, and 4 bits of data. We design 3 more instructions:

0100, the data is read into the register

0001, the data is added to the register, and the result is saved to the register

0010, the register data is shifted one bit to the left (by 2)

Why is it designed this way, as I said just now, we can design an activation pin for each module. Then we can connect the register, adder, and shifter activation pins with the second, third, and fourth pins of the instruction input, respectively. In this way, when we enter the 0100 command, the register input is activated, and other modules are 0 not activated, and the data is stored in the register. Similarly, if we enter the instruction 0001, the adder starts to work, and we can perform the addition operation.

Here is a simple answer to the first small question of the question:

How can the cpu understand these secondary numbers?

Why can the CPU understand it, because the lines in the CPU are connected like this. You enter a binary number, activate a number of specified modules in the CPU like a switch, and change the combination of these modules, and finally get the result.

A few questions that may be asked:

Q: There may be thousands of small modules in the CPU. Can a 32-bit/64-bit instruction control that many?

A: There are only 3 modules in the CPU of our example, so we connected them directly. There will be a decoder in the real CPU, which translates the instructions into the required form.

Q: The simple CPU you gave as an example, what happens if I enter the command 0011?

A: Of course, the adder and the shifter are activated at the same time, resulting in unpredictable consequences. Simply put, because you use an undesigned instruction, the consequences are at your own risk. (Doing this on a real CPU has a high probability of crashing. Of course, there must be various protective designs. If you die, the current process will die)

Careful friends may find a problem: the instructions you designed

[0001, the data is added to the register, and the result is saved to the register]

Can't do this step? After all, there is a write-back process, which is actually the case. The simple CPU we designed executes an instruction in almost three steps, reading the instruction, executing the instruction, and writing the register.

The classic RISC design is divided into 5 steps: read instruction (IF), decode instruction (ID), execute instruction (EX), memory operation (MEM), write register (WB). Some of the instructions of the x86 CPU we usually use may be divided into nearly 20 steps.

You can understand that there is such a switch. When we click it, the CPU will take a step. The faster you press it, the faster the CPU will go. Huh? I hear you have an idea? Young man, your idea is very dangerous, let alone whether you have a unicorn arm, can you press it so fast (modern CPUs are only more than 2GHz, about 2 billion presses per second)

Even if you can press it so fast, although the speed is going up, the power consumption will be greatly increased, and the stability of heat rise will decrease. There is indeed such a way of playing on the rivers and lakes, which is called overclocking, but it is not recommended for beginners to try it.

How does the CPU know where it is? Didn't I introduce FF earlier? This can be used not only to store intermediate data, but also to store intermediate states, that is, where to go.

The specific design involves FSM (finite-state machine), which is the theory of finite state machine, and how to use FF to implement. This is also a very important part. The exam is mandatory, but it has little to do with the topic, so I won't talk about it here.

Let's continue what we just said, now we have 3 instructions. Let's try to calculate (1+4)X2+3.

0100 0001 ; register is stored in 1

0001 0100 ; the number of the register plus 4

0010 0000 ; multiply by 2

0001 0011 ; plus three

Great, with this computer we should be able to beat all the kindergarten children and dominate the big class. And now we are using a 4-bit CPU. If we replace it with an 8-bit CPU, we can completely beat the lower grade primary school students! In fact, it is a very advanced idea to control the CPU with a program. Before that, the CPU of the computer (device) was designed separately.

In 1969, a Japanese company, BUSICOM, wanted to develop a program-controlled calculator, and the American company responsible for designing the CPU felt that it would be silly to redesign the CPU every time. On the first microprocessor 4004.

This architecture changed the world, and the American company responsible for designing the CPU became an industry giant step by step. Oh yes, it's called Intel, yes, it's the one with the beep. Let's sort out the program just now:

01000001000101000010000000010011

You come and input it into the CPU, and I will prepare for the work of going to the kindergarten class to play in the gym. What? After we lose, can the children break their fingers and figure it out? ? There is no way machine language is so anti-human. Oh, forgot to mention, this language composed of only 01 is called machine language (machine code), and it is the only language that the CPU can understand. However, if you make machine language read by people, it will definitely become a classic in one second. No one can stand it.

## Related Articles

• ### A detailed explanation of Hadoop core architecture HDFS

Knowledge Base Team

• ### What Does IOT Mean

Knowledge Base Team

• ### 6 Optional Technologies for Data Storage

Knowledge Base Team

• ### What Is Blockchain Technology

Knowledge Base Team

## Explore More Special Offers

1. ### Short Message Service(SMS) & Mail Service

50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00 Contact Us