How Computers Calculate - the ALU: Crash Course Computer Science #5
Summary
TLDR这段视频脚本来自Crash Course计算机科学系列,由Carrie Ann主讲。视频主要介绍了计算机中的算术逻辑单元(ALU),它是计算机执行所有计算的核心部分。ALU由算术单元和逻辑单元两部分组成,负责处理加法、减法等数值操作,以及逻辑运算如AND、OR和NOT。视频中详细解释了如何使用逻辑门(如AND、OR、NOT和XOR门)构建一个简单的ALU电路,并逐步展示了如何构建一个全加器,以及如何将这些组件组合起来形成一个8位的全加器电路。此外,还讨论了溢出问题,以及现代计算机中使用的更快的“进位预测加法器”。最后,视频还涉及了ALU的另一个重要组成部分——标志位,它们用于指示特定状态和状态。整个视频以深入浅出的方式,让观看者理解了计算机如何使用逻辑门进行数字计算,为构建CPU和理解计算机内存打下了基础。
Takeaways
- 📐 **二进制表示法**:计算机使用二进制来表示和存储数字,例如00101010在二进制中表示42。
- 🧮 **算术逻辑单元(ALU)**:ALU是计算机的数学大脑,负责执行所有的计算操作。
- 🏆 **Intel 74181**:这是最著名的ALU之一,1970年发布时是首个完全集成在单个芯片上的完整ALU。
- 🔍 **布尔逻辑门**:使用AND、OR、NOT和XOR等逻辑门构建简单的ALU电路。
- 🛠️ **半加器与全加器**:半加器用于处理两个二进制位的加法,而全加器则处理多一位的进位。
- 🔗 **8位进位加法器**:通过将全加器串联起来,可以构建一个能够处理两个8位数字相加的电路。
- 🚫 **溢出问题**:当两个数字的和超出8位所能表示的范围时,会发生溢出,导致错误和异常行为。
- 🏎️ **Pac-Man游戏溢出**:原始的Pac-Man街机游戏因为8位ALU溢出,导致超过255级时出现错误和无法完成的关卡。
- ⏫ **扩展电路以避免溢出**:通过增加更多的全加器,可以处理16位或32位数字的加法,减少溢出的可能性。
- 🚀 **现代计算机的加法电路**:现代计算机使用更快的“进位预测”加法器,尽管其基本功能与普通加法器相同。
- 🔢 **ALU的算术和逻辑操作**:ALU的算术单元执行数学运算,而逻辑单元执行逻辑运算和简单的数值测试。
- 🏁 **ALU的标志位**:ALU输出包括一系列的标志位,用于指示特定状态和状态,如零标志、负数标志和溢出标志。
Q & A
什么是计算机中的算术逻辑单元(ALU)?
-算术逻辑单元(ALU)是计算机中负责执行所有计算的部件,它包括算术运算单元和逻辑运算单元,负责处理加法、减法等数值操作,以及逻辑运算如AND、OR和NOT。
ALU的设计和功能理解为什么对理解现代计算机至关重要?
-理解ALU的设计和功能是理解现代计算机的基础,因为ALU执行了计算机所有的基本数学运算,这些运算是计算机处理信息和执行程序的核心部分。
Intel 74181是什么,它在计算机历史上有什么重要性?
-Intel 74181是一种著名的ALU,它在1970年发布时是第一个完全集成在单个芯片上的完整ALU,这在当时是一个巨大的工程成就,为更小型化、更强大、更便宜的计算机的发展铺平了道路。
如何使用布尔逻辑门构建一个简单的ALU电路?
-可以使用AND、OR、NOT和XOR等布尔逻辑门来构建一个简单的ALU电路。例如,通过组合这些逻辑门,可以构建一个半加器和全加器,这些组件可以进一步组合来执行更复杂的计算。
什么是半加器,它如何工作?
-半加器是一种简单的电路,它接受两个二进制位作为输入,并计算它们的和以及进位位。它由一个XOR门来计算和,以及一个AND门来计算进位位。
全加器与半加器有什么区别?
-全加器比半加器复杂,它接受三个输入位(A、B和进位位C),并计算这三个位的和以及新的进位位。全加器使用半加器和OR门来实现。
什么是8位进位加法器,它是如何工作的?
-8位进位加法器是一种电路,它能够对两个8位的二进制数进行加法运算。它通过将每一位的加法结果和进位位串联起来,形成一条“进位链”,从而实现对整个8位数的加法运算。
溢出是什么,它为什么会在计算机中发生?
-溢出是指当两个数相加的结果太大,以至于无法用你正在使用的位数来表示时发生的情况。它通常会导致错误和意外的行为,因为超出了计算机能够处理的数值范围。
为什么现代计算机使用'进位预测'加法器而不是简单的进位加法器?
-现代计算机使用'进位预测'加法器因为它比简单的进位加法器更快。尽管它们执行的基本功能相同——即加法运算——但进位预测加法器通过预测进位位来减少等待进位位逐级传递的时间。
ALU中的逻辑单元执行哪些类型的操作?
-ALU中的逻辑单元执行逻辑操作,如AND、OR和NOT,以及进行简单的数值测试,比如检查一个数是否为负数或是否为零。
什么是操作码,它在ALU中有什么作用?
-操作码是ALU用来指定应该执行哪种操作的一段代码,比如加法或减法。它告诉ALU根据给定的输入执行特定的运算。
ALU输出的标志位有哪些,它们分别表示什么?
-ALU输出的标志位包括零标志位(Zero Flag)、负数标志位(Negative Flag)和溢出标志位(Overflow Flag)。零标志位在结果为零时设置为真,负数标志位在结果为负数时设置为真,溢出标志位在发生溢出时设置为真。
Outlines
😀 计算机科学速成课:算术逻辑单元(ALU) 简介
Carrie Ann介绍了计算机如何使用二进制表示数字,并强调了计算——即以结构化和有意义的方式来操作数字,如加法——的重要性。她提到了计算机的算术逻辑单元(ALU),这是计算机的数学大脑。ALU由算术单元和逻辑单元组成,其中算术单元负责处理所有数值运算,如加法和减法。逻辑单元执行逻辑运算,如AND、OR和NOT。Carrie Ann还提到了使用布尔逻辑门构建简单ALU电路的计划,并概述了如何使用这些电路构建一台计算机。她介绍了一个简单的半加器电路,它使用XOR门来处理两个二进制位的加法,并使用AND门来生成进位位。
📚 构建全加器和处理溢出
为了处理超过两位的加法,需要构建一个全加器,它接受三个输入位:A、B和C。全加器使用半加器和OR门来处理进位位。全加器的设计允许我们构建一个能够处理8位数字加法的电路,称为8位进位链加法器。Carrie Ann解释了进位位如何逐位传递,导致溢出的概念,即当两个数字的和太大而无法用8位表示时。她举了PacMan街机游戏的溢出错误作为例子。为了避免溢出,可以通过增加全加器的数量来扩展电路,以处理16位或32位的数字。此外,现代计算机使用一种更快的加法电路,称为“进位前瞻”加法器。ALU的算术单元还包括其他数学运算的电路,而逻辑单元则执行逻辑运算和简单的数值测试。
🖥️ ALU的标志和未来的CPU构建
Carrie Ann讨论了ALU的标志,包括零标志、负数标志和溢出标志,这些标志是单比特输出,用于特定状态和状态。她提到,更复杂的ALU会有更多的标志,但这三种标志是通用且经常使用的。标志在确定两个数字是否相等或比较它们的大小时非常有用。最后,她提到了未来节目中将使用的ALU,并预告了接下来将讨论计算机内存的主题。
Mindmap
Keywords
💡二进制
💡算术逻辑单元(ALU)
💡英特尔74181
💡逻辑门
💡半加器
💡全加器
💡溢出
💡进位 lookahead 加法器
💡逻辑单元
💡操作码
💡标志位
Highlights
计算机中的数字以二进制形式表示,例如00101010在二进制中代表十进制的42。
计算机的真正目标是计算,即以结构化和有目的的方式操作数字,如将两个数字相加。
计算机的算术逻辑单元(ALU)负责处理这些操作,它是计算机的数学大脑。
Intel 74181是最著名的ALU之一,1970年发布时是首个完全集成在单个芯片上的完整ALU。
使用布尔逻辑门构建简单的ALU电路,与74181具有相似的功能。
ALU实际上是两个单元的组合:算术单元和逻辑单元。
算术单元负责处理计算机中的所有数值运算,如加法和减法。
通过构建一个简单的半加器电路,可以处理两个二进制位的加法。
全加器比半加器复杂,它接受三个输入位:A、B和C。
使用半加器和全加器可以构建一个电路,用于将两个8位的数字A和B相加。
8位进位加法器被称为“8位进位链加法器”,因为进位位会向前传递到下一个加法器。
溢出发生在加法结果太大,无法用所使用的位数表示时,这可能导致错误和意外行为。
现代计算机使用一种更快的加法电路,称为“进位先看”加法器。
ALU的算术单元还有其他数学运算的电路,通常支持8种操作。
简单的ALU没有乘法和除法电路,而是通过一系列加法来执行。
更高级的处理器拥有专用的乘法电路,比加法更复杂,需要更多的逻辑门。
逻辑单元执行逻辑运算,如AND、OR和NOT,以及简单的数值测试,如检查数字是否为负数。
ALU由一系列逻辑门以巧妙的方式连接而成,工程师使用特殊的符号来简化其复杂性。
ALU有两个输入A和B,每个输入有8位,并通过4位的操作码指定要执行的操作。
ALU还输出一系列标志位,用于特定状态和状态的1位输出。
更高级的ALU会有更多的标志位,但零标志位、负标志位和溢出标志位是通用且经常使用的。
计算机使用ALU执行所有基本的数学运算,全部数字化,无需齿轮或杠杆。
Transcripts
Hi, I’m Carrie Ann and this is Crash Course Computer Science.
So last episode, we talked about how numbers can be represented in binary. Representing
Like, 00101010 is 42 in decimal.
Representing and storing numbers is an important function of a computer, but the real goal is computation,
or manipulating numbers in a structured and purposeful way, like adding two numbers together.
These operations are handled by a computer’s Arithmetic and Logic Unit,
but most people call it by its street name: the ALU.
The ALU is the mathematical brain of a computer.
When you understand an ALU’s design and function, you’ll understand a fundamental
part of modern computers. It is THE thing that does all of the computation in a computer,
so basically everything uses it.
First though, look at this beauty.
This is perhaps the most famous ALU ever, the Intel 74181.
When it was released in 1970, it was
It was the first complete ALU that fit entirely inside of a single chip -
Which was a huge engineering feat at the time.
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.
And over the next few episodes we’ll use
this to construct a computer from scratch. So it’s going to get a little bit complicated,
but I think you guys can handle it.
INTRO
An ALU is really two units in one -- there’s an arithmetic unit and a logic unit.
Let's start with the arithmetic unit, which is responsible for handling all numerical operations in a
computer, like addition and subtraction. It also does a bunch of other simple things like
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.
We could build this circuit entirely out of
individual transistors, but that would get confusing really fast.
So instead as we talked about in Episode 3 – we can use a high-level of abstraction and build our components
out of logic gates, in this case: AND, OR, NOT and XOR gates.
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.
Just to clarify: A, B and the output are all single bits.
There are only four possible input combinations.
The first three are: 0+0 = 0
1+0 = 1 0+1 = 1
Remember that in binary, 1 is the same as true, and 0 is the same as false.
So this set of inputs exactly matches the boolean logic of an XOR gate, and we can use it as
our 1-bit adder.
But the fourth input combination, 1 + 1, is a special case. 1 + 1 is 2 (obviously)
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.
But, we need an extra output wire for that carry bit.
The carry bit is only “true” when the inputs are 1 AND 1, because that's the only
time when the result (two) is bigger than 1 bit can store… and conveniently we have
a gate for that! An AND gate, which is only true when both inputs are true, so
we’ll add that to our circuit too.
And that's it. This circuit is called a half adder. It’s
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.
I wonder if this is going to become a thing.
Anyway, If you want to add more than 1 + 1
we’re going to need a “Full Adder.” That half-adder left us with a carry bit as output.
That means that when we move on to the next column in a multi-column addition,
and every column after that, we are going to have to add three bits together, no two.
A full adder is a bit more complicated - it
takes three bits as inputs: A, B and C. So the maximum possible input is 1 + 1 + 1,
which equals 1 carry out 1, so we still only need two output wires: sum and carry.
We can build a full adder using half adders. To do this, we use a half adder to add A plus B
just like before – but then feed that result and input C into a second half adder.
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
up this full adder as its own component. It takes three inputs, adds them, and outputs
the sum and the carry, if there is one.
Armed with our new components, we can now build a circuit that takes two, 8-bit numbers
– Let’s call them A and B – and adds them together.
Let’s start with the very first bit of
A and B, which we’ll call A0 and B0. At this point, there is no carry bit to deal
with, because this is our first addition. So we can use our half adder to add these
two bits together. The output is sum0. Now we want to add A1 and B1 together.
It's possible there was a carry from the previous addition of A0 and B0, so this time we need
to use a full adder that also inputs the carry bit. We output this result as sum1.
Then, we take any carry from this full adder, and run it into the next full adder that handles
A2 and B2. And we just keep doing this in a big chain until all 8 bits have been added.
Notice how the carry bits ripple forward to each subsequent adder. For this reason,
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.
This is called an overflow.
In general, an overflow occurs when the result of an addition is too large to be represented by the number of bits you are using.
This can usually cause errors and unexpected behavior.
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 –
to level 256, the ALU overflowed.
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,
but that’s enough to make a difference in today’s fast computers.
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 in general these 8 operations are always supported.
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.
Let’s say you want to multiply 12 by 5.
That’s the same thing as adding 12 to itself 5 times. So it would take 5 passes through
the ALU to do this one multiplication. And this is how many simple processors,
like those in your thermostat, TV remote, and microwave, do multiplication.
It’s slow, but it gets the job done.
However, fancier processors, like those in your laptop or smartphone,
have arithmetic units with dedicated circuits for multiplication.
And as you might expect, the circuit is more complicated than addition -- there’s no
magic, it just takes a lot more logic gates – which is why less expensive processors
don’t have this feature.
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.
It also performs simple numerical tests, like checking if a number is negative.
For example, here’s a circuit that tests if the output of the ALU is zero.
It does this using a bunch of OR gates to see if any of the bits are 1.
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
input so the output is 1 only if the input number is 0.
So that’s a high level overview of what makes up an ALU. We even built several of
the main components from scratch, like our ripple adder.
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.
The Intel 74181.
Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs,
which means YOU BUILT AN ALU THAT’S LIKE
TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH YOUR MIND! Well.. sort of.
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.
This 4-bit ALU circuit is already a lot to take in,
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
symbol to wrap it all up, which looks like a big ‘V’. Just another level of abstraction!
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,
for example, addition or subtraction.
For that, we use a 4-bit operation code.
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.
If we wanted to test if A was less than B,
we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true.
If it was, we know that A was smaller than B.
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.
In fact, we’ll be using them soon in a future episode.
So now you know how your computer does all its basic mathematical operations digitally
with no gears or levers required.
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.
Просмотреть больше связанных видео
Registers and RAM: Crash Course Computer Science #6
The Central Processing Unit (CPU): Crash Course Computer Science #7
Boolean Logic & Logic Gates: Crash Course Computer Science #3
Intro to Algorithms: Crash Course Computer Science #13
Screens & 2D Graphics: Crash Course Computer Science #23
Early Programming: Crash Course Computer Science #10
5.0 / 5 (0 votes)