We know that addition is the most basic operation in arithmetic.
Addition is also the only job a computer does. If we can build adders, we can use addition to perform subtraction, multiplication, and division.
In fact, building an adder doesn't mean you go to an electronics market, buy parts, and actually build something in a room like Lego as a kid. Conversely, the logical structure of an adder can be drawn on paper. This is enough to allow us to understand the lowest level of computer work.
The adder is the most basic component in a computer. But it must also be composed of other parts. To understand this, we have to go back more than 100 years.
First, the emergence of the relay
A hundred years ago, Samuel Morse was known for two inventions in his life: one was the Morse code; the other was the telegraph . What the telegraph does is simple: take some action on one end of the line and make some changes on the other end of the line. The invention of the telegraph marked the beginning of modern communication.
In telegraph lines, the current gets weaker as the circuit wires get longer. To communicate over long distances, it is also necessary to solve the problem of resistance caused by long wires, hence the emergence of relays. As shown in the picture below: the current enters from the input and the iron rod becomes a magnet, pulling the movable lever down. At this time, the switch in the relay is closed, because the V terminal provides a voltage, and the weaker current at the input end is amplified into a stronger output current.
In the early days of the relay, it only played the role of amplifying the circuit signal in the telegraph line. But we need to apply it to the computer and use it to complete more important work.
Although relays have been replaced by transistors in modern computers, they all work the same way.
A relay is like a switch, in charge of whether the circuit is connected or not. But it is not just a switch, because its closing and opening do not need our manual control, but through the current to control.
Another important point is that the relay has one more exposed interface than the switch. In addition to the input and output terminals, there is also the uppermost V-terminal input. It is because of this V terminal that there are many different connection methods between the two relays, which are then combined into different forms of relay groups.
2. Logic gate
Different connection methods of two or more relays can be combined into various logic gates . As the name implies, just like a door in life, a logic gate prevents or allows current to pass through. Let's take a look at four common logic gates.
1. AND gate
The AND gate is connected as follows.
It can be seen that the output terminal of the upper relay provides the V terminal voltage for the lower relay. In this circuit, the current at ③ can flow to the light bulb only when the switches at both input ends are closed to light the light bulb. Closing the switch anywhere, or not closing it at all, is not enough to make the line connect.
The AND gate formed by the connection of two relays can be simplified into the following symbols.
Not only does this symbol show that there are two relays in the circuit, it also means that the relay above is connected to the power supply, and that both relays are grounded.
2. OR gate
The OR gate is connected as follows.
In this circuit, the closing of any of the input switches will provide voltage to the bulb, causing it to light.
The OR gate formed by the connection of two relays can be simplified to the following symbol.
3. NAND gate
The NAND gate only turns off the light bulb if both switches are closed. In all other cases, the bulb is lit.
The NAND gate formed by the connection of two relays can be simplified to the following symbol.
4. NOR gate
The NOR gate will only light the bulb if both switches are open. In all other cases, the bulb is off.
The NOR gate formed by connecting two relays can be simplified to the following symbol.
So far, we've seen four different ways to connect a relay with two inputs and one output. These four different connection methods form four basic logic gates, namely AND gate, OR gate, NAND gate and NOR gate.
We agree that the closing of the switch is represented as 1, the opening is represented as 0, the lighting of the light bulb is represented as 1, and the off is represented as 0. Then the input-output relationship of the four logic gates can be summarized as the following table.
Now we are more convinced that relays do more than just switches. Because we can't use two switches to complete such a rich circuit selection operation anyway.
If the output of the relay is connected in a different way, we can also get another device commonly used in computers: the inverter.
The input signal of the inverter is opposite to the output signal. We can also use the following notation in place of the reverser.
Now that we have completed all the preparations for constructing an adder, the next thing to consider is how to construct an adder using the 4 basic logic gates and inverters we know.
3. Adder
Imagine how we added the decimal numbers 425 and 286 in an elementary school math class.
Starting from the leftmost column, we need to perform a total of three addition operations. The addition operation in the leftmost column has only two addends, and the middle column has three addends, because the two numbers in the first column will be added with a carry.
Here we don't consider decimal addition for now. Because from the above logic gate, only the binary operation sum is the most suitable for the circuit we have learned.
The steps for binary addition are the same as in decimal, from left to right, adding every two numbers.
When we learn decimal addition, there is a decimal addition table. Binary also has its own addition table of course, and is much simpler than the addition table for decimal. As shown below.
It can be seen that the binary addition of two one-bit results has only two bits. We call the bit on the left the addition bit; the bit on the right is called the carry bit. Let's take these two digits individually and tabulate them as follows.
First let's look at the carry table. It is not difficult to find that it is the same as the input and output relationship table of the AND gate.
This is a surprising discovery, we can use a circuit we have learned to implement the operation of the carry bit. However, among the four basic logic gates, there is no relation table of a gate to realize the operation of the addition bit.
Here is the last commonly used logic gate: the XOR gate. It does not belong to the basic logic gate, because it is connected by the combination of basic logic gates we have learned. The schematic diagram is as follows.
The symbol of the XOR gate is as follows.
The relationship table of the XOR gate is as follows.
It can be seen that the relation table of the XOR gate is exactly the same as the operation table of the addition bit.
Now we can complete the addition of two one-bit binary numbers through the combination of XOR gate and AND gate. Let two one-bit binary numbers (A and B) be the input signals of the XOR gate and the AND gate, respectively. The connection circuit is as shown in the figure below.
A circuit composed of an XOR gate and an AND gate in this connection is called a half adder (hereinafter referred to as a half adder). The half adder can also be simplified to the following notation.
Why is it called a half adder? In fact, the half adder is also a kind of adder, but because of its lack of function, the half adder can only add two binary numbers, that is to say, the half adder can only be used for the addition of the rightmost column of binary addition. Because the half adder has only two inputs. For other columns, due to the existence of the carry bit, the three numbers need to be added, and the half adder cannot be used at this time.
So is there a circuit combination that can add two numbers and three numbers at the same time? The answer is yes. We connect two half adders and an OR gate as follows.
For simplicity, we replace the above figure with the following symbols, and name it full adder (hereinafter referred to as full adder).
The full adder has three inputs and two outputs. When the carry input is 0, it is equivalent to a half adder. When the carry input is 1, the addition of three binary bits is realized. The following table lists all cases of full adder input combinations and output results.
Here the 8 full adders are connected as follows.
The circuit connected as above is called an 8-bit binary adder.
Now let's do some instructions on how to use this adder. In this 8-bit binary adder, the A-terminal and B-terminal inputs represent each digit in the two addends, respectively. The S terminal represents the addition and output. Since there is no carry in the addition of two numbers in the first column on the right when adding binary numbers, the carry input terminal here should always be set to 0. If the sum of the two binary numbers is beyond the representation range of the 8-bit binary numbers, the ninth position is 1, that is, the carry output terminal is 1.
For simplicity, we simplify the 8-bit binary adder as shown below.
So far, we have constructed an 8-bit binary adder. If you are not satisfied with adding 8-bit binary numbers, you can cascade two 8-bit binary adders into a 16-bit binary adder, as shown below.
At the beginning of this article, it has been said that binary addition is the most basic operation in computers. But understanding addition is not enough. Because the problems encountered in daily life are far more complicated than addition operations. For the adder to do more work, more devices must be used.
Modern computers can do nothing more than just use an adder.
The content of this article is referenced from "Code"