what? How does the CPU run the code? Too difficult for people! ! !

I recently read such an article and analyzed the recognition and reading of the code by the CPU from the perspective of the underlying hardware. The content is wonderful. After reading it, I feel that many things I learned are instantly connected and shared with the apes.

 

To start this topic first, let's talk about semiconductors. What is a semiconductor?

 

A semiconductor is actually something between a conductor and an insulator, such as a diode.

Current can flow from terminal A to terminal C, but not vice versa. You can understand 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, and the result is that the current at the A terminal continuously flows to the C terminal, resulting in the final result is A terminal = C terminal = 10V

Wait, isn't it a good C terminal 0V, A terminal 10V? So it turns out that end A = end C = 10V?

You can understand this as the initial state, and when it finally stabilizes, it will become A terminal = C terminal = 10V.

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

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

At this time, as long as one of the A terminal and the B terminal 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, no current flows between Y and AB, and the Y end is also 10V.

We call this device an "AND gate", and count places with voltage as 1, and places with 0 voltage as 0. As for the specific voltage of several volts, that is not important.

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

There are also [OR gates] [NOT gates] and [Exclusive OR gates], which are similar to this. The OR gate means that one of the inputs is 1 and the output is 1, and the input of 00 is 0.

The not gate is easy to understand, that is, input 1 and output 0, input 0 and output 1.

The XOR gate is a little harder to understand, but it's the same. Input 01 or 10 to output 1, and input 00 or 11 to output 0. (That is, if you enter two identical values, you will output 0, and if you enter two different values, you will output 1).

These types of doors can be made with diodes, and I won’t demonstrate how to do them. Those who are interested can try them on their own. It is also troublesome to draw diodes every time, so we simplified the gate circuit into the following symbols.

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

Adder, as the name suggests, is a circuit used to calculate addition. The simplest one is the following.

AB can only input 0 or 1, which means that this adder can count 0+0, 1+0 or 1+1.

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

After spending a lot of effort, is it a sense of accomplishment to count as 1+1?

Let's go further and calculate 1+2 (binary 01+10), and then we found a new problem: the second bit needs to deal with the possibility of carrying the first bit, so we have to design a full adder.

 

It’s too much trouble 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 one, and then input the result and whether to carry.

Then we string up this full adder

 

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

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

Multiplying by 2 is very simple. For a binary number, we add a 0 at the end to multiply it by 2.

such as:

5=101(2)

10=1010(2)

So we just need to move the input one bit forward, and add a zero to the lowest bit, and it will be multiplied by 2. I won't draw the specific logic circuit diagram, as long as you know what's going on.

How about multiplying by 3? Simple, first shift once (multiply by 2) and add again. How about multiplying by 5? Shift it twice (multiply by 4) and add it again.

Therefore, a simple CPU does not have multiplication, and the multiplication is realized 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. You can forget it if you string it together!

(A+B)X2

Exciting, it has almost reached the level of quasi-primary school students.

What if I want to count?

AX2+B

Simple, you just need to change the wiring of 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 are you talking about? ? ? Do you mean I have to re-wire after changing the program?

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

Surprise not surprise? Accident is not accident?

Earlier computers were programmed like this, and it took a few minutes to plug in for days. And plugging in is a meticulous and patient work, so the programmers at that time were all beautiful girls, the kind in uniform, just like the picture. Is there a feeling of untimely birth?

Although it is a joy to be company with beautiful women, it is also a tiring job to plug in. So we need to improve it so that the CPU can add or multiply 2 according to instructions.

Two more modules are introduced here, one is called flip-flop, or FF for short, 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 is cleared. S is Set, enter 1 to save 1. When the RS inputs 0, the content saved just now will always be output.

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

The other is called MUX, and Chinese is called selector.

This is simple, sel inputs 0 and outputs the data of i0, and outputs whatever i0 is, and 01 is fine. Similarly, if sel inputs 1 then outputs i1 data. Of course the selector can be very long, such as this four-in and one-out

 

I won’t elaborate on the specific principles. In fact, just look at the logic diagram and figure it out. Just know that there is this thing.

With this thing, we can design an activation pin for the adder and the multiply 2 module (displacement).

Enter 1 of the activation pin to activate the module, and enter 0 to deactivate. This way we can control whether the data flows into the adder or the displacement module.

So we first designed 8 input pins, 4 instructions and 4 data for the CPU.

We design 3 more instructions:

0100, data is read into the register

0001, add the data to the register, and save the result to the register

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

Why is it designed like this? Just now, we can design an activation pin for each module. Then we can use the second, third, and fourth pins of the command input to connect the activation pins of the register, adder and shifter respectively.

So when we input the 0100 command, the register input is activated, and other modules are all 0 not activated, and the data is stored in the register. In the same way, 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 this question:

Then why can the CPU understand these two-level numbers?

Why the CPU can understand, because the line inside the CPU is connected like this. You enter a binary number, activate several specified modules in the CPU like a switch and change the way these modules are combined, 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 they are directly connected. There will be a decoder in the real CPU, which translates instructions into the required form.

Q: For your simple CPU, what will happen 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 used an undesigned instruction, you are responsible for the consequences. (The high probability of doing this on a real CPU is a crash. Of course, there will be various protective designs, and death will kill the current process)

Attentive friends may find a problem: the instructions you designed

【0001, add data to register, save the result to register】

Can't do this step? After all, there is a write-back process, which is actually the case. The simple CPU we designed takes almost three steps to execute an instruction, read instructions, execute instructions, and write registers.

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 instructions of our usual x86 CPUs may be divided into nearly 20 steps.

You can understand that there is such a switch. When we click it, the CPU will go one step. The faster you press it, the faster the CPU will go. Huh? I heard you have an idea? Boy, this idea of ​​yours is very dangerous, let alone whether you have a unicorn arm, can you press it so fast (modern CPUs are only 2GHz, about 2 billion times per second)

Even if you can press that fast, although the speed is up, the power consumption will increase greatly, and the heat will increase and the stability will decrease. There is indeed this kind of gameplay in the arena, called overclocking, but novices do not recommend you to try it.

How does the CPU know where it is? Didn't you introduce FF before? This can be used not only to store intermediate data, but also to store intermediate states, which is where you are.

The specific design involves FSM (finite-state machine), which is the theory of finite state machine, and how to implement it with FF. This is also a very important piece. The exam must be taken, but it has nothing to do with the topic, so I won’t go into it here.

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

0100 0001; store 1 in the register

0001 0100; add 4 to the number of the register

0010 0000; multiply by 2

0001 0011; add three more

It’s 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 you change to an 8-bit CPU, you can beat the lower grade pupils!

In fact, using a program to control the CPU is a very advanced idea. Before this, the CPU of the computer (machine) was designed separately.

In 1969, a Japanese company BUSICOM wanted to develop a program-controlled calculator, and the American company responsible for the design of the CPU also felt that it was a silly thing to redesign the CPU every time, so the two hit it off and launched an epoch-making product in 1970. The first microprocessor 4004 was launched.

This architecture changed the world, and the American company responsible for designing the CPU has also become an industry giant step by step. Oh, by the way, it's called Intel, yes, it's the chuckle.

Let's sort out the procedure just now,

01000001000101000010000000010011

You come to input it into the CPU, I will prepare for the work of going to the kindergarten class. What? After we lose, the kids can figure it out by breaking their fingers? ?

There is no way that machine language is so anti-human. Oh, forgot to say, this language composed of only 01 is called machine language (machine code), and it is the only language that the CPU can understand. But if you make machine language read by people, it will definitely change to Dianwei in one second. No one can bear it.

Guess you like

Origin blog.csdn.net/weixin_45132238/article/details/108550290