In every age, people who can learn are not mistreated.

Hello, I’m Yes.

For us programmers, the importance of the computer is self-evident, I believe you have a certain understanding of the computer inside.

But have you ever wondered why a bunch of logic gates combined together work? How does it work to add and subtract?

Why does the CPU keep fetching and executing? What drives it?

Today I will explore the mysteries of the bottom with you, but there are specializing in the industry, we can roughly understand, many details are not clear also does not affect.

But I believe through this article you will have a different understanding of the underlying, including computing units, memory, clock, address, overflow, complement, and so on.

This article has a lot of circuit diagrams, you may feel that this is what ah, and our development has a relationship?

You’ll see, even though we are CRUD boys, we should always be curious and have the spirit to explore.

The body of the

The story begins with electricity.

Electricity is everywhere in our life, but it always remains a mystery. Why do our screens light up when we plug them in? Our server can run?

How does electricity come from?

Electricity originates from the motion of electrons, and we know that all substances are made up of atoms, which in turn are made up of neutrons, protons and electrons.

At some point the electron disassociates from the atom and electricity is created.

Both protons and electrons have the property of being charged, with protons positive and electrons negative.

And different charges attract, same charges repel, when the number of protons and the number of electrons are equal is the most stable, if the number of imbalance will also tend to balance the direction of development.

Like a thunderstorm, when the lower cloud layer accumulates electrons and the top layer loses them, lightning is the result of a large number of electrons moving quickly from one end to the other, in order to balance out.

An aside:

Careful friends may see that the nucleus of the proton is not together ah, does not say that the same charge repel each other? That’s because there’s something called a strong internal force that aggregates them, and that’s what causes the fission that releases the nuclear energy.

I’m sure you’ve all done physics experiments where a battery lights up a light bulb.

This is actually a chemical reaction in the battery, creating extra electrons in the negative electrode, and then through the circuit of atoms in the form of a relay, one atom will receive electrons and pass them to the next atom, so the cycle of transmission circuit is formed, and finally through the light bulb to the positive electrode of the battery.

It was modified and fitted with a casing, and the flashlight was built.

And flashlights can be used not only for lighting, but also for communication. I believe everyone has seen similar movie scenes, I this flashlight light flash three we on!

When it comes to such simple communication, we have to mention The Morse code. I believe that we also have a certain understanding of the Morse code from various channels, such as “Interstellar”, the thief’s beautiful movie.

At the beginning of the 19th century, when long-distance communication was still carried over long periods of time by horse-drawn wagons and other tools, people were exploring ways to communicate over long distances instantly. At that time, Morse was immersed in experiments that would eventually lead to the invention of the telegraph.

The idea of the telegraph is the same as the idea of the torch, the idea of the torch communication is to control the light by switching on and off, while the telegraph uses electromagnetic phenomena.

The wire is wound around an iron bar, and when the electricity is applied, the bar becomes a magnet, and when the power is turned off, the magnetism will disappear again. Then, a microphone is created to attract the magnetic force of the movable bar to make a sound.

After electrification, the movable rod is pulled down and the sound of “drip” will be issued on the lower part of the tap. When the power is off, the movable rod will be reset and the sound of “tap” will be issued on the upper part of the tap. Use the quick ticks as dots and the slow ticks as strokes.

Long-distance communication can be achieved through the long connection of wires, and the Morse code table can be consulted by the combination of discriminating dots and dashes, which translates into the final information.

If you want two-way communication, you just have another one deployed in reverse, and that’s the telegraph.

But the wire has a resistance, and the longer the wire, the greater the resistance, so there’s a distance limit, but that doesn’t bother us, the easiest way to do it is to forward it.

Build a telegraph station in the middle distance, and hire someone to get the message from the sender, and then tap it again to send it to the real receiver, but this requires extra manpower, so you can connect the rods to drive the output of the next switch, as shown in the picture below.

So that’s actually how relays work, so let’s look at how relays are designed.

The electricity on the bottom generates a magnetic force, which attracts the metal rod above to hang down, and then the loop is formed on the top and the electricity is energized, and the weak current that was transmitted over a long distance is amplified and output again, so the final telegraphy over a long distance should look like this.

You can see that the invention of the relay was really clever.

Now that we’ve understood electrical generation, telegrams, and relays, let’s look at binary.

Binary based number systems are the simplest, with only zeros and ones, and can’t be simplified any further, and simplicity means clarity, like a switch that is either on or off.

The combination of binary can represent a variety of possibilities, such as the first zero for male, one for female, the second zero for fat, one for thin.

Let’s go back to the battery light diagram, this time with two switches.

We can know that the two switches are on the light will be on, if converted into binary representation, 0 means the switch is off, 1 means the switch is on, 0 means the bulb is not on, 1 means the bulb is on, summed up into a table is:

The left switch The right switch The light bulb
0 0 0
0 1 0
1 0 0
1 1 1

This is actually what we know as an AND operation, AND if you change the circuit a little bit it’s an OR operation.

Many switches can be combined to perform simple logical tasks, but switches need to be controlled manually.

Remember the relay? It can also be connected in series or parallel, and can be controlled by other relays in tandem, so it is more appropriate to combine them with relays, and the combination of relays is called logic gate.

A simpler version, like the one shown below, lights up when the switch is turned on.

Some people think that this is not multiple action, this is actually a buffer, can delay the signal, can also amplify the signal, and this circuit is relatively simple, in fact, there are many combinations, such as the reverse operation of the following picture, the switch closed the light will not turn on.

There are also series combinations like this, where the bulb only lights up when both switches are on.

Of course, the input doesn’t have to be a switch, AND the output doesn’t have to be a light bulb, just to make it more intuitive, but it’s too cumbersome to draw circuits like this, so electrical engineers created symbols to represent these circuits, for example, the series above is actually an AND operation, it’s an AND door.

Simplify the above picture and it will look like this.

If the circuit diagram is shown below, it is connected in parallel, and the light bulb will turn on if any switch is turned on, this is the or door.

The simplified notation looks like this:

Before also mentioned a reverse operation, open and close on the bulb instead of light is called the reverse device, the symbol is shown in the following figure.

Let’s look at a circuit like this.

The light bulb is on only when both switches are off. When either switch is off, the light bulb goes off. This operation is the opposite of the OR operation. It is called NOR gate.

Or this way, the same thing with the paintings.

And then when we look at this circuit, it only goes off when both switches are closed, which is the opposite of an NAND gate, called an NAND gate.

The simplified notation looks like this, again with an extra circle:

Let me summarize these simplified diagrams again, just to make it more impressive.

Binary adding machine

With these things, we can build a binary adding machine. Don’t underestimate addition, because you can subtract, multiply, divide, and so on.

We know that when we add, we’re going to get the current sum and carry, so for example in binary 1 + 1, the current sum is 0, carry 1.

carry 0 1
0 0 0
1 0 1

You can see that it’s just 1 +1 carrying the 1, but if you take a closer look, is it similar to AND? Only 1 AND 1 gives you 1.

AND 0 1
0 0 0
1 0 1

Let’s look at the calculation of the current sum

and 0 1
0 0 1
1 1 0

So you can imagine in your head, if you take OR and you get the wrong result in the lower right corner, if you take NAND and you get the wrong result in the upper left corner, so you have to combine the two, and the circuit diagram is as follows.

XOR gate XOR gate XOR gate XOR gate XOR gate XOR gate XOR gate XOR gate

So the addition needs to go through two logic gates, the XOR gate to operate on the current sum and the and gate to operate on the carry, which are combined as shown in the figure below:

This is actually a half adder, and the simplified diagram is as follows:

Then why is it called a half adder? Because you can only add one bit, and the carry of the previous bit does not participate in the calculation of the next bit, if you want to carry the next bit, then the run of the next bit is the current bit of A + the current bit of B + the carry before A and B.

Therefore, it is necessary to modify the two and a half adducts and add a or door.

Assume that A input 1, B input 1, carry input 1, from the most left of the first half adder S output 0, CO output 1, the second half adder S output 1, CO output 0, and the final output 1, carry output 1, the result is no problem feasible, this is called the full addition, simplify the following figure:

We have the full adder, and we have to put it together, and we have to have an input and an output, and we input the numbers through the switch, and the light bulb shows the results.

So this is an 8-bit calculator, and it has 9 lights because two 8-bits add up to 9 bits.

Then start from the right as shown in the following figure with full adder, carry ground means 0 input

Everything in the middle is the following connection, where the carry output of the previous bit is the carry input of the next bit.

The last one is to connect the carry output directly to the ninth lamp.

At this point you swing the switch of the control panel, and the machine can get the result of the addition. The simplified drawing method is shown in the figure below:

Now that we’ve built an eight-bit adder, what about a 16-bit adder? Just keep it simple.

Of course, the real computer principle is similar to this, but more complicated, such as not like our adder, carry one by one, but first carry, and do not use relays, transistors and so on.

How do we subtract?

Now that we have the adder, what do we do with the subtraction? Subtraction requires a borrow.

Let’s start with the familiar decimal system. Let’s say your account is capped at 499 and your overdraft is capped at 500, which means your account is in the range of 1000 numbers between -500 and 499, with no negative sign.

You can see that this is a three-digit number, and the maximum is 499, which means that 500 to 999 is useless, so wouldn’t it be a good idea to use negative numbers?

So let 500 mean minus 500, 501 mean minus 499, and so on.

500501… 998999000001… 498,499, make the numbers starting with 5, 6, 7, 8, and 9 all negative numbers, and it looks like it’s in a ring. 499 + 1 becomes 500, and 999 + 1 becomes 1000, but it’s only three digits, so it overflows into 000.

This process is called 10’s complement, and if you want to turn three negative digits into 10’s complement, you subtract 999 and add one, which means that 10’s complement is equal to 9’s complement plus one.

In the case of 9’s complement, subtracting a number from a string of 9’s is called 9’s complement. For example, 123 is a three-digit number. 999-123 = 876 so the 9’s complement of 123 is 876, and if you add 1 it is 10’s complement.

So let’s take minus 499, and we’re going to convert it to the complement, which is 999 minus 499 plus 1 is 501, and if you look at the permutation here it does represent minus 499.

Isn’t subtracting a number the same as adding a negative number? So with the complement we don’t have to subtract, we just have to convert to the complement and add!

Now let’s switch to binary, which is much simpler than decimal.

Take the 8-bit binary number, 00000000 11111111, which corresponds to 0, 255 in decimal, but now we want it to represent negative numbers, so in decimal we use positive numbers starting with 5, 6, 7, 8, 9 to represent negative numbers, and in binary we use the first digit starting with 1 as a negative number.

The scope at this point is:

If you understand the decimal conversion above, this binary will be fine, which is really just figuring out 2’s complement, which in turn is 1’s complement plus 1.

Let’s take 125 for example. 125 in binary is 01111101. The complement of 1 is 11111111-01111101.

You take the reverse and you add one, you get two’s complement.

So minus 125 is 10000011.

Of course, all of this assumes that the number of digits has to be fixed, so the number of digits in a computer has to be fixed, and if you go beyond that, you overflow, and by this point you should be able to understand where the complement in a computer comes from, and why does the maximum plus one become the minimum?

So the subtraction we just have to modify the adder up here, give it a switch that says that this number is negative, if it’s negative then we do a wave of reverse and then we add one more, and then we get the final result.

I’m not going to do multiplication and division, but you can also do addition and subtraction.

Oscillator (clock), latch (flip-flop) and counter

Of course, this and we know the computer is still a lot of difference, now can only do some very simple addition and subtraction operations, don’t hurry, let’s look at this circuit first.

This is an interesting circuit, when you close the switch and the circuit is open, and then the electromagnetic effect pulls the rod down, and the circuit breaks, and when it breaks the magnetism disappears and the rod moves up again, and the circuit is open again, and so on.

This circuit is called an oscillator, and it’s a very important thing to remember.

Its oscillating back and forth is in fact an alternating sequence of 0’s and 1’s in the output, as shown below:

It alternates between 0 and 1 over time, so it’s also called a clock.

The time it takes to change a cycle is called a cycle, and the frequency is the inverse of the cycle, so if the cycle is 0.05 seconds, then the frequency is 20, 20 cycles per second, in Hertz, so it’s 20 Hz.

Let’s look at this circuit again.

The light bulb is not on. When the upper switch is closed, the left or not gate outputs 0, and the right or not gate outputs 1, so the bulb lights up. Here’s the magic, you turn off the switch, the light bulb is still on, because the or not gate on the left has an output of 0, and the or not gate has an output of 0 as long as one of the inputs is 1.

If you turn on the switch below, the light bulb will go out, and then turn off the switch below the light bulb is still not bright.

You can see that this circuit has memory, so if you see that the light bulb is on now, you can infer that the top switch was closed last time, and if the light bulb is dark then the bottom switch was closed last time!

This circuit is called a flip-flop. In fact, the switch on the top is set and the switch on the bottom is Reset, so it is also called an R-S flip-flop.

But a more useful circuit would remember that the last signal at a particular point in time was 0 and 1.

Therefore, it is also necessary to make a hold bit, so that after the hold bit is off, the upper and lower switches can be toggle at will without affecting the previous hold results (the reset and setting positions in the figure below are opposite to our circuit diagram, but it does not have the same effect).

In fact, while holding bit 0, the output of reset and set through the and gate must be 0 and have no effect at all on the previous results.

But then you have three inputs, which is a little bit cumbersome. From the above observation, the meaningful input is actually on and off at the top, or off and on at the bottom, so it must be the opposite. So we set up a counter so we only have two inputs.

This is called a level trigger D flip-flop, where D stands for Data, Data input. Level triggering is when the hold bit is set to a particular level (for example, 1), the trigger stores the input value on the data side.

After understanding the hold bit, we need to introduce a clock (marked CLK), a clock that changes back and forth regularly. When the clock switches from 1 to 0, the contents of the last operation are saved, so replace the hold bit input with the clock input.

Such a circuit is called a level-triggered D latch, which means that the circuit latches one bit of data and holds it for future use. It is also called 1-bit memory.

With 1-bit storage, multi-bit storage is a simple matter of putting multiple latches together. Here is an 8-bit latch.

An edge flip flop, unlike a level flip flop, records the result instantly when 0 becomes 1. For a level flip flop, every result is overwritten when 1. Remember, in certain scenarios, the instant of an edge flip flop is more appropriate.

The circuit diagram is as follows. It is made up of two r-S trigger links. In fact, this circuit is not very messy.

A simplified drawing is as follows:

Then let’s look at the circuit:

The output of the oscillator is used as the input to the clock, and then the input to the reverse Q terminal (the lower Q in the figure stands for reverse Q, with one bar missing) is used as the input to D.

The resulting waveform looks like this, and you can see that the output frequency of Q is half that of the clock, so this circuit is called a frequency divider.

And the output of the divider can be the input of the next divider. Let’s look at this diagram again:

The resulting waveform looks like this:

Add another 0 and 1:

From the bottom up of each column starting from Q3, is it 0000, 0001, 0010…. This is the counter, and you put all eight of them together in a black box, and you get an 8-bit counter.

Of course, this counter is asynchronous, and the later ones have to wait for the previous notification, which is less accurate, so it’s better to synchronize the counter, but it’s more complicated, so I won’t cover it here.

So let’s just put it together

Now that we have the adder, oscillator (clock), latch (trigger), and counter, we are ready to assemble them.

Let’s say we have a light bulb, and we want to test eight latches, eight latches and we need three switches to show which latches to choose, two to the third power is equal to eight.

The black box in the middle is definitely for selection, it’s a switch to control the path, it’s a little bit complicated and I think it’s just a little bit of a look, but it’s circuit selection.

You don’t have to use eight to input it, so make three switches as well.

I will not post the internal structure, also as complex as the selector, this is called decoder, the final complete circuit diagram is as follows:

Instead, S0, S1, S2 are actually addresses, which latch to write to, and the corresponding output, this configuration is called read/write memory, also known as random access memory, or RAM.

It’s called memory because it can hold information, and it’s random because it can write and read according to address selection.

A simplified diagram of the circuit above is shown below, which can store 8 independent 1-bit data.

The combination of two 8*1 RAM accounts for storing eight separate 2 bits of data.

And if you do it in a combination like this, you get 16 times 1 RAM, and that DI is actually the fourth address line, so it’s 2 to the fourth.

Can see the address of the RAM array of storage capacity equal to 2 times square, and then note we figure is simplified, it actually has a lot of relay, like logic gates are made of the relay, when power electromagnetic effect and the contact back to the original, which is why the RAM is non-volatile storage medium.

We’ve got the memory out now.

Next, our goal is to input the data to be calculated into memory, and then let the adder calculate the results back to memory, and then through memory to view the results, the general assembly is as follows:

We can then combine the adder and the latch as an accumulator, that is, the value of each addition is stored in the latch as the value of the next accumulation.

Once we have an accumulator, we can pass the value of the memory to the accumulator, called Load, Add the next value to the accumulator, called Add, and then save the result in a place called Store.

Can write to the memory inside first through control panel to the value of the operation and can be through the lights on the control panel to view the memory written to the results, and then access memory address is 0000 at the beginning, is driven by the counter address to go forward, then together, finally stores the result back into RAM array, of course, also need to set the stop signal.

Turn the opcodes we defined earlier, such as Load, into specific code to control the overall flow. (You can assume that this code will tell the circuit to do some operation. You don’t need to think about it, but it is generated by a combination of logic gates.)

This opcode is only a mnemonic, because the address is fixed and the opcode instruction byte is of fixed length (1 byte), so we can follow the address after each operation, and in total each instruction (except stop) requires 3 bytes.

Simply look at the picture below, that is, store the following “code” at the memory address 0000.

And can make a Jump instruction to Jump address, can be set by the counter to reach the function of the Jump address, with the Jump we can do the loop operation. Some repeated instructions only need to be written once, and the loop is completed by conditional jump. The final assembly diagram is as follows:

The 2-1 selector switches the address input of the counter or the calculated input, and uses three 8-bit latch to represent the code, high and low address respectively. The figure above may be a bit complicated, but it doesn’t matter if you don’t understand the details, the general process is simple.

At this point we have actually assembled a computer, called a computer rather than a calculator because it can automatically pick and execute according to the instructions you write to memory, and it can perform conditional jumps and loops and automatically stop execution.

The processor of a computer is our accumulator, or arithmetic logic unit, or ALU.

That counter is our program counter PC.

The memory is the memory, the input is the control panel, and the output is the light on the control panel.

The computer has several core modules.

As for the operation code we defined in the front is actually machine language, and human beings in order to remember will make some mnemonic to identify, later is assembly language, and assembly language is too troublesome, so the abstraction of high-level languages, such as C, Java and so on.

The last

The computer described in this article is actually quite rudimentary, and a real computer would certainly not be built this way, for example, it would not use relays, it would use buses and so on to build integrated circuits and so on, and the ALU would not be so simple, it would have all kinds of parallel computing and so on.

Basically, I want to give you a general idea of how a computer works and how it’s built, because it’s essentially the same thing. If I had to tell you a lot of details, I wouldn’t, I would know a little bit, I’m not a hardware guy, I didn’t take any analog classes, I would just be the installer level.

This article makes great use of the examples of the book “The Upstory of Coding”, or it is the combing and summary of some chapters of the book. If you are interested in the original text, you can buy books by yourself. If you feel that you are short of money, you can return to “233”.


I’m Yes, from a little bit to a billion points. See you next time.