Hello and welcome to my small series about
computers. In this episode, we will take a look at the
construction of a computer from individual transistors up to a whole CPU. We will also see how a CPU works, and how
we can give it commands. So stay tuned for awesome tech stuff. Understanding a computer isn't that complicated. It's just as easy as moving your body. When you were younger, you probably were wondering
how you can move your body, but you didn't know the answer, so you probably thought of
something like magic. Later you learned that we have muscles which
can tense and relax. So let's go under the skin and take a quick
look at them. Basically, a muscle is made up of a bunch
of fascicles bundled together, which are by themselves just made up of a bunch of muscle
fibers, but even they are made up of a bunch of myofibrils. So ultimately a muscle is nothing more than
a bunch of strings that are bundled together and then bundled again and again. Or just a bundle of a bundle of a bundle of
myofibrils. Let's go back to computers after all this
is what this video is about, but aren't they human after all? However, it's the same principle for computers
but with fewer bundles and more circuits. The smallest component of a computer is a
transistor, and if you combine a few of them, you can build new circuits, which can be used
to create even more advanced circuits until you made a whole computer. Essentially, a transistor is nothing more
than a switch (Not that Switch, couldn't they choose a better name?). If it's activated current can flow and if
it's deactivated current can not flow. It consists of 3 connections called base, collector, and emitter and a
semiconductor. All three electrodes are connected to the
semiconductor, which sometimes conducts and sometimes not (hence it's name). But with the base electrode, we can control
the semiconductor, just like flipping a switch with your finger. If enough voltage is applied to the base electrode,
current can flow between emitter and collector. Just like a dam can control the flow of water. So you may ask yourself now why we need simple
switches to build a computer. First, you can fill long Wikipedia articles
with them, second, they can switch billions of times per second, and lastly, you can build
pretty awesome stuff with them. For example, if you put billions of transistors
together, wrap them with plastic and put a display on top, you can build one giant switch
out of many tiny switches (Oh, that joke got old pretty soon, but I won't switch that running
gag). Or basically any other computer. But let's start with way more primitive circuits. What could be better than one transistor? Two transistors! Let's put one behind another and voila you
just made a breakthrough in computer science by inventing the AND-gate. Now we can create a truth table for all inputs
and outputs. This fancy circuit only conducts electricity
if both bases or inputs are activated. If both or one input is deactivated or false,
the output is false too. Totally not confusing. Let me clarify things a bit. Current is the flow of electrons from one
place to another. In computer science, we say '1' to electrons
and '0' if there isn't any current. And when were are talking about logic gates,
which perform logic operations, like our AND-gate, we use the terms 'true' and 'false' instead
of '1' and '0'. So there are just a few different terms that
flow down the river, but they all mean basically the same. There are more logic gates that we can build. If we can put them behind each other, what
could we also do with them? Why not put them next to each other. And again we built a new logic gate, the OR-gate. Current can flow if any of the inputs are
true. There is one more logic gate left that we
can build with two or fewer transistors. In fact, we need only one transistor and a
clever trick, like the criminal who is just stealing your wallet. Instead of using the collector as an output,
we add an extra wire and use it as an output. If the transistor is deactivated, we get true
as an output because the electrons don't have any other way. But if we activate our transistor, the electrons
flow to the emitter, because they are lazy and don't want to go a detour. Therefore we get false as an output. The NOT-gate converts every signal, just like
a criminal trying to turn false money into 'real' money. There are four more logic gates, but we will
only take a look at one more, the XOR-gate. Actually, we won't take a look at its construction,
but we will take a look at its truth table. Aren't these logic gates awesome? Oh, still boring? Then it's time to use multiple logic gates
to build even more complex circuits. But engineers are lazy like mathematicians. They say three times four instead of four
plus four plus four. They can't even write pi out. It is a cakewalk just pie. Remember our muscle from the beginning? We put a bunch of myofibrils together and
called it muscle fiber, so we don't have to care about individual myofibrils. And we do the same with transistors. Mathematicians have a special symbol for multiple
additions, and we have special symbols for our logic gates. A long D for an AND-gate, a pointed D for
an OR-gate, a triangle with a dot for the NOT-gate and a pointed D with a curve behind
it for the XOR-gate. This principle of putting smaller parts together
to create a new unit, so you don't have to care about individual parts, is called abstraction. This principle is important in computer science
or do you want to build a modern CPU like a giant puzzle out of billions of transistors. For us, it's time to move to a higher level
of abstraction and say goodbye to our beloved transistors. It's time to build something that can actually
calculate for us. Let's start with the simple addition of 2
single digit binary numbers. The table would look like this. 2 isn't a binary number, so we use the same
trick from our decimal system. If we have no numbers left, we put a new digit
to the left side and start again. If you compare this table with the tables
from our logic gates, can you tell me which two logic gates do we need? One XOR-gate for the first digit and one OR-gate
for the second digit. This circuit is called half adder. Why half? Well, because when you add two numbers, you
have to add sometimes three numbers at once. Let me give you an example. Let's add 11 and 11. We want only one output per digit, so we move
the '1' also called carry-bit to our next digit, but now we have to add three numbers
which our half adder can't handle. We need a new circuit. Let's move up to a higher level of abstraction
and use or new half adder to build a full adder. We can use one half-adder to get the sum of
B and C and another one to add the sum of B and C to A. Then we need only one OR-gate
to check if any half adder has a carry bit. We can move to an even higher level of abstraction
and connect our full adders. Now we can add any 8-digit binary numbers
faster than mental arithmetic. If we want to build a machine capable of adding,
multiplication, subtraction, and division we only need a new circuit which can subtract
because multiplication is just a series of addition and division is subtracting until
the leftover is smaller than the divisor. To keep things simple we use a bit of magic
and create one. That's all we need to build the heart of a
CPU the arithmetic logic unit or ALU for short. An ALU has a bit more functions than just
addition and subtraction. For example, we can add or subtract 1, can
apply AND OR or XOR logic on every digit of two numbers and even more. The cool thing about the ALU is that we can
control which operation it performs. Therefore we need three inputs, two for our
numbers and one to specify which operation should be executed. It has two outputs, one for the result and
the other one for additional information like if our result is zero, negative or too large
for our ALU. If we add 10000000 and 1000(The same number)
with an 8bit ALU, our result would be 0 because the 9th digit gets cut off. But the other output gives us the information
that the overflow flag and the zero flag was activated. A buffer overflow is like, trying to get as
much as possible in your luggage but once it's full, you can't add more, or your luggage
could explode! You have to leave the rest at home or get
a bigger suitcase. So if we want to give our ALU instructions,
we need an opcode. Our opcodes could look like this. So if we give our ALU the opcode 0100 and
these two numbers 00001010, 01011101, we get 01100111 as a result. Great but we can't do multiplication and division
ore more than one instruction without having to give new inputs. It's time to move once again to a higher level
of abstraction and build a fully working Central Processing Unit. We need to connect our ALU to a control unit,
give it a few registers to save intern results and lastly we need to connect it to RAM. The control unit receives our commands and
then decides what our ALU or RAM has to do. RAM means random access memory because we
can access its data in a random order unlike tape drives, CDs or DVDs. It's also a volatile memory, which means it
loses its data when it's not powered. That's why you always have to save your data
when you are working on it. The power could go off, or your computer could
crash. (BSOD) If we want to give our CPU instructions,
we need to know its instruction set in the first place. Ours could look like this. It works like the opcodes from our ALU but
is a bit more complicated so let's go through a program together. Oh, we don't have one yet, then let's write
one together. Let's start with a very simple program adding
two numbers together. First, we need to get our numbers from our
RAM into the CPUs registers. So we need to specify where we want to save
them in our RAM. Let's use the two last memory locations for
that. So the first two lines of our program are
made up by the opcode and the memory address. For our third line, we need the ADD opcode
and two register ID's, which could be the following. Now it's time to save our result into RAM. Our result was stored in the second register
from our addition, so we need its STORE instruction and let's use the memory address before our
numbers to save it. Wait, our program isn't finished yet we need
to stop it with a HALT instruction. So let's run our program. The control unit recognizes the LOAD_A instruction,
so it tells the RAM to load memory location 14 into register A which has now the information
01001110 or 78 in decimal. The same thing happens with the next instruction
and register B holds now the information of memory location 15, 10100100 which is 164
in decimal. Now our control unit understands an ADD instruction
and adds the two registers with the ID's 01 and 10. Our result gets stored in the second register
and B holds now 11110010. Next, the STORE_B instruction is being executed
and memory location 13 stores now 11110010 or 242. Now the control unit gets the HALT instruction
and stops our program. In the early days, results were printed on
paper, but today they are displayed in a console or even a GUI. Congratulations you wrote your first program
in machine language or machine code. But today only very few programs are written
in machine language. Machine code has the advantage that computers
can read them with ease but not humans. Could you tell me which bit I flipped? Finding mistakes or bugs in machine code is
very hard, so programmers needed a way to code faster and with fewer bugs. They needed a new language, and that's what
we will discuss in the next episode. Let's summarize everything so you can remember
it better. The transistor is the smallest part of a computer,
and it works like a switch(The other switch!). We can control the flow of electrons with
it. If we put a few of them together, we can create
some logic gates, capable of basic logic operations. With these logic gates, we can build circuits
that can do addition, subtraction or a hell of a lot more. If we put these circuits together with more
logic gates, we can build an ALU, which can do addition, subtraction and more like bitwise
logic operations. When the ALU is connected with a control unit,
some registers, and RAM, it becomes a CPU. We can load our programs and data into RAM. The program is written in machine language,
which consists only of '1' and '0'. The instruction set contains all commands
our CPU can execute. And that's how a computer works without going
into too much detail to make it enjoyable and understandable for humans. Just imagine what would happen if we take
the next step and start connecting computers together. I hope you learned something about computers
and muscles, your biology teacher would be proud of you. Thanks for watching. If you are interested in computer science
or programming, I can really recommend this crash course about computer science. It really helped me a lot to make this video
possible, and it covers this topic with more detail and historical context. It also features topics that I haven't and
won't talk about in this series. And it has some nice animations but isn't
fully animated. So you have to see another human being talking
to a camera. If this is still not appealing enough to you,
I will link a computer science playlist somewhere. See you in the next episode, when we will
discuss programming languages.