How Computers Calculate - the ALU: Crash Course Computer Science #5

CrashCourse
22 Mar 201711:09

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

00:00

😀 计算机科学速成课:算术逻辑单元(ALU) 简介

Carrie Ann介绍了计算机如何使用二进制表示数字,并强调了计算——即以结构化和有意义的方式来操作数字,如加法——的重要性。她提到了计算机的算术逻辑单元(ALU),这是计算机的数学大脑。ALU由算术单元和逻辑单元组成,其中算术单元负责处理所有数值运算,如加法和减法。逻辑单元执行逻辑运算,如AND、OR和NOT。Carrie Ann还提到了使用布尔逻辑门构建简单ALU电路的计划,并概述了如何使用这些电路构建一台计算机。她介绍了一个简单的半加器电路,它使用XOR门来处理两个二进制位的加法,并使用AND门来生成进位位。

05:01

📚 构建全加器和处理溢出

为了处理超过两位的加法,需要构建一个全加器,它接受三个输入位:A、B和C。全加器使用半加器和OR门来处理进位位。全加器的设计允许我们构建一个能够处理8位数字加法的电路,称为8位进位链加法器。Carrie Ann解释了进位位如何逐位传递,导致溢出的概念,即当两个数字的和太大而无法用8位表示时。她举了PacMan街机游戏的溢出错误作为例子。为了避免溢出,可以通过增加全加器的数量来扩展电路,以处理16位或32位的数字。此外,现代计算机使用一种更快的加法电路,称为“进位前瞻”加法器。ALU的算术单元还包括其他数学运算的电路,而逻辑单元则执行逻辑运算和简单的数值测试。

10:03

🖥️ ALU的标志和未来的CPU构建

Carrie Ann讨论了ALU的标志,包括零标志、负数标志和溢出标志,这些标志是单比特输出,用于特定状态和状态。她提到,更复杂的ALU会有更多的标志,但这三种标志是通用且经常使用的。标志在确定两个数字是否相等或比较它们的大小时非常有用。最后,她提到了未来节目中将使用的ALU,并预告了接下来将讨论计算机内存的主题。

Mindmap

Keywords

💡二进制

二进制是一种数字系统,它只使用两个数字0和1来表示所有的数值。在视频中,二进制被用来展示如何表示数字,例如00101010在二进制中表示十进制的42。二进制是计算机科学的基础,因为它是计算机内部处理和存储数据的方式。

💡算术逻辑单元(ALU)

算术逻辑单元(ALU)是计算机的数学大脑,负责执行所有的计算操作,如加法和逻辑运算。在视频中,ALU被描述为执行计算的核心部件,包括基本的数学运算和逻辑判断。

💡英特尔74181

英特尔74181是历史上著名的ALU,它在1970年发布,是首个完全集成在一个芯片上的完整ALU,这在当时是一个巨大的工程成就。视频中提到了这个ALU,以展示ALU技术的发展和重要性。

💡逻辑门

逻辑门是实现布尔逻辑运算的基本电子设备,包括AND、OR、NOT和XOR等。在视频中,逻辑门被用来构建简单的ALU电路,说明了如何使用这些逻辑门来执行基本的数学运算。

💡半加器

半加器是一种简单的电路,用于将两个二进制数字相加。它由两个逻辑门组成,输出两个比特位的和以及进位位。视频中通过构建半加器来展示如何开始构建更复杂的ALU电路。

💡全加器

全加器是一种更复杂的电路,它不仅可以处理两个比特位的加法,还可以处理从上一位传递过来的进位位。视频中提到全加器在多列加法中的重要性,以及如何使用半加器和额外的逻辑门来构建全加器。

💡溢出

溢出是指在加法运算中,如果两个数字的和超出了用于表示它们的位数所能容纳的最大值,就会发生溢出。视频中通过Pac-Man游戏的bug来说明溢出现象,以及它可能导致的错误和意外行为。

💡进位 lookahead 加法器

进位 lookahead 加法器是一种比传统的8位进位传递加法器更快的加法电路。视频中提到现代计算机使用这种加法器,因为它可以更快地执行加法运算,尽管它们在本质上执行的是相同的任务。

💡逻辑单元

逻辑单元是ALU的另一半,它执行逻辑运算,如AND、OR和NOT,以及简单的数值测试,比如检查一个数字是否为负数。视频中解释了逻辑单元的功能,并展示了如何使用逻辑门来构建测试ALU输出是否为零的电路。

💡操作码

操作码是一种用于指定ALU应执行的操作的代码,比如加法或减法。视频中提到了使用4位操作码来告诉ALU执行哪种操作,这是控制ALU行为的关键部分。

💡标志位

标志位是ALU输出的一系列单比特位,用于表示特定的状态和状态。例如,如果两个数字相减的结果为0,则零测试电路会将零标志位设置为真(1)。视频中提到了标志位在确定数字比较结果中的应用,如相等、小于等。

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

play00:03

Hi, I’m Carrie Ann and this is Crash Course Computer Science.

play00:05

So last episode, we talked about how numbers can be represented in binary. Representing

play00:09

Like, 00101010 is 42 in decimal.

play00:13

Representing and storing numbers is an important function of a computer, but the real goal is computation,

play00:19

or manipulating numbers in a structured and purposeful way, like adding two numbers together.

play00:23

These operations are handled by a computer’s Arithmetic and Logic Unit,

play00:27

but most people call it by its street name: the ALU.

play00:29

The ALU is the mathematical brain of a computer.

play00:32

When you understand an ALU’s design and function, you’ll understand a fundamental

play00:36

part of modern computers. It is THE thing that does all of the computation in a computer,

play00:41

so basically everything uses it.

play00:43

First though, look at this beauty.

play00:45

This is perhaps the most famous ALU ever, the Intel 74181.

play00:50

When it was released in 1970, it was

play00:52

It was the first complete ALU that fit entirely inside of a single chip -

play00:57

Which was a huge engineering feat at the time.

play00:59

So today we’re going to take those Boolean logic gates we learned about last week

play01:03

to build a simple ALU circuit with much of the same functionality as the 74181.

play01:08

And over the next few episodes we’ll use

play01:10

this to construct a computer from scratch. So it’s going to get a little bit complicated,

play01:14

but I think you guys can handle it.

play01:16

INTRO

play01:25

An ALU is really two units in one -- there’s an arithmetic unit and a logic unit.

play01:30

Let's start with the arithmetic unit, which is responsible for handling all numerical operations in a

play01:34

computer, like addition and subtraction. It also does a bunch of other simple things like

play01:39

add one to a number, which is called an increment operation, but we’ll talk about those later.

play01:43

Today, we’re going to focus on the pièce de résistance, the crème de la crème of

play01:47

operations that underlies almost everything else a computer does - adding two numbers together.

play01:52

We could build this circuit entirely out of

play01:54

individual transistors, but that would get confusing really fast.

play01:57

So instead as we talked about in Episode 3 – we can use a high-level of abstraction and build our components

play02:03

out of logic gates, in this case: AND, OR, NOT and XOR gates.

play02:08

The simplest adding circuit that we can build takes two binary digits, and adds them together.

play02:12

So we have two inputs, A and B, and one output, which is the sum of those two digits.

play02:18

Just to clarify: A, B and the output are all single bits.

play02:21

There are only four possible input combinations.

play02:24

The first three are: 0+0 = 0

play02:27

1+0 = 1 0+1 = 1

play02:30

Remember that in binary, 1 is the same as true, and 0 is the same as false.

play02:35

So this set of inputs exactly matches the boolean logic of an XOR gate, and we can use it as

play02:39

our 1-bit adder.

play02:40

But the fourth input combination, 1 + 1, is a special case. 1 + 1 is 2 (obviously)

play02:46

but there’s no 2 digit in binary, so as we talked about last episode, the result is

play02:50

0 and the 1 is carried to the next column. So the sum is really 10 in binary.

play02:54

Now, the output of our XOR gate is partially correct - 1 plus 1, outputs 0.

play03:00

But, we need an extra output wire for that carry bit.

play03:03

The carry bit is only “true” when the inputs are 1 AND 1, because that's the only

play03:06

time when the result (two) is bigger than 1 bit can store… and conveniently we have

play03:10

a gate for that! An AND gate, which is only true when both inputs are true, so

play03:15

we’ll add that to our circuit too.

play03:17

And that's it. This circuit is called a half adder. It’s

play03:19

It's not that complicated - just two logic gates - but let’s abstract away even this level

play03:24

of detail and encapsulate our newly minted half adder as its own component, with two

play03:28

inputs - bits A and B - and two outputs, the sum and the carry bits.

play03:32

This takes us to another level of abstraction… heh… I feel like I say that a lot.

play03:36

I wonder if this is going to become a thing.

play03:43

Anyway, If you want to add more than 1 + 1

play03:46

we’re going to need a “Full Adder.” That half-adder left us with a carry bit as output.

play03:50

That means that when we move on to the next column in a multi-column addition,

play03:54

and every column after that, we are going to have to add three bits together, no two.

play03:59

A full adder is a bit more complicated - it

play04:00

takes three bits as inputs: A, B and C. So the maximum possible input is 1 + 1 + 1,

play04:07

which equals 1 carry out 1, so we still only need two output wires: sum and carry.

play04:12

We can build a full adder using half adders. To do this, we use a half adder to add A plus B

play04:17

just like before – but then feed that result and input C into a second half adder.

play04:22

Lastly, we need a OR gate to check if either one of the carry bits was true.

play04:27

That’s it, we just made a full adder! Again,we can go up a level of abstraction and wrap

play04:31

up this full adder as its own component. It takes three inputs, adds them, and outputs

play04:36

the sum and the carry, if there is one.

play04:38

Armed with our new components, we can now build a circuit that takes two, 8-bit numbers

play04:42

– Let’s call them A and B – and adds them together.

play04:44

Let’s start with the very first bit of

play04:46

A and B, which we’ll call A0 and B0. At this point, there is no carry bit to deal

play04:51

with, because this is our first addition. So we can use our half adder to add these

play04:55

two bits together. The output is sum0. Now we want to add A1 and B1 together.

play05:01

It's possible there was a carry from the previous addition of A0 and B0, so this time we need

play05:06

to use a full adder that also inputs the carry bit. We output this result as sum1.

play05:11

Then, we take any carry from this full adder, and run it into the next full adder that handles

play05:16

A2 and B2. And we just keep doing this in a big chain until all 8 bits have been added.

play05:21

Notice how the carry bits ripple forward to each subsequent adder. For this reason,

play05:26

this is called an 8-bit ripple carry adder. Notice how our last full adder has a carry out.

play05:32

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.

play05:36

This is called an overflow.

play05:37

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.

play05:43

This can usually cause errors and unexpected behavior.

play05:45

Famously, the original PacMan arcade game used 8 bits to keep track of what level you were on.

play05:50

This meant that if you made it past level 255 – the largest number storablein 8 bits –

play05:55

to level 256, the ALU overflowed.

play05:58

This caused a bunch of errors and glitches making the level unbeatable.

play06:01

The bug became a rite of passage for the greatest PacMan players.

play06:04

So if we want to avoid overflows, we can extend our circuit with more full adders, allowing

play06:09

us to add 16 or 32 bit numbers. This makes overflows less likely to happen, but at the

play06:14

expense of more gates. An additional downside is that it takes a little bit of time for

play06:19

each of the carries to ripple forward.

play06:21

Admittedly, not very much time, electrons move pretty fast, so we’re talking about billionths of a second,

play06:26

but that’s enough to make a difference in today’s fast computers.

play06:29

For this reason, modern computers use a slightly different adding circuit called a ‘carry-look-ahead’ adder

play06:35

which is faster, but ultimately does exactly the same thing-- adds binary numbers.

play06:39

The ALU’s arithmetic unit also has circuits for other math operations

play06:43

and in general these 8 operations are always supported.

play06:46

And like our adder, these other operations are built from individual logic gates.

play06:50

Interestingly, you may have noticed that there are no multiply and divide operations.

play06:54

That's because simple ALUs don’t have a circuit for this, and instead just perform a series of additions.

play06:59

Let’s say you want to multiply 12 by 5.

play07:01

That’s the same thing as adding 12 to itself 5 times. So it would take 5 passes through

play07:06

the ALU to do this one multiplication. And this is how many simple processors,

play07:10

like those in your thermostat, TV remote, and microwave, do multiplication.

play07:15

It’s slow, but it gets the job done.

play07:17

However, fancier processors, like those in your laptop or smartphone,

play07:20

have arithmetic units with dedicated circuits for multiplication.

play07:23

And as you might expect, the circuit is more complicated than addition -- there’s no

play07:27

magic, it just takes a lot more logic gates – which is why less expensive processors

play07:31

don’t have this feature.

play07:32

Ok, let’s move on to the other half of the ALU: the Logic Unit.

play07:36

Instead of arithmetic operations, the Logic Unit performs… well...

play07:39

logical operations, like AND, OR and NOT, which we’ve talked about previously.

play07:44

It also performs simple numerical tests, like checking if a number is negative.

play07:47

For example, here’s a circuit that tests if the output of the ALU is zero.

play07:51

It does this using a bunch of OR gates to see if any of the bits are 1.

play07:55

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

play08:01

input so the output is 1 only if the input number is 0.

play08:05

So that’s a high level overview of what makes up an ALU. We even built several of

play08:09

the main components from scratch, like our ripple adder.

play08:11

As you saw, it’s just a big bunch of logic gates connected in clever ways.

play08:14

Which brings us back to that ALU you admired so much at the beginning of the episode.

play08:18

The Intel 74181.

play08:21

Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs,

play08:26

which means YOU BUILT AN ALU THAT’S LIKE

play08:29

TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH YOUR MIND! Well.. sort of.

play08:34

We didn’t build the whole thing… but you get the idea.

play08:36

The 74181 used about 70 logic gates, and it couldn’t multiply or divide.

play08:41

But it was a huge step forward in miniaturization, opening the doors to more capable and less expensive computers.

play08:47

This 4-bit ALU circuit is already a lot to take in,

play08:50

but our 8-bit ALU would require hundreds of logic gates to fully build and engineers

play08:54

don’t want to see all that complexity when using an ALU, so they came up with a special

play08:59

symbol to wrap it all up, which looks like a big ‘V’. Just another level of abstraction!

play09:09

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,

play09:17

for example, addition or subtraction.

play09:19

For that, we use a 4-bit operation code.

play09:21

We’ll talk about this more in a later episode, but in brief, 1000 might be the command

play09:27

to add, while 1100 is the command for subtract. Basically, the operation code tells the ALU

play09:33

what operation to perform. And the result of that operation on inputs A and B is an 8-bit output.

play09:38

ALUs also output a series of Flags, which are 1-bit outputs for particular states and statuses.

play09:43

For example, if we subtract two numbers, and the result is 0, our zero-testing circuit, the one we made earlier,

play09:50

sets the Zero Flag to True (1). This is useful if we are trying to determine if two numbers are are equal.

play09:55

If we wanted to test if A was less than B,

play09:57

we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true.

play10:03

If it was, we know that A was smaller than B.

play10:05

And finally, there’s also a wire attached to the carry out on the adder we built,

play10:09

so if there is an overflow, we’ll know about it. This is called the Overflow Flag.

play10:13

Fancier ALUs will have more flags, but these three flags are universal and frequently used.

play10:18

In fact, we’ll be using them soon in a future episode.

play10:21

So now you know how your computer does all its basic mathematical operations digitally

play10:25

with no gears or levers required.

play10:27

We’re going to use this ALU when we construct our CPU two episodes from now.

play10:31

But before that, our computer is going to need some memory! We'll talk about that next week.

Rate This

5.0 / 5 (0 votes)

Related Tags
计算机科学ALU二进制逻辑门全加器溢出错误数字逻辑计算原理Intel 74181PacManCPU构建
Do you need a summary in English?