Before writing a very simple Hello OS, let’s think about a question — how does the code actually work? The following will start with 0 and 1, diode, digital circuit, analog circuit, and talk about the secret inside the code.

From 0 and 1 to code

Now it’s time to start asking the question: How does a code actually work? How does the inside of the CPU actually work?

Let’s start by looking at the basic elements “0” and “1” inside the computer. The hacker scene in the movie is usually accompanied by a “digital rain” effect like the one shown below:

0 s and 1 s

So what are the zeros and ones in a computer?

Simple, low level and high level.

What you mean?

0 actually represents low level and 1 represents high level. The level here refers to the magnitude of the voltage (in fords, denoted by the symbol V), and the level is really a contrast.

For example, if 0.2V is low, 5V May be high. Notice THAT I said “probably”, isn’t the voltage alone enough to determine whether it is high or not?

Yes, that’s right. The value of high and low level is determined by the specific circuit. Generally, both of them have a threshold value. When the voltage is greater than a certain threshold value, it is the high level. When the level is below a certain threshold, it is low.

0 and 1 in computers are human nicknames for low/high levels for ease of understanding. In fact, the two terms stand for digital circuit and analog circuit respectively.

Digital circuit is the circuit is represented by “0” and “1”, namely the corresponding logical symbols, as shown below:

Analog circuit is represented by parameters such as voltage and current in the circuit, as shown in the figure below:

So what’s the connection between digital and analog circuits?

In fact, it can be seen as the relationship between building construction drawing and building physical drawing: digital circuit is mainly to show its logic and function, analog circuit is to figure out what materials and ways to achieve the results that digital circuit wants to achieve!

Now you can begin to consider: how are high and low levels achieved?

diode

A diode is an electronic device made of semiconductor materials (silicon, selenium, germanium, etc.) with unidirectional conductivity.

What is unidirectional conductivity? Take a look at the following image first, a physical image of a diode on the left and a logical circuit diagram (i.e. abstract) on the right:

Note: Diodes can also be divided into rectifier diodes, regulator diodes, light emitting diodes, etc., only ordinary diodes are introduced here.

The current can flow from the positive (+) pole to the negative (-) pole, which is in the conduction state; But it doesn’t work the other way around. It’s cutoff. This is called unidirectional conductivity!

Attached a diode volt ampere characteristic diagram, do not understand it does not matter, familiar:

Because of its unidirectional conductivity, a diode acts like a switch:

When the switch is on, the voltage on both sides is the same.

If the positive (+) voltage is 5.2V, then the negative (-) voltage is also 5.2V.

Note: this is not accurate, because it also involves the voltage size of the positive and negative poles, threshold voltage, etc., only consider the ideal situation, the other can be explored by oneself.

If we further define 5.2V as high, then both positive and negative are “1” at this point!

When in the cutoff state, the switch is closed, and the voltage on both sides is inconsistent.

If the positive electrode is only 0.2V, then both the positive and negative electrodes may be low at this point, i.e., “0”!

Logic operations and gates

We can get zeros and ones with diodes, but zeros and ones alone are not enough. How can computers do trillions of calculations?

It takes time to do this, and you can’t do it without logic, which is a gate circuit. Common logic operations are and, or, not, xor, same or. Their truth tables and logical symbols are as follows.

Never mind the logic notation, the point is the truth table.

Let’s start with “and” :

And, that is, the output is 1 only when all inputs are 1. Otherwise, the output is 0. Now the truth table, is that what it means?

Wait, I think we went from a diode to here? This gate can be realized with a diode?

Of course! The picture below is a simple gate with diode and resistor (no tube)!

A and B are inputs, and Y is output.

Let’s break it down:

If A is high level “1” and B is low level “0”, at this time, D1 is in cut-off state and D2 is in on state, Y and B have the same voltage and low level “0”, so the output is “0”.

If A is low level “0” and B is high level “1”, at this time, D1 is in the on-state and D2 is off state, Y and A are the same voltage, also low level “0”, so the output is “0”.

If A is high level “1” and B is high level “1”, both D1 and D2 are in the cutoff state, Y and VCC voltage is basically the same, that is, high level “1”, so the output is “1”.

Note: to determine the size of the voltage circuit of current and voltage, in simple terms, in a circuit, the greater the resistance, the resistance occupied the greater the voltage, when the diode is off condition, resistance can be considered super big, so R1 point voltage is very small, the output Y, and the power supply VCC are basically identical. If you want to know more, please baidu explore or leave a message, I continue to update.

And gates can also be represented as ampersand in digital circuits:

Analyze an or gate again: when there is at least one “1” in the input, the output is “1”, if all “0”, the output is “0”.

The door just showed two inputs, now let’s look at four!

For simple analysis, when all four ABCD are input low level “0”, all four diodes are in cut-off state, and the output is low level “0”.

Only when either of them is not low, if A is high level “1”, then the D1 diode turns on, and the output is the level of A, that is, high level “1”.

Or gates in digital circuits can also be expressed as:

At the back of the gate, such as diode alone is less, you need to please its brother – “transistor”, here temporarily not to introduce, we just need to know: we can use these components constitute such as shown on the chart of the gate, and now we have to do is think about how to use the gate for code execution!

Add, subtract, multiply and divide four operations

With the logic gates above, how about we consider how to add, subtract, multiply and divide? For the convenience of recall, now we have the gate tool set out again!

adder

First of all, we need to understand that all computer operations are actually based on binary (after the conversion of base), that is, the adder input and output are only “0” and “1”.

It is worth noting that the best way to design A logic circuit is to first sketch A value table, which for A simple adder has two inputs (A/B) and one output (S) and one carry (C).

** Input two 1’s, output the current bit 0, carry 1; Enter two zeros, the current bit is 0 and the carry bit is 0; Input a 0 and a 1, output the current bit 0, carry 0.

A B S C
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

Just look at the current position and see if the gate tool set looks familiar? Xor gate!

Just look at the carry, to see if the door is a little familiar!

That is to say, adder can be made up of xOR gates and gates! As shown below:

But now this can only be called a “half adder” and can only compute 1+1 at most. How to go further, say 1+2,2+2?

This needs to add a carry interface in the original adder, as follows is the “full adder” :

Copy a Baidu picture to simplify the structure of the above picture:

By connecting two adders in series, you get an adder that can do 2-bit (3+3) addition.

To add several digits, use several adders in series, such as four digits (15+15) :

Note: The figure above shows a serial carry adder, also known as a ripple adder. There is also a carry ahead adder that is faster than the one that does the calculation once from right to left.

Reference: wenku.baidu.com/view/9de55e…

subtracter

The subtracter is essentially an adder, like 15 minus 15, which is actually 15 plus minus 15, which is the so-called complement, but I’ll talk about it later.

multiplier

Add and subtract. What about multiplication?

Let’s look at how binary multiplication works, as follows:

The figure above shows a 4-digit multiplication. Consider a 2-digit multiplication first, for example, a2A1 × b2B1:

Why does the output have four digits? Because a two-digit multiplication is at best a four-digit result! Let’s say 11 binary times 11 binary, what’s the result? 1001!

Combined with the multiplication process in the figure above, we can use the and gate and adder to form the multiplier, as shown in the figure below:

By 1? The original cost.

By 2? Since it’s binary, you just add a 0 to the end.

By 3? So let’s add 0 first, and we can do another addition.

By 4? Plus two 0’s.

.

divider

Divider design is more troublesome, here not to expand, interested students can refer to the following links:

The making of multiplier and divider — Baidu Library

Wenku.baidu.com/view/b30ce2…

.

ok! After the above analysis, we have obtained the device of addition, subtraction, multiplication and division, and we can carry out more difficult calculations.

Such as:

A * B + C

This requires saving the result of A×B before adding it to C, etc. Save? So how do you keep data inside a computer?

register

And that’s what registers are about.

Because of the importance of saving data, scientists have been thinking about how to use in the circuit to save data, such as making a device output high level all the time, that is not “1”? Input low level all the time, that is not “0”? And you can switch between “0” and “1” freely.

After a bit of tinkering, scientists invented the latch. What does it do? It can change the output state according to the input signal.

A latch is a basic unit of memory (that is, storage). The simplest latch can store a bit of binary information.

It has two characteristics:

  • There are two stably maintained states, representing logic 0 and logic 1 respectively.
  • It is possible to change from one state to another (0-1 or 1-0) with appropriate input signals;

The simplest S-R latch is constructed from two “or” or “and” gates:

R, S is the input end of the latch, the former is generally known as Reset end, the latter is Set end, Q and Q plus a bar is the opposite output (you 0 I 1, you 1 I 0).

Notice that this circuit is different from what we’ve seen before, where the output of the gate serves as its own input, and this structure is called a feedback circuit.

Analyze the working principle of latch composed of “or not” gate circuit, which involves stable state and unstable state, assuming that Q before input is 1, bar Q is 0:

  • When R and S are both zero, eh? As if this Q and bar Q didn’t change, right? Yeah, the output of the latch stays the same, so what does that do? That saves the previous data! For example, Q keeps printing 1.
  • When the input R is 0 and the input S is 1, the output Q does not change, it remains 1. Is it the same as the last output? Isn’t. Change the premise: if Q is 0 and bar Q is 1, what is the output Q after this input? Is 1. This is called transpose.
  • When R is 1 and S is 0, whatever the previous Q was, the final Q is 0, which is reset.
  • When the input R and S are both 1, the output state is uncertain, so the input is evaded by the circuit.

Not sure? Then bring out the truth table! Below is the truth table of RS latches:

S-r latches are derived from a variety of other latches and triggers, such as D latches, S-R triggers, D triggers, etc. For those interested, see the reference links at the end of this article.

By the way, triggers introduce the idea of a clock signal. What does that mean?

In fact, the clock signal is to standardize the execution timing of various circuits in the computer, just like the “321 Freeze” played in childhood, only when the fixed clock signal comes, each component will change its state. Without a clock signal to manage the timing, the changes in components would be chaotic and erratic.

With a latch and register, you can parallel constitute a register, a latch can store 1 bit, 4 parallel can store 4…… Anyway, we’ve got something to store data on.

Decoders and selectors

Suddenly we have more and more components available to us, and the question is, how do we control which components we use? You can’t manually change the connection yourself, can you?

Note: Previously it was really manual change.

Can we just input a few bits of binary and use the corresponding element? Let’s take three digits.

A 3-bit binary number can represent up to 8 outputs (2^3).

How is it implemented internally? 3 is too complicated, find a 2 will see.

The device described above is called a decoder, which translates the input binary code into the corresponding output high and low level signals, realizing the function of a selective logic circuit.

There is another problem, ** that is so many components, which one do we choose to output? Like choosing which gate to release water from a dam, this leads to the concept of **** selector. The ** selector can select one of the input binary codes as an output signal based on the state of the input binary code.

Using the gate circuit we can construct it, with which we can obtain the corresponding output:

Sel inputs 0, so out outputs the value of A, input 1 outputs the value of B.

You can also select one of four, one of eight, and so on. Here is the one of four selector:

If we add an input port to each of the four arithms, latches, registers, etc., which is 1, the component can only be used. Using the selector and the activation pin, we can control the selected component.

Actually, this is about the basics of the computer, and then finally, the connection between programming languages and what we’ve been talking about.

“The end” chapter

Suppose you have a CPU with only 8 pins, 4 data pins and 4 instruction pins.

Data pins are used to input data, and instruction pins are used to select the operations to be performed. We define when the instruction pin enters:

0001 Reads data and reads the data of the data pin into the register.

0010 Select adder, add data pin data and register data, the result in the register;

0100 Select the multiplier, multiply the data of the register by the data pin, the result is in the register;

1000 Clear the register.

May wonder how to carry out the corresponding operation through instructions? Don’t forget that we already have a lot of devices available, such as decoders, which can use four bits of binary data to select the appropriate adder, multiplier, and so on.

The above is the simplest instruction decoding process, of course, there are more than these instructions in the actual scene, but thousands of them, the logic circuit is incredibly complex. We don’t have to worry about complex scenes, just start with the basics, after all, even complex scenes are made up of basic components.

If 0001 0001 is the input 0001 save it in register.

Ok, let’s take a calculation example: 3×5+6, the inputs are:

0001, 0011,

0100, 0101,

0010, 0110,

Ok, let’s do a slightly more complicated one: 3*6+8+9+10+11 * 5 *6 * 7 * 8 * 9+15.

I’m not going to write the input, it’s too much trouble. It’s still that we’ve only defined four instructions, and who can handle thousands of them?

We might as well wrap up the directive a bit and say:

0001 is MOV

0010 is denoted by SHL

0100 is called ADD

And assuming there is now an additional element that can convert from decimal to binary, the command should be:

MOV 3;

SHL 5;

ADD 6;

Just to make things a little bit easier, this is called assembly language, and previously only zeros and ones were called machine language.

But I still feel a little bit awkward, it is clearly a 3×5+6, why do I have to write three lines, not one line? !

For example, type:

Print (" % d: / n ", 3 x 5 + 6);Copy the code

This is the famous C language, in the computer above the input is actually through a series of operations called “compilation and assembly”, compilation converts C into assembly language, assembly into assembly language machine language.

There are many other languages like C, such as Java, Golong, C++, Python, and so on, all of which have a common name — high-level languages.

A senior? Advanced is the use of these languages to program, compared to machine language and assembly language has been very human, very readable.

If later in “the ultimate language”, “language” the chosen don’t strange, in the computer the way predecessors are paving the way for future generations, whether diodes, gate circuit, logic circuit, register, decoder, or machine language, assembly language, a high-level language, and so on, others are on the basis of predecessors’ reconstruction, concentration, adaptation, and dancing.

It’s an endless spiral of iterations, which is why the end of the title is in quotation marks. Computers never end, and so does personal learning.

Computer composition

The structure of the computer as we know it is simple: host + screen + keyboard + mouse. Music aficionados or game lovers have a higher demand for music, so add a high-quality headset. Programmers probably need to copy programs a lot, so add a portable hard drive…

In fact, the above mentioned only the hardware part of the computer, a complete computer system includes hardware and software part. The hardware part is all the devices you can see and touch, the chips, the hard drive, the keyboard, etc. The software part is the program that is visible or invisible when the computer starts and runs. As shown in the figure:

There are so many things in the figure above that we can simplify it into four parts: CPU, memory, IO (input/output devices), and software.

The CPU takes software code from memory and executes it, and uses IO devices to interact with the user.

CPU

CPU can be said to be the soul and core of the computer, with the overall control, data operations (controller, arithmetic unit) function. See “From Sand to CPU” for details on the actual hardware structure, but only the logical structure is described here, namely the control unit, the storage unit, and the arithmetic unit.

· The control unit is the control center of CPU, responsible for executing instructions and operating control circuits.

· Storage unit is to store the result elements of CPU during and after running.

· An operation unit is mainly used to perform logical (or, and, nor, etc.) operations on elements stored in a storage unit.

The relationship between the three parts is as follows:

The control unit

The composition of the control unit includes:

· Instruction Register (IR) : storage of instructions, reduce the number of CPU to read instructions in memory;

· Instruction Decoder ID(Instruction Decoder) : to convert instructions to machine code so that operation units and registers can understand (perform corresponding circuit operation);

· Operation Controller (OC)

It takes out each instruction in turn from the memory (memory/external memory) and puts it in the instruction register IR. It determines what operation should be carried out through the instruction decoder ID, and then sends out the microoperation control signal to the corresponding parts according to the determined timing sequence through the operation controller OC (to be discussed later).

Storage unit

The storage unit consists of:

·CPU in-chip cache

· Register group

It is the place in the CPU where data is stored temporarily. It holds the data to be processed or processed. The CPU accesses the register in less time than it accesses the memory. The register group can be divided into special register and general register. The function of special registers is fixed, storing corresponding data separately. General-purpose registers are versatile and can be specified by the programmer. The number of general-purpose registers varies from microprocessor to microprocessor.

The use of registers can reduce the number of CPU access to memory, thus improving the CPU speed. However, due to the limitations of chip area, integration and cost, the capacity of the register bank cannot be very large. Click the Performance page in Task Manager, the interface as shown in the picture will appear:

As you can see from the lower right corner of the image, the CPU has a level 3 cache and its capacity is gradually increasing.

The operation unit

An operation unit can perform both arithmetic operations (including basic operations such as addition, subtraction, and multiplication, and their additional operations) and logical operations (including shifts, logical tests, or comparison of two values). All operations carried out by the operation unit are directed by the control signal sent by the control unit.

The working principle of CPU is shown as follows:

memory

Memory in a computer is divided into internal memory and external memory. The CPU deals directly with internal memory, that is, we often say memory, which stores the current running programs and data. Once the power is off, the data is lost and the speed is faster. External storage is also the data storage media, that is, we often say floppy disk, hard disk, CD and tape, etc., can save data for a long time, the speed is relatively slow.

I/o devices

IO devices are the medium through which computers and users interact. Such as the common keyboard, mouse, printer, scanner, monitor, card reader, tape, headphones and so on

In simple terms, the user can see what the computer wants us to see or input what the computer wants to get.

software

Software is the program in the computer, it is the interface between the user and the hardware. If the three parts besides the software are “bodies”, then the software is “soul”, and only if there is both “body” and “soul”, this is a real computer.

Software can be further divided into system software and application software.

The former is commonly heard as Windows, Linux, Android, IOS and so on. The latter is software launched by the user or invoked by other programs. Such as Word, Excel, QQ, wechat and so on.

Refer to the link

Inspiration:

How does the CPU know the code? – Zign answer – zhihu www.zhihu.com/question/34…

(2) Basic logic operation and logic gate circuit

Blog.csdn.net/as480133937…

Computer composition and design

www.cnblogs.com/lfri/p/1004…

Digital circuit latch details

Wenku.baidu.com/view/f73607…

The distinction between latch, trigger and register

Blog.csdn.net/surgeddd/ar…

Data selector

Blog.csdn.net/vivid117/ar…

“All articles on this site are original, welcome to reprint, please indicate the source of the article: juejin.cn/post/696587…” This update is updated at 2021/5/24