(English) How Computers Calculate - The ALU - Crash Course Computer Science #5 (DownSub - Com)
(English) How Computers Calculate - The ALU - Crash Course Computer Science #5 (DownSub - Com)
or manipulating numbers in a structured and purposeful way, like adding two numbers
together.
When you understand an ALU’s design and function, you’ll understand a fundamental
This is perhaps the most famous ALU ever, the Intel 74181.
It was the first complete ALU that fit entirely inside of a single chip -
So today we’re going to take those Boolean logic gates we learned about last week
to build a simple ALU circuit with much of the same functionality as the 74181.
this to construct a computer from scratch. So it’s going to get a little bit
complicated,
INTRO
Let's start with the arithmetic unit, which is responsible for handling all
numerical operations in a
add one to a number, which is called an increment operation, but we’ll talk about
those later.
Today, we’re going to focus on the pièce de résistance, the crème de la crème of
operations that underlies almost everything else a computer does - adding two
numbers together.
The simplest adding circuit that we can build takes two binary digits, and adds
them together.
So we have two inputs, A and B, and one output, which is the sum of those two
digits.
1+0 = 1
0+1 = 1
So this set of inputs exactly matches the boolean logic of an XOR gate, and we can
use it as
but there’s no 2 digit in binary, so as we talked about last episode, the result is
0 and the 1 is carried to the next column. So the sum is really 10 in binary.
Now, the output of our XOR gate is partially correct - 1 plus 1, outputs 0.
The carry bit is only “true” when the inputs are 1 AND 1, because that's the only
It's not that complicated - just two logic gates - but let’s abstract away even
this level
of detail and encapsulate our newly minted half adder as its own component, with
two
inputs - bits A and B - and two outputs, the sum and the carry bits.
This takes us to another level of abstraction… heh… I feel like I say that a lot.
we’re going to need a “Full Adder.” That half-adder left us with a carry bit as
output.
and every column after that, we are going to have to add three bits together, no
two.
We can build a full adder using half adders. To do this, we use a half adder to add
A plus B
Lastly, we need a OR gate to check if either one of the carry bits was true.
That’s it, we just made a full adder! Again,we can go up a level of abstraction and
wrap
Armed with our new components, we can now build a circuit that takes two, 8-bit
numbers
It's possible there was a carry from the previous addition of A0 and B0, so this
time we need
Then, we take any carry from this full adder, and run it into the next full adder
that handles
this is called an 8-bit ripple carry adder. Notice how our last full adder has a
carry out.
If there is a carry into the 9th bit, it means the sum of the two numbers is too
large to fit into 8-bits.
Famously, the original PacMan arcade game used 8 bits to keep track of what level
you were on.
This meant that if you made it past level 255 – the largest number storablein 8
bits –
This caused a bunch of errors and glitches making the level unbeatable.
The bug became a rite of passage for the greatest PacMan players.
So if we want to avoid overflows, we can extend our circuit with more full adders,
allowing
us to add 16 or 32 bit numbers. This makes overflows less likely to happen, but at
the
expense of more gates. An additional downside is that it takes a little bit of time
for
each of the carries to ripple forward.
Admittedly, not very much time, electrons move pretty fast, so we’re talking about
billionths of a second,
For this reason, modern computers use a slightly different adding circuit called a
‘carry-look-ahead’ adder
which is faster, but ultimately does exactly the same thing-- adds binary numbers.
The ALU’s arithmetic unit also has circuits for other math operations
And like our adder, these other operations are built from individual logic gates.
Interestingly, you may have noticed that there are no multiply and divide
operations.
That's because simple ALUs don’t have a circuit for this, and instead just perform
a series of additions.
That’s the same thing as adding 12 to itself 5 times. So it would take 5 passes
through
And as you might expect, the circuit is more complicated than addition -- there’s
no
Ok, let’s move on to the other half of the ALU: the Logic Unit.
Instead of arithmetic
operations, the Logic Unit performs… well...
logical operations, like AND, OR and NOT, which we’ve talked about previously.
Even if one single bit is 1, we know the number can’t be zero and then we use a
final NOT gate to flip this
So that’s a high level overview of what makes up an ALU. We even built several of
As you saw, it’s just a big bunch of logic gates connected in clever ways.
Which brings us back to that ALU you admired so much at the beginning of the
episode.
Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs,
We didn’t build the whole thing… but you get the idea.
The 74181 used about 70 logic gates, and it couldn’t multiply or divide.
But it was a huge step forward in miniaturization, opening the doors to more
capable and less expensive computers.
but our 8-bit ALU would require hundreds of logic gates to fully build and
engineers
don’t want to see all that complexity when using an ALU, so they came up with a
special
Our 8-bit ALU has two inputs, A and B, each with 8 bits. We also need a way to
specify what operation the ALU should perform,
We’ll talk about this more in a later episode, but in brief, 1000 might be the
command
to add, while 1100 is the command for subtract. Basically, the operation code tells
the ALU
what operation to perform. And the result of that operation on inputs A and B is an
8-bit output.
ALUs also output a series of Flags, which are 1-bit outputs for particular states
and statuses.
For example, if we subtract two numbers, and the result is 0, our zero-testing
circuit, the one we made earlier,
sets the Zero Flag to True (1). This is useful if we are trying to determine if two
numbers are are equal.
we can use the ALU to calculate A subtract B and look to see if the Negative Flag
was set to true.
And finally, there’s also a wire attached to the carry out on the adder we built,
so if there is an overflow, we’ll know about it. This is called the Overflow Flag.
Fancier ALUs will have more flags, but these three flags are universal and
frequently used.
So now you know how your computer does all its basic mathematical operations
digitally
We’re going to use this ALU when we construct our CPU two episodes from now.
But before that, our computer is going to need some memory! We'll talk about that
next week.