Who actually does the adding?
Last time we saw the "add" command. But who actually does that adding? And subtraction, and comparison? Let's meet the worker inside the CPU that handles only calculation, the ALU.
There's a part that only does calculation
Inside the CPU
there's a part
that handles only calculation.
Its name is the ALU.
Give it two numbers
and "which calculation to do,"
and it does that calculation
and produces a result.
It's like the heart of a calculator.
Two numbers go in the top, the result comes out the bottom.
ALU is short for
"arithmetic logic unit."
The name is grand,
but what it does is clear.
With the two numbers it gets,
do the calculation it's told.
Only that.
Pick the kind of calculation with a dial
The ALU is clever
because it doesn't do just one thing.
Addition,
subtraction,
and even comparison.
Turn the "which calculation" dial
and pick what you want,
and the same two numbers
give a different result.
Try turning it.
Put in two numbers and pick a calculation.
You only changed the dial,
yet the same worker
did a different calculation, right?
This "which calculation"
is actually set by
last time's instruction.
The adder from lesson 6 lives here
Open up the ALU
and there's something familiar.
It's the adder we built in lesson 6.
Addition is handled by that adder.
Subtraction, comparison, and
logic operations like and, or,
it's a neighborhood
where small calculation circuits live together.
The calculation circuits gathered inside the ALU.
The small circuits we built
one by one in lessons 5 and 6
were, in fact, the parts
that make up this ALU.
Small things gathered
into one calculation worker.
"Which is bigger" is answered by the ALU too
Among the ALU's jobs,
as important as calculation is comparison.
"Is this number bigger than that one?"
"Are the two numbers equal?"
To questions like these
it answers true or false.
This single true or false
later becomes the seed
of the fork called "if."
True is 1, false is 0. To a computer this too is, in the end, 0s and 1s.
"Is 10 bigger than 7?"
The answer is true.
This simple judgment
is enormously important.
A program looking at a situation
and choosing a path
all starts from this comparison.
The instruction turns the dial
Now two areas meet.
Remember last time's instruction?
The front part of "add,"
that operation code,
turns the ALU's dial itself.
"Add" sets it to addition,
"subtract" to subtraction.
The command tells the ALU
which calculation to do.
An instruction's operation code → the ALU's dial.
The instruction decides what to do,
and the ALU does that calculation.
Now the CPU's two core parts
have come together.
Next time we'll look into
how these two work as one body,
the whole CPU.