├── (字幕)全40集中英字幕文本.txt ├── (字幕)全40集中英字幕文本 ├── 01. 计算机早期历史-Early Computing.ass.txt ├── 02. 电子计算机-Electronic Computing.ass.txt ├── 03. 布尔逻辑 和 逻辑门-Boolean Logic & Logic Gates.ass.txt ├── 04. 二进制-Representing Numbers and Letters with Binary.ass.txt ├── 05. 算术逻辑单元-How Computers Calculate-the ALU.ass.txt ├── 06. 寄存器 & 内存-Registers and RAM.ass.txt ├── 07. 中央处理器-The Central Processing Unit(CPU).ass.txt ├── 08. 指令和程序-Instructions & Programs.ass.txt ├── 09. 高级CPU设计-Advanced CPU Designs.ass.txt ├── 10. 早期的编程方式-Early Programming.ass.txt ├── 11. 编程语言发展史-The First Programming Languages.ass.txt ├── 12. 编程原理-语句和函数-Programming Basics - Statements & Functions.ass.txt ├── 13. 算法入门 - Intro to Algorithms.ass.txt ├── 14. 数据结构-Data Structures.ass.txt ├── 15. 阿兰·图灵-Alan Turing.ass.txt ├── 16. 软件工程-Software Engineering.ass.txt ├── 17. 集成电路&摩尔定律-Integrated Circuits & Moore’s Law.ass.txt ├── 18. 操作系统-Operating Systems.ass.txt ├── 19. 内存&储存介质-Memory & Storage.mp4.ass.txt ├── 20. 文件系统-Files & File Systems.ass.txt ├── 21. 压缩-Compression.ass.txt ├── 22. 命令行界面-Keyboards & Command Line Interfaces.ass.txt ├── 23. 屏幕&2D 图形显示-Screens&2D Graphics.ass.txt ├── 24. 冷战和消费主义-The Cold War and Consumerism.ass.txt ├── 25. 个人计算机革命-The Personal Computer Revolution.ass.txt ├── 26. 图形用户界面-Graphical User Interfaces.ass.txt ├── 27. 3D 图形-3D Graphics.ass.txt ├── 28. 计算机网络-Computer Networks.ass.txt ├── 29. 互联网-The Internet.ass.txt ├── 30. 万维网-The World Wide Web.ass.txt ├── 31. 计算机安全-Cybersecurity.ass.txt ├── 32. 黑客&攻击-Hackers & Cyber Attacks.ass.txt ├── 33. 加密-Cryptography.ass.txt ├── 34. 机器学习&人工智能-Machine Learning & Artificial Intelligence.ass.txt ├── 35. 计算机视觉-Computer Vision.ass.txt ├── 36. 自然语言处理-Natural Language Processing.ass.txt ├── 37. 机器人-Robots.ass.txt ├── 38. 计算机心理学 - Psychology of Computing.ass.txt ├── 39. 教育科技-Educational Technology.ass.txt └── 40. 奇点,天网,计算机的未来-The Singularity, Skynet, and the Future of Computing.ass.txt ├── .gitignore ├── 1. screenshot.py ├── README-about-subtitle.md ├── README.md ├── extract_from_ass_subtitle ├── 2. extract_head.js ├── 3. extract_ass_to_txt.js ├── README.md ├── common.js └── package.json └── image ├── cs40.jpg ├── english.png └── qr.png /(字幕)全40集中英字幕文本/02. 电子计算机-Electronic Computing.ass.txt: -------------------------------------------------------------------------------- 1 | Our last episode brought us to the start of the 20th century, 2 | 上集讲到 20 世纪初 3 | 4 | where early, special purpose computing devices, like tabulating machines, 5 | 当时的早期计算设备都针对特定用途 比如 制表机 6 | 7 | were a huge boon to governments and business 8 | 大大推进了政府和企业 9 | 10 | - aiding, and sometimes replacing, rote manual tasks. 11 | 它们帮助, 甚至代替了人工 12 | 13 | But the scale of human systems continued to increase at an unprecedented rate. 14 | 然而人类社会的规模 在以前所未有的速度增长 15 | 16 | The first half of the 20th century saw the world's population almost double. 17 | 20世纪上半叶,世界人口几乎翻倍 18 | 19 | World War 1 mobilized 70 million people, and World War 2 involved more than 100 million. 20 | 一战动员7千万人,二战1亿多人 21 | 22 | Global trade and transit networks became interconnected like never before, 23 | 全球贸易和运输更加紧密 24 | 25 | and the sophistication of our engineering and scientific endeavors reached new heights 26 | 工程和科学的复杂度也达到新高 27 | 28 | - we even started to seriously consider visiting other planets. 29 | - 我们甚至开始考虑造访其他行星 30 | 31 | And it was this explosion of complexity, bureaucracy, and ultimately data, 32 | 复杂度的增高导致数据量暴增 33 | 34 | that drove an increasing need for automation and computation. 35 | 人们需要更多自动化 更强的计算能力 36 | 37 | Soon those cabinet-sized electro-mechanical computers grew into room-sized behemoths 38 | 很快,柜子大小的计算机变成房间大小 39 | 40 | that were expensive to maintain and prone to errors. 41 | 维护费用高 而且容易出错 42 | 43 | And it was these machines that would set the stage for future innovation. 44 | 而正是这些机器 为未来的创新打下基础 45 | 46 | One of the largest electro-mechanical computers built was the Harvard Mark I, 47 | 最大的机电计算机之一是 哈佛马克一号 48 | 49 | completed in 1944 by IBM for the Allies during World War 2. 50 | IBM 在 1944 完成建造,给二战同盟国建造的. 51 | 52 | It contained 765,000 components, three million connections, and five hundred miles of wire. 53 | 它有76万5千个组件,300万个连接点和500英里长的导线 54 | 55 | To keep its internal mechanics synchronized, 56 | 为了保持内部机械装置同步 57 | 58 | it used a 50-foot shaft running right through the machine driven by a five horsepower motor. 59 | 它有一个50英尺的传动轴,由一个 5 马力的电机驱动 60 | 61 | One of the earliest uses for this technology was running simulations for the Manhattan Project. 62 | 这台机器最早的用途之一 是给"曼哈顿计划"跑模拟 63 | 64 | The brains of these huge electro-mechanical beasts were relays: 65 | 这台机器的大脑是"继电器" 66 | 67 | electrically-controlled mechanical switches. 68 | 继电器是:用电控制的机械开关 69 | 70 | In a relay, there is a control wire that determines whether a circuit is opened or closed. 71 | 继电器里,有根"控制线路",控制电路是开还是关 72 | 73 | The control wire connects to a coil of wire inside the relay. 74 | "控制线路" 连着一个线圈 75 | 76 | When current flows through the coil, an electromagnetic field is created, 77 | 当电流流过线圈,线圈产生电磁场 78 | 79 | which in turn, attracts a metal arm inside the relay, snapping it shut and completing the circuit. 80 | 吸引金属臂,从而闭合电路 81 | 82 | You can think of a relay like a water faucet. 83 | 你可以把继电器 想成水龙头 84 | 85 | The control wire is like the faucet handle. 86 | 把控制线路 想成水龙头把 87 | 88 | Open the faucet, and water flows through the pipe. 89 | 打开水龙头,水会流出来 90 | 91 | Close the faucet, and the flow of water stops. 92 | 关闭水龙头,水就没有了 93 | 94 | Relays are doing the same thing, just with electrons instead of water. 95 | 继电器是一样的,只不过控制的是电子 而不是水 96 | 97 | The controlled circuit can then connect to other circuits, or to something like a motor, 98 | 这个控制电路可以连到其他电路,比如马达 99 | 100 | which might increment a count on a gear, 101 | 马达让计数齿轮 +1 102 | 103 | like in Hollerith's tabulating machine we talked about last episode. 104 | 就像上集中 Hollerith 的制表机一样 105 | 106 | Unfortunately, the mechanical arm inside of a relay *has mass*, 107 | 不幸的是,继电器内的机械臂 *有质量* 108 | 109 | and therefore can't move instantly between opened and closed states. 110 | 因此无法快速开关 111 | 112 | A good relay in the 1940's might be able to flick back and forth fifty times in a second. 113 | 1940 年代一个好的继电器 1 秒能翻转 50 次 114 | 115 | That might seem pretty fast, but it's not fast enough to be useful at solving large, complex problems. 116 | 看起来好像很快,但还不够快,不足以解决复杂的大问题 117 | 118 | The Harvard Mark I could do 3 additions or subtractions per second; 119 | 哈佛马克一号,1 秒能做 3 次加法或减法运算 120 | 121 | multiplications took 6 seconds, and divisions took 15. 122 | 一次乘法要花 6 秒,除法要花 15 秒 123 | 124 | And more complex operations, like a trigonometric function, could take over a minute. 125 | 更复杂的操作 比如三角函数,可能要一分钟以上 126 | 127 | In addition to slow switching speed, another limitation was wear and tear. 128 | 除了速度慢,另一个限制是齿轮磨损 129 | 130 | Anything mechanical that moves will wear over time. 131 | 任何会动的机械都会随时间磨损 132 | 133 | Some things break entirely, and other things start getting sticky, slow, and just plain unreliable. 134 | 有些部件会完全损坏,有些则是变黏,变慢,变得不可靠 135 | 136 | And as the number of relays increases, the probability of a failure increases too. 137 | 并且随着继电器数量增加,故障概率也会增加 138 | 139 | The Harvard Mark I had roughly 3500 relays. 140 | 哈佛马克一号 有大约 3500 个继电器 141 | 142 | Even if you assume a relay has an operational life of 10 years, 143 | 哪怕假设继电器的使用寿命是 10 年 144 | 145 | this would mean you'd have to replace, on average, one faulty relay every day! 146 | 也意味着平均每天得换一个故障继电器! 147 | 148 | That's a big problem when you are in the middle of running some important, multi-day calculation. 149 | 这个问题很严重,因为有些重要运算要运行好几天 150 | 151 | And that's not all engineers had to contend with. 152 | 而且还有更多其他问题要考虑 153 | 154 | These huge, dark, and warm machines also attracted insects. 155 | 这些巨大,黑色,温暖的机器也会吸引昆虫 156 | 157 | In September 1947, operators on the Harvard Mark II pulled a dead moth from a malfunctioning relay. 158 | 1947年9月,哈佛马克2型的操作员从故障继电器中,拔出一只死虫 159 | 160 | Grace Hopper who we'll talk more about in a later episode noted, 161 | Grace Hopper(这位我们以后还会提到)曾说 162 | 163 | "From then on, when anything went wrong with a computer, 164 | "从那时起,每当电脑出了问题, 165 | 166 | we said it had bugs in it." 167 | 我们就说它出了 bug(虫子)" 168 | 169 | And that's where we get the term computer bug. 170 | 这就是术语 "bug" 的来源 171 | 172 | It was clear that a faster, more reliable alternative to electro-mechanical relays was needed 173 | 显然,如果想进一步提高计算能力 174 | 175 | if computing was going to advance further, 176 | 我们需要更快更可靠的东西,来替代继电器 177 | 178 | and fortunately that alternative already existed! 179 | 幸运的是,替代品已经存在了! 180 | 181 | In 1904, English physicist John Ambrose Fleming 182 | 在 1904 年,英国物理学家 "约翰·安布罗斯·弗莱明" 183 | 184 | developed a new electrical component called a thermionic valve, 185 | 开发了一种新的电子组件,叫"热电子管" 186 | 187 | which housed two electrodes inside an airtight glass bulb 188 | 把两个电极装在一个气密的玻璃灯泡里 189 | 190 | - this was the first vacuum tube. 191 | -这是世上第一个真空管 192 | 193 | One of the electrodes could be heated, which would cause it to emit electrons 194 | 其中一个电极可以加热,从而发射电子 195 | 196 | – a process called thermionic emission. 197 | -这叫 "热电子发射" 198 | 199 | The other electrode could then attract these electrons to create the flow of our electric faucet, 200 | 另一个电极会吸引电子,形成"电龙头"的电流 201 | 202 | but only if it was positively charged 203 | 但只有带正电才行 204 | 205 | - if it had a negative or neutral charge, the electrons would no longer be attracted across the vacuum 206 | - 如果带负电荷或中性电荷,电子就没办法被吸引,越过真空区域 207 | 208 | so no current would flow. 209 | 因此没有电流 210 | 211 | An electronic component that permits the one-way flow of current is called a diode, 212 | 电流只能单向流动的电子部件叫 "二极管" 213 | 214 | but what was really needed was a switch to help turn this flow on and off. 215 | 但我们需要的是,一个能开关电流的东西 216 | 217 | Luckily, shortly after, in 1906, American inventor Lee de Forest 218 | 幸运的是,不久之后在 1906 年,美国发明家 "李·德富雷斯特" 219 | 220 | added a third "control" electrode that sits between the two electrodes in Fleming's design. 221 | 他在"弗莱明"设计的两个电极之间,加入了第三个 "控制" 电极 222 | 223 | By applying a positive charge to the control electrode, it would permit the flow of electrons as before. 224 | 向"控制"电极施加正电荷,它会允许电子流动 225 | 226 | But if the control electrode was given a negative charge, 227 | 但如果施加负电荷 228 | 229 | it would prevent the flow of electrons. 230 | 它会阻止电子流动 231 | 232 | So by manipulating the control wire, one could open or close the circuit. 233 | 因此通过控制线路,可以断开或闭合电路 234 | 235 | It's pretty much the same thing as a relay 236 | 和继电器的功能一样 237 | 238 | - but importantly, vacuum tubes have no moving parts. 239 | - 但重要的是,真空管内没有会动的组件 240 | 241 | This meant there was less wear, 242 | 这意味着更少的磨损 243 | 244 | and more importantly, they could switch thousands of times per second. 245 | 更重要的是,每秒可以开闭数千次 246 | 247 | These triode vacuum tubes would become the basis of radio, long distance telephone, 248 | 因此这些"三极真空管"成为了无线电,长途电话 249 | 250 | and many other electronic devices for nearly a half century. 251 | 以及其他电子设备的基础,持续了接近半个世纪 252 | 253 | I should note here that vacuum tubes weren't perfect 254 | 我应该提到,真空管不是完美的 255 | 256 | - they're kind of fragile, and can burn out like light bulbs, 257 | -它们有点脆弱,并且像灯泡一样会烧坏 258 | 259 | they were a big improvement over mechanical relays. 260 | 但比起机械继电器是一次巨大进步 261 | 262 | Also, initially vacuum tubes were expensive 263 | 起初,真空管非常昂贵 264 | 265 | – a radio set often used just one, 266 | 收音机一般只用一个 267 | 268 | but a computer might require hundreds or thousands of electrical switches. 269 | 但计算机可能要上百甚至上千个电气开关 270 | 271 | But by the 1940s, 272 | 但到了 1940 年代 273 | 274 | their cost and reliability had improved to the point where they became feasible for use in computers…. 275 | 它的成本和可靠性得到改进,可以用在计算机里 276 | 277 | at least by people with deep pockets, like governments. 278 | 至少有钱人负担得起,比如政府 279 | 280 | This marked the shift from electro-mechanical computing to electronic computing. 281 | 这标志着计算机 从机电转向电子 282 | 283 | Let's go to the Thought Bubble. 284 | 我们来进入思想泡泡 285 | 286 | The first large-scale use of vacuum tubes for computing was the Colossus MK 1, 287 | 第一个大规模使用真空管的计算机是 "巨人1号" 288 | 289 | designed by engineer Tommy Flowers and completed in December of 1943. 290 | 由工程师 Tommy Flowers 设计,完工于1943年12月 291 | 292 | The Colossus was installed at Bletchley Park, in the UK, 293 | 巨人1号 在英国的"布莱切利园", 用于破解纳粹通信 294 | 295 | and helped to decrypt Nazi communications. 296 | 巨人1号 在英国的"布莱切利园", 用于破解纳粹通信 297 | 298 | This may sound familiar because two years prior Alan Turing, 299 | 听起来可能有点熟,因为 2 年前 阿兰·图灵 300 | 301 | often called the father of computer science, 302 | 他经常被称为"计算机科学之父" 303 | 304 | had created an electromechanical device, also at Bletchley Park, called the Bombe. 305 | 图灵也在"布莱切利园"做了台机电装置,叫 "Bombe" 306 | 307 | It was an electromechanical machine designed to break Nazi Enigma codes, 308 | 这台机器的设计目的是 破解纳粹"英格码"通讯加密设备 309 | 310 | but the Bombe wasn't technically a computer, 311 | 但 Bombe 严格来说不算计算机 312 | 313 | and we'll get to Alan Turing's contributions later. 314 | 我们之后会讨论"阿兰·图灵"的贡献 315 | 316 | Anyway, the first version of Colossus contained 1,600 vacuum tubes, 317 | 总之,巨人1号有 1600 个真空管 318 | 319 | and in total, ten Colossi were built to help with code-breaking. 320 | 总共造了 10 台巨人计算机,来帮助破解密码 321 | 322 | Colossus is regarded as the first programmable, electronic computer. 323 | 巨人 被认为是第一个可编程的电子计算机 324 | 325 | Programming was done by plugging hundreds of wires into plugboards, 326 | 编程的方法是把几百根电线插入插板 327 | 328 | sort of like old school telephone switchboards, 329 | 有点像老电话交换机 330 | 331 | in order to set up the computer to perform the right operations. 332 | 这是为了让计算机执行正确操作 333 | 334 | So while "programmable", it still had to be configured to perform a specific computation. 335 | 虽然"可编程" ,但还是要配置它 336 | 337 | Enter the The Electronic Numerical Integrator and Calculator - or ENIAC - 338 | 电子数值积分计算机 "ENIAC" 339 | 340 | completed a few years later in 1946 at the University of Pennsylvania. 341 | 几年后在 1946 年,在"宾夕法尼亚大学"完成建造 342 | 343 | Designed by John Mauchly and J. Presper Eckert, 344 | 设计者是 John Mauchly 和 J. Presper Eckert 345 | 346 | this was the world's first truly general purpose, programmable, electronic computer. 347 | 这是世上第一个真正的通用,可编程,电子计算机 348 | 349 | ENIAC could perform 5000 ten-digit additions or subtractions per second, 350 | ENIAC 每秒可执行 5000 次十位数加减法 351 | 352 | many, many times faster than any machine that came before it. 353 | 比前辈快了很多倍 354 | 355 | It was operational for ten years, 356 | 它运作了十年 357 | 358 | and is estimated to have done more arithmetic than the entire human race up to that point. 359 | 据估计,它完成的运算,比全人类加起来还多 360 | 361 | But with that many vacuum tubes failures were common, 362 | 因为真空管很多,所以故障很常见 363 | 364 | and ENIAC was generally only operational for about half a day at a time before breaking down. 365 | ENIAC 运行半天左右就会出一次故障 366 | 367 | Thanks Thought Bubble. 368 | 谢了 思想泡泡 369 | 370 | By the 1950's, even vacuum-tube-based computing was reaching its limits. 371 | 到 1950 年代,真空管计算机都达到了极限 372 | 373 | The US Air Force's AN/FSQ-7 computer, which was completed in 1955, 374 | 美国空军的 AN/FSQ-7 计算机于 1955 年完成 375 | 376 | was part of the "SAGE" air defense computer system, 377 | 是 "SAGE" 防空计算机系统的一部分 378 | 379 | which we'll talk more about in a later episode. 380 | 之后的视频还会提到. 381 | 382 | To reduce cost and size, as well as improve reliability and speed, 383 | 为了降低成本和大小,同时提高可靠性和速度 384 | 385 | a radical new electronic switch would be needed. 386 | 我们需要一种新的电子开关 387 | 388 | In 1947, Bell Laboratory scientists John Bardeen, Walter Brattain, and William Shockley 389 | 1947 年,贝尔实验室科学家 \N John Bardeen,Walter Brattain,William Shockley 390 | 391 | invented the transistor, 392 | 发明了晶体管 393 | 394 | and with it, a whole new era of computing was born! 395 | 一个全新的计算机时代诞生了! 396 | 397 | The physics behind transistors is pretty complex, relying on quantum mechanics, 398 | 晶体管的物理学相当复杂,牵扯到量子力学 399 | 400 | so we're going to stick to the basics. 401 | 所以我们只讲基础 402 | 403 | A transistor is just like a relay or vacuum tube 404 | 晶体管 就像之前提过的"继电器"或"真空管" 405 | 406 | - it's a switch that can be opened or closed by applying electrical power via a control wire. 407 | -它是一个开关,可以用控制线路来控制开或关 408 | 409 | Typically, transistors have two electrodes separated by a material that sometimes can conduct electricity, 410 | 晶体管有两个电极,\N 电极之间有一种材料隔开它们,这种材料有时候导电 411 | 412 | and other times resist it 413 | 有时候不导电 414 | 415 | - a semiconductor. 416 | - 这叫"半导体" 417 | 418 | In this case, the control wire attaches to a "gate" electrode. 419 | 控制线连到一个 "门" 电极 420 | 421 | By changing the electrical charge of the gate, 422 | 通过改变 "门" 的电荷 423 | 424 | the conductivity of the semiconducting material can be manipulated, 425 | 我们可以控制半导体材料的导电性 426 | 427 | allowing current to flow or be stopped 428 | 来允许或不允许 电流流动 429 | 430 | - like the water faucet analogy we discussed earlier. 431 | - 就像之前的水龙头比喻 432 | 433 | Even the very first transistor at Bell Labs showed tremendous promise 434 | 贝尔实验室的第一个晶体管就展示了巨大的潜力 435 | 436 | - it could switch between on and off states 10,000 times per second. 437 | 每秒可以开关 10,000 次 438 | 439 | Further, unlike vacuum tubes made of glass and with carefully suspended, fragile components, 440 | 而且,比起玻璃制成,小心易碎的真空管 441 | 442 | transistors were solid material known as a solid state component. 443 | 晶体管是固态的 444 | 445 | Almost immediately, transistors could be made smaller than the smallest possible relays or vacuum tubes. 446 | 晶体管可以远远小于继电器或真空管 447 | 448 | This led to dramatically smaller and cheaper computers, like the IBM 608, released in 1957 449 | 导致更小更便宜的计算机,比如1957年发布的IBM 608 450 | 451 | – the first fully transistor-powered, commercially-available computer. 452 | - 第一个完全用晶体管,而且消费者也可以买到的计算机 453 | 454 | It contained 3000 transistors and could perform 4,500 additions, 455 | 它有 3000 个晶体管,每秒执行 4500 次加法 456 | 457 | or roughly 80 multiplications or divisions, every second. 458 | 每秒能执行 80 次左右的乘除法 459 | 460 | IBM soon transitioned all of its computing products to transistors, 461 | IBM 很快把所有产品都转向了晶体管 462 | 463 | bringing transistor-based computers into offices, and eventually, homes. 464 | 把晶体管计算机带入办公室,最终引入家庭 465 | 466 | Today, computers use transistors that are smaller than 50 nanometers in size 467 | 如今,计算机里的晶体管小于 50 纳米 468 | 469 | - for reference, a sheet of paper is roughly 100,000 nanometers thick. 470 | - 而一张纸的厚度大概是 10 万纳米 471 | 472 | And they're not only incredibly small, they're super fast 473 | 晶体管不仅小,还超级快 474 | 475 | - they can switch states millions of times per second, and can run for decades. 476 | - 每秒可以切换上百万次,并且能工作几十年 477 | 478 | A lot of this transistor and semiconductor development happened 479 | 很多晶体管和半导体的开发在"圣克拉拉谷" 480 | 481 | in the Santa Clara Valley, between San Francisco and San Jose, California. 482 | 这个地方在加州,位于"旧金山"和"圣荷西"之间 483 | 484 | As the most common material used to create semiconductors is silicon, 485 | 而生产半导体最常见的材料是 "硅" 486 | 487 | this region soon became known as Silicon Valley. 488 | 所以这个地区被称为 "硅谷" 489 | 490 | Even William Shockley moved there, founding Shockley Semiconductor, 491 | 甚至 William Shockley 都搬了过去,创立了"肖克利半导体" 492 | 493 | whose employees later founded Fairchild Semiconductors, 494 | 里面的员工后来成立了"仙童半导体" 495 | 496 | whose employees later founded Intel - the world's largest computer chip maker today. 497 | 这里面的员工后来创立了英特尔 - 当今世界上最大的计算机芯片制造商 498 | 499 | Ok, so we've gone from relays to vacuum tubes to transistors. 500 | 好了,我们从"继电器"到"真空管"到"晶体管" 501 | 502 | We can turn electricity on and off really, really, really fast. 503 | 我们可以让电路开闭得非常非常快 504 | 505 | But how do we get from transistors to actually computing something, 506 | 但我们是如何用晶体管做计算的? 507 | 508 | especially if we don't have motors and gears? 509 | 我们没有马达和齿轮啊? 510 | 511 | That's what we're going to cover over the next few episodes. 512 | 我们接下来几集会讲 513 | 514 | Thanks for watching. See you next week. 515 | 感谢观看 下周见 516 | 517 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/03. 布尔逻辑 和 逻辑门-Boolean Logic & Logic Gates.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne and welcome to Crash Course Computer Science! 2 | 嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | Today we start our journey up the ladder of abstraction, 5 | 今天我们开始"抽象"的旅程 6 | 7 | where we leave behind the simplicity of being able to see every switch and gear, 8 | 不用管底层细节,把精力用来构建更复杂的系统 9 | 10 | but gain the ability to assemble increasingly complex systems. 11 | 不用管底层细节,把精力用来构建更复杂的系统 12 | 13 | Last episode, we talked about how computers evolved from electromechanical devices, 14 | 上集,我们谈了计算机最早是机电设备 15 | 16 | that often had decimal representations of numbers 17 | 一般用十进制计数 18 | 19 | - like those represented by teeth on a gear 20 | - 比如用齿轮数来代表十进制 21 | 22 | - to electronic computers with transistors that can turn the flow of electricity on or off. 23 | - 再到晶体管计算机 24 | 25 | And fortunately, even with just two states of electricity, 26 | 幸运的是,只用 开/关 两种状态也可以代表信息 27 | 28 | we can represent important information. 29 | 幸运的是,只用 开/关 两种状态也可以代表信息 30 | 31 | We call this representation Binary 32 | 这叫 二进制 33 | 34 | -- which literally means "of two states", 35 | - 意思是"用两种状态表示" 36 | 37 | in the same way a bicycle has two wheels or a biped has two legs. 38 | 就像自行车有两个轮,双足动物有两条腿 39 | 40 | You might think two states isn't a lot to work with, and you'd be right! 41 | 你可能觉得两种状态不多,你是对的! 42 | 43 | But, it's exactly what you need for representing the values "true" and "false". 44 | 但如果只需要表示 true 和 false,两个值就够了 45 | 46 | In computers, an "on" state, when electricity is flowing, represents true. 47 | 电路闭合,电流流过,代表 "真" 48 | 49 | The off state, no electricity flowing, represents false. 50 | 电路断开,无电流流过,代表"假" 51 | 52 | We can also write binary as 1's and 0's instead of true's and false's 53 | 二进制也可以写成 1 和 0 而不是 true 和 false 54 | 55 | - they are just different expressions of the same signal 56 | - 只是不同的表达方式罢了 57 | 58 | - but we'll talk more about that in the next episode. 59 | - 我们下集会讲更多细节 60 | 61 | Now it is actually possible to use transistors for more than just turning electrical current on and off, 62 | 晶体管的确可以不只是 开/关,还可以让不同大小的电流通过 63 | 64 | and to allow for different levels of current. 65 | 晶体管的确可以不只是 开/关,还可以让不同大小的电流通过 66 | 67 | Some early electronic computers were ternary, that's three states, 68 | 一些早期电子计算机是三进制的,有 3 种状态 69 | 70 | and even quinary, using 5 states. 71 | 甚至五进制,5 种状态 72 | 73 | The problem is, the more intermediate states there are, 74 | 问题是,状态越多,越难区分信号 75 | 76 | the harder it is to keep them all seperate 77 | 问题是,状态越多,越难区分信号 78 | 79 | -- if your smartphone battery starts running low or there's electrical noise 80 | - 如果手机快没电了或者附近有电噪音 81 | 82 | because someone's running a microwave nearby, 83 | 因为有人在用微波炉, 84 | 85 | the signals can get mixed up... 86 | 信号可能会混在一起... 87 | 88 | and this problem only gets worse with transistors changing states millions of times per second! 89 | 而每秒百万次变化的晶体管会让这个问题变得更糟! 90 | 91 | So, placing two signals as far apart as possible 92 | 所以我们把两种信号尽可能分开 93 | 94 | - using just 'on and off' - gives us the most distinct signal to minimize these issues. 95 | - 只用"开"和"关"两种状态,可以尽可能减少这类问题 96 | 97 | Another reason computers use binary 98 | 计算机用二进制的另一个原因是 99 | 100 | is that an entire branch of mathematics already existed that dealt exclusively with true and false values. 101 | 有一整个数学分支存在,专门处理"真"和"假" 102 | 103 | And it had figured out all of the necessary rules and operations for manipulating them. 104 | 它已经解决了所有法则和运算 105 | 106 | It's called Boolean Algebra! 107 | 叫"布尔代数"! 108 | 109 | George Boole, from which Boolean Algebra later got its name, 110 | 乔治·布尔(George Boole)是布尔二字的由来 111 | 112 | was a self-taught English mathematician in the 1800s. 113 | 是一位 19 世纪自学成才的英国数学家 114 | 115 | He was interested in representing logical statements that went "under, over, and beyond" 116 | 他有兴趣用数学式子 扩展亚里士多德基于哲学的逻辑方法 117 | 118 | Aristotle's approach to logic, which was, unsurprisingly, grounded in philosophy. 119 | 他有兴趣用数学式子 扩展亚里士多德基于哲学的逻辑方法 120 | 121 | Boole's approach allowed truth to be systematically and formally proven, through logic equations 122 | 布尔用 逻辑方程 系统而正式的证明真理(truth) 123 | 124 | which he introduced in his first book, "The Mathematical Analysis of Logic" in 1847. 125 | 他在 1847 年的第一本书"逻辑的数学分析"中介绍过 126 | 127 | In "regular" algebra -- the type you probably learned in high school -- the values of variables 128 | 在"常规"代数里 - 你在高中学的那种 - 变量的值 129 | 130 | are numbers, and operations on those numbers are things like addition and multiplication. 131 | 是数字,可以进行加法或乘法之类的操作 132 | 133 | But in Boolean Algebra, the values of variables are true and false, and the operations are logical. 134 | 但在布尔代数中,变量的值是 true 和 false,\N 能进行逻辑操作 135 | 136 | There are three fundamental operations in Boolean Algebra: a NOT, an AND, and an OR operation. 137 | 布尔代数中有三个基本操作:NOT, AND 和 OR 138 | 139 | And these operations turn out to be really useful so we're going to look at them individually. 140 | 这些操作非常有用,我们一个个来看 141 | 142 | A NOT takes a single boolean value, either true or false, and negates it. 143 | NOT 操作把布尔值反转,\N把 true 进行 NOT 就会变成 false,反之亦然 144 | 145 | It flips true to false, and false to true. 146 | NOT 操作把布尔值反转,\N把 true 进行 NOT 就会变成 false,反之亦然 147 | 148 | We can write out a little logic table that shows the original value under Input, 149 | 我们可以根据 NOT 操作的输入和输出,做出这个表 150 | 151 | and the outcome after applying the operation under Output. 152 | 我们可以根据 NOT 操作的输入和输出,做出这个表 153 | 154 | Now here's the cool part -- we can easily build boolean logic out of transistors. 155 | 酷的地方是 - 用晶体管可以轻松实现这个逻辑 156 | 157 | As we discussed last episode, transistors are really just little electrically controlled switches. 158 | 上集说过,晶体管只是电控制的开关 159 | 160 | They have three wires: two electrodes and one control wire. 161 | 有 3 根线:2 根电极和 1 根控制线 162 | 163 | When you apply electricity to the control wire, 164 | 控制线通电时 165 | 166 | it lets current flow through from one electrode, through the transistor, to the other electrode. 167 | 电流就可以从一个电极流到另一个电极 168 | 169 | This is a lot like a spigot on a pipe 170 | 就像水龙头一样 171 | 172 | -- open the tap, water flows, 173 | - 打开水龙头,就有水流出来 174 | 175 | close the tap, water shuts off. 176 | 关掉水龙头,就没水了 177 | 178 | You can think of the control wire as an input, 179 | 可以把控制线,当做输入 ( input ) \N 底部的电极,当做输出(output) 180 | 181 | and the wire coming from the bottom electrode as the output. 182 | 可以把控制线,当做输入 ( input ) \N 底部的电极,当做输出(output) 183 | 184 | So with a single transistor, we have one input and one output. 185 | 所以 1 个晶体管,有一个输入和一个输出 186 | 187 | If we turn the input on, the output is also on because the current can flow through it. 188 | 如果我们打开输入(input on) \N 输出也会打开(output on) \N 因为电流可以流过 189 | 190 | If we turn the input off, the output is also off and the current can no longer pass through. 191 | 如果关闭输入(input off) \N 输出也会关闭(output off)\N 因为电流无法通过 192 | 193 | Or in boolean terms, when the input is true, the output is true. 194 | 或者用布尔术语来说\N 输入为 真,输出为 真 195 | 196 | And when the input is false, the output is also false. 197 | 输入为 假 \N 输出为 假 198 | 199 | Which again we can show on a logic table. 200 | 我们也可以把这个做成"真值表" 201 | 202 | This isn't a very exciting circuit though because its not doing anything 203 | 这个电路没什么意思,因为它没做什么事 204 | 205 | -- the input and output are the same. 206 | - 输入和输出是一样的 207 | 208 | But, we can modify this circuit just a little bit to create a NOT. 209 | 但我们可以稍加修改,实现 NOT 210 | 211 | Instead of having the output wire at the end of the transistor, we can move it before. 212 | 与其把下面那根线当做 输出,我们可以把 输出 放到上面 213 | 214 | If we turn the input on, the transistor allows current to pass through it to the "ground", 215 | 如果打开 输入,电流可以流过然后 "接地" 216 | 217 | and the output wire won't receive that current 218 | 输出就没有电流,所以输出是 off 219 | 220 | - so it will be off. 221 | 输出就没有电流,所以输出是 off 222 | 223 | In our water metaphor grounding would be like 224 | 如果用水来举例 225 | 226 | if all the water in your house was flowing out of a huge hose 227 | 就像家里的水都从一个大管子流走了 228 | 229 | so there wasn't any water pressure left for your shower. 230 | 打开淋浴头一点水也没有 231 | 232 | So in this case if the input is on, output is off. 233 | 如果输入是 on,输出是 off 234 | 235 | When we turn off the transistor, though, current is prevented from flowing down it to the ground, 236 | 当输入是 off,电流没法接地,就流过了输出,所以输出是 on 237 | 238 | so instead, current flows through the output wire. 239 | 当输入是 off,电流没法接地,就流过了输出,所以输出是 on 240 | 241 | So the input will be off and the output will be on. 242 | 如果输入是 off,输出是 on 243 | 244 | And this matches our logic table for NOT, so congrats, we just built a circuit that computes NOT! 245 | 和 NOT 操作表一样!太棒了!我们做了个有点用的电路! 246 | 247 | We call them NOT gates - we call them gates because they're controlling the path of our current. 248 | 我们叫它 "NOT 门" \N 之所以叫 "门",是因为它能控制电流的路径 249 | 250 | The AND Boolean operation takes two inputs, but still has a single output. 251 | "AND"操作有 2 个输入,1 个输出 252 | 253 | In this case the output is only true if both inputs are true. 254 | 如果 2 个输入都是 true,输出才是 true 255 | 256 | Think about it like telling the truth. 257 | 你可以想成是 说真话 258 | 259 | You're only being completely honest if you don't lie even a little. 260 | 如果完全不说谎,才是诚实 261 | 262 | For example, let's take the statement, 263 | 举例,看如下这个句子 264 | 265 | "My name is Carrie Anne AND I'm wearing a blue dress". 266 | 我叫 Carrie Anne "而且"我穿着蓝色的衣服 267 | 268 | Both of those facts are true, so the whole statement is true. 269 | 2 个都是真的,所以整个是真的 270 | 271 | But if I said, "My name is Carrie Anne AND I'm wearing pants" that would be false, 272 | 但如果说,我叫 Carrie Anne"而且"我穿了裤子, 就是假的 273 | 274 | because I'm not wearing pants. 275 | 因为我没穿裤子 276 | 277 | Or trousers. 278 | 或长裤,如果你是英国人你会用这个词……(英/美单词不同梗) 279 | 280 | If you're in England. 281 | 或长裤,如果你是英国人你会用这个词……(英/美单词不同梗) 282 | 283 | The Carrie Anne part is true, but a true AND a false, is still false. 284 | 虽然前半句是真的,但是 真 "AND" 假,还是假 285 | 286 | If I were to reverse that statement it would still obviously be false, 287 | 就算把前后顺序反过来,也依然是 假 288 | 289 | and if I were to tell you two complete lies that is also false, 290 | 如果我说 2 个假的事情,那么结果是假。 291 | 292 | and again we can write all of these combinations out in a table. 293 | 和上次一样,可以给"AND"做个表 294 | 295 | To build an AND gate, we need two transistors connected together 296 | 为了实现 "AND 门",我们需要 2 个晶体管连在一起 297 | 298 | so we have our two inputs and one output. 299 | 这样有 2 个输入和 1 个输出 300 | 301 | If we turn on just transistor A, current won't flow because the current is stopped by transistor B. 302 | 如果只打开 A,不打开 B \N 电流无法流到 output,所以输出是 false 303 | 304 | Alternatively, if transistor B is on, but the transistor A is off, 305 | 如果只打开 B,不打开 A ,也一样,电流无法流到 output 306 | 307 | the same thing, the current can't get through. 308 | 如果只打开 B,不打开 A ,也一样,电流无法流到 output 309 | 310 | Only if transistor A AND transistor B are on does the output wire have current. 311 | 只有 A 和 B 都打开了,output 才有电流 312 | 313 | The last boolean operation is OR 314 | 最后一个是 OR (前面讲了 NOT 和 AND) 315 | 316 | -- where only one input has to be true for the output to be true. 317 | 只要 2 个输入里,其中 1 个是 true,输出就是 true 318 | 319 | For example, my name is Margaret Hamilton OR I'm wearing a blue dress. 320 | 比如,我叫 Margaret Hamilton"或"我穿着蓝色衣服 321 | 322 | This is a true statement because although I'm not Margaret Hamilton unfortunately, 323 | 结果是 true,虽然我不是 Margaret Hamilton 324 | 325 | I am wearing a blue dress, so the overall statement is true. 326 | 但是我穿着蓝色衣服,所以结果是 true 327 | 328 | An OR statement is also true if both facts are true. 329 | 对于"OR 操作"来说,\N如果 2 个 输入都是 true,输出也是 true 330 | 331 | The only time an OR statement is false is if both inputs are false. 332 | 只有 2 个输入都是 false,OR 的结果才是 false 333 | 334 | Building an OR gate from transistors needs a few extra wires. 335 | 实现 "OR 门" 除了晶体管还要额外的线 336 | 337 | Instead of having two transistors in series -- one after the other -- 338 | 不是串联起来。而是并联 339 | 340 | we have them in parallel. 341 | 不是串联起来。而是并联 342 | 343 | We run wires from the current source to both transistors. 344 | 然后左边这条线有电流输入 345 | 346 | We use this little arc to note that the wires jump over one another and aren't connected, 347 | 我们用"小拱门"代表 2 条线没连在一起,只是跨过而已 348 | 349 | even though they look like they cross. 350 | 虽然看起来像连在一起 351 | 352 | If both transistors are turned off, the current is prevented from flowing to the output, 353 | 如果 A 和 B 都是 off,电流无法流过 354 | 355 | so the output is also off. 356 | 所以输出是 off 357 | 358 | Now, if we turn on just Transistor A, current can flow to the output. 359 | 如果打开 A,电流可以流过。输出是 on 360 | 361 | Same thing if transistor A is off, but Transistor B in on. 362 | 如果只打开 B 也一样 363 | 364 | Basically if A OR B is on, the output is also on. 365 | 只要 A OR B 是 on, 输出就是 on 366 | 367 | Also, if both transistors are on, the output is still on. 368 | 如果 A 和 B 都 on,结果是 on 369 | 370 | Ok, now that we've got NOT, AND, and OR gates, 371 | 好,现在 NOT 门, AND 门, OR 门 都搞定了 372 | 373 | and we can leave behind the constituent transistors and move up a layer of abstraction. 374 | 我们可以进行一次抽象 375 | 376 | The standard engineers use for these gates are a triangle with a dot for a NOT, 377 | NOT 门的画法是三角形前面一个圆点 378 | 379 | a D for the AND, and a spaceship for the OR. 380 | AND 门用 D 表示 ,OR 门用太空船表示 381 | 382 | Those aren't the official names, but that's howI like to think of them. 383 | "D 形状和太空船"不是标准叫法, 只是我喜欢这样叫而已 384 | 385 | Representing them and thinking about them this way allows us to build even bigger components 386 | 我们可以用这种方法表示它们,构建更大的组件 387 | 388 | while keeping the overall complexity relatively the same 389 | 就不会变得很复杂 390 | 391 | - just remember that that mess of transistors and wires is still there. 392 | - 晶体管和电线依然在那里,我们只是用符号来代表而已 393 | 394 | For example, another useful boolean operation in computation is called an Exclusive OR 395 | 除了前面说的三个\N 另一个有用的布尔操作叫 "异或" 396 | 397 | - or XOR for short. 398 | - 简称 XOR 399 | 400 | XOR is like a regular OR, but with one difference: 401 | XOR 就像普通 OR,但有一个区别: 402 | 403 | if both inputs are true, the XOR is false. 404 | 如果 2 个输入都是 true,XOR 输出 false 405 | 406 | The only time an XOR is true is when one input is true and the other input is false. 407 | 想要 XOR 输出 true \N 一个输入必须是 true,另一个必须是 false 408 | 409 | It's like when you go out to dinner and your meal comes with a side salad OR a soup 410 | 就像你出去吃晚饭,你点的饭要么配沙拉,要么配汤 411 | 412 | - sadly, you can't have both! 413 | - 你不能两个都要! 414 | 415 | And building this from transistors is pretty confusing, 416 | 用晶体管实现 XOR 门有点烧脑子 417 | 418 | but we can show how an XOR is created from our three basic boolean gates. 419 | 但我可以展示一下\N 怎么用前面提到的 3 种门来做 XOR 门 420 | 421 | We know we have two inputs again -- A and B -- and one output. 422 | 我们有 2 个输入,A 和 B ,还有 1 个输出. 423 | 424 | Let's start with an OR gate, since the logic table looks almost identical to an OR. 425 | 我们先放一个 OR 门. 因为 OR 和 XOR 的逻辑表很像 426 | 427 | There's only one problem - when A and B are true, the logic is different from OR, 428 | 只有 1 个问题 - 当 A 和 B 都是 true 时 \N OR 的输出和想要的 XOR 输出不一样 429 | 430 | and we need to output "false". 431 | 我们想要 false 432 | 433 | And XOR turns out to be a very useful component, 434 | XOR 超有用的 435 | 436 | and we'll get to it in another episode, 437 | 我们下次再说它 438 | 439 | so useful in fact engineers gave it its own symbol too -- an OR gate with a smile :) 440 | 因为超有用,\N 工程师给了它一个符号,一个 OR 门 + 一个笑脸 441 | 442 | But most importantly, we can now put XOR into our metaphorical toolbox 443 | 重要的是,现在可以把 XOR 放入"工具箱"了 444 | 445 | and not have to worry about the individual logic gates that make it up, 446 | 不用担心 XOR 具体用了几个门 447 | 448 | or the transistors that make up those gates, 449 | 这几个门又是怎么用晶体管拼的 450 | 451 | or how electrons are flowing through a semiconductor. 452 | 或电子是怎么流过半导体的 453 | 454 | Moving up another layer of abstraction. 455 | 再次向上抽象 456 | 457 | When computer engineers are designing processors, they rarely work at the transistor level, 458 | 工程师设计处理器时,很少在晶体管的层面上思考, 459 | 460 | and instead work with much larger blocks, like logic gates, and even larger components 461 | 而是用更大的组件,比如逻辑门,或者由逻辑门组成的更大组件, 462 | 463 | made up of logic gates, which we'll discuss in future episodes. 464 | 我们以后会讲 465 | 466 | And even if you are a professional computer programmer, 467 | 就算是专业程序员 468 | 469 | it's not often that you think about 470 | 也不用考虑逻辑是怎样在物理层面实现的 471 | 472 | how the logic that you are programming is actually implemented 473 | 也不用考虑逻辑是怎样在物理层面实现的 474 | 475 | in the physical world by these teeny tiny components. 476 | 也不用考虑逻辑是怎样在物理层面实现的 477 | 478 | We've also moved from thinking about raw electrical signals to our first representation of data 479 | 我们从电信号开始,到现在第一次表示数据 480 | 481 | - true and false - and we've even gotten a little taste of computation. 482 | - 真和假 - 开始有点"计算"的感觉了 483 | 484 | With just the logic gates in this episode, 485 | 仅用这集讲的 逻辑门 486 | 487 | we could build a machine that evaluates complex logic statements, 488 | 我们可以判断复杂的语句 比如: 489 | 490 | like if "Name is John Green AND after 5pm OR is Weekend AND near Pizza Hut", 491 | [如果是 John Green] AND [下午 5 点后] \N OR [周末] AND [在比萨店附近] 492 | 493 | then "John will want pizza" equals true. 494 | 那么 "John 想要比萨" = 真 495 | 496 | And with that, I'm starving, I'll see you next week. 497 | 我都说饿了,下周见 498 | 499 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/04. 二进制-Representing Numbers and Letters with Binary.ass.txt: -------------------------------------------------------------------------------- 1 | Hi I'm Carrie Anne, this is Crash Course Computer Science 2 | 嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | and today we're going to talk about how computers store and represent numerical data. 5 | 今天,我们讲计算机如何存储和表示数字 6 | 7 | Which means we've got to talk about Math! 8 | 所以会有一些数学 9 | 10 | But don't worry. 11 | 不过别担心 12 | 13 | Every single one of you already knows exactly what you need to know to follow along. 14 | 你们的数学水平绝对够用了 15 | 16 | So, last episode we talked about how transistors can be used to build logic gates, 17 | 上集我们讲了,怎么用晶体管做逻辑门 18 | 19 | which can evaluate boolean statements. 20 | 逻辑门可以判断布尔语句 21 | 22 | And in boolean algebra, there are only two, binary values: true and false. 23 | 布尔代数只有两个值:True 和 False 24 | 25 | But if we only have two values, 26 | 但如果只有两个值,我们怎么表达更多东西? 27 | 28 | how in the world do we represent information beyond just these two values? 29 | 但如果只有两个值,我们怎么表达更多东西? 30 | 31 | That's where the Math comes in. 32 | 这就需要数学了 33 | 34 | So, as we mentioned last episode, a single binary value can be used to represent a number. 35 | 上集提到,1 个二进制值可以代表 1 个数 36 | 37 | Instead of true and false, we can call these two states 1 and 0 which is actually incredibly useful. 38 | 我们可以把真和假 ,当做 1 和 0 39 | 40 | And if we want to represent larger things we just need to add more binary digits. 41 | 如果想表示更多东西,加位数就行了 42 | 43 | This works exactly the same way as the decimal numbers that we're all familiar with. 44 | 和我们熟悉的十进制一样 45 | 46 | With decimal numbers there are "only" 10 possible values a single digit can be; 0 through 9, 47 | 十进制只有 10 个数(0到9) 48 | 49 | and to get numbers larger than 9 we just start adding more digits to the front. 50 | 要表示大于 9 的数,加位数就行了 51 | 52 | We can do the same with binary. 53 | 二进制也可以这样玩 54 | 55 | For example, let's take the number two hundred and sixty three. 56 | 拿 263 举例 57 | 58 | What does this number actually represent? 59 | 这个数字 "实际" 代表什么? 60 | 61 | Well, it means we've got 2 one-hundreds, 6 tens, and 3 ones. 62 | 2 个 100 \N6 个 10 \N 3 个 1 63 | 64 | If you add those all together, we've got 263. 65 | 加在一起,就是 263 66 | 67 | Notice how each column has a different multiplier. 68 | 注意每列有不同的乘数 69 | 70 | In this case, it's 100, 10, and 1. 71 | 100, 10, 1 72 | 73 | Each multiplier is ten times larger than the one to the right. 74 | 每个乘数都比右边大十倍 75 | 76 | That's because each column has ten possible digits to work with, 0 through 9, 77 | 因为每列有 10 个可能的数字(0到9) 78 | 79 | after which you have to carry one to the next column. 80 | 如果超过 9,要在下一列进 1. 81 | 82 | For this reason, it's called base-ten notation, also called decimal since deci means ten. 83 | 因此叫 "基于十的表示法" 或十进制 84 | 85 | AND Binary works exactly the same way, it's just base-two. 86 | 二进制也一样,只不过是基于 2 而已 87 | 88 | That's because there are only two possible digits in binary - 1 and 0. 89 | 因为二进制只有两个可能的数, 1 和 0 90 | 91 | This means that each multiplier has to be two times larger than the column to its right. 92 | 意味着每个乘数必须是右侧乘数的两倍 93 | 94 | Instead of hundreds, tens, and ones, we now have fours, twos and ones. 95 | 就不是之前的 100, 10, 1 \N 而是 4, 2, 1 96 | 97 | Take for example the binary number: 101. 98 | 拿二进制数 101 举例 99 | 100 | This means we have 1 four, 0 twos, and 1 one. 101 | 意味着有\N 1个 "4" \N 0个 "2" \N 1个 "1" 102 | 103 | Add those all together and we've got the number 5 in base ten. 104 | 加在一起,得到十进制的 5 105 | 106 | But to represent larger numbers, binary needs a lot more digits. 107 | 为了表示更大的数字,二进制需要更多位数 108 | 109 | Take this number in binary 10110111. 110 | 拿二进制数 10110111 举例 111 | 112 | We can convert it to decimal in the same way. 113 | 我们可以用相同的方法转成十进制 114 | 115 | We have 1 x 128, 0 x 64, 1 x 32, 1 x 16, 0 x 8, 1 x 4, 1 x 2, and 1 x 1. 116 | 1 x 128 ,0 x 64 ,1 x 32 ,1 x 16 \N 0 x 8 ,1 x 4 ,1 x 2 ,1 x 1 117 | 118 | Which all adds up to 183. 119 | 加起来等于 183 120 | 121 | Math with binary numbers isn't hard either. 122 | 二进制数的计算也不难 123 | 124 | Take for example decimal addition of 183 plus 19. 125 | 以十进制数 183 加 19 举例 126 | 127 | First we add 3 + 9, that's 12, so we put 2 as the sum and carry 1 to the ten's column. 128 | 首先 3 + 9,得到 12,然后位数记作 2,向前进 1 129 | 130 | Now we add 8 plus 1 plus the 1 we carried, thats 10, so the sum is 0 carry 1. 131 | 现在算 8+1+1=10,所以位数记作0,再向前进 1 132 | 133 | Finally we add 1 plus the 1 we carried, which equals 2. 134 | 最后 1+1=2,位数记作2 135 | 136 | So the total sum is 202. 137 | 所以和是202 138 | 139 | Here's the same sum but in binary. 140 | 二进制也一样 141 | 142 | Just as before, we start with the ones column. 143 | 和之前一样,从个位开始 144 | 145 | Adding 1+1 results in 2, even in binary. 146 | 1+1=2,在二进制中也是如此 147 | 148 | But, there is no symbol "2" so we use 10 and put 0 as our sum and carry the 1. 149 | 但二进制中没有 2,所以位数记作 0 ,进 1 150 | 151 | Just like in our decimal example. 152 | 就像十进制的例子一样 153 | 154 | 1 plus 1, plus the 1 carried, 155 | 1+1,再加上进位的1 156 | 157 | equals 3 or 11 in binary, 158 | 等于 3,用二进制表示是 11 159 | 160 | so we put the sum as 1 and we carry 1 again, and so on. 161 | 所以位数记作 1,再进 1,以此类推 162 | 163 | We end up with this number, which is the same as the number 202 in base ten. 164 | 最后得到这个数字,跟十进制 202 是一样的 165 | 166 | Each of these binary digits, 1 or 0, is called a "bit". 167 | 二进制中,一个 1 或 0 叫一"位" 168 | 169 | So in these last few examples, we were using 8-bit numbers with their lowest value of zero 170 | 上个例子我们用了 8 位 , 8 位能表示的最小数是 0, 8位都是0 171 | 172 | and highest value is 255, which requires all 8 bits to be set to 1. 173 | 最大数是 255,8 位都是 1 174 | 175 | Thats 256 different values, or 2 to the 8th power. 176 | 能表示 256 个不同的值,2 的 8 次方 177 | 178 | You might have heard of 8-bit computers, or 8-bit graphics or audio. 179 | 你可能听过 8 位机,8 位图像,8 位音乐 180 | 181 | These were computers that did most of their operations in chunks of 8 bits. 182 | 意思是计算机里\N 大部分操作都是 8 位 8 位这样处理的 183 | 184 | But 256 different values isn't a lot to work with, so it meant things like 8-bit games 185 | 但 256 个值不算多,意味着 8位游戏只能用 256 种颜色 186 | 187 | were limited to 256 different colors for their graphics. 188 | 但 256 个值不算多,意味着 8位游戏只能用 256 种颜色 189 | 190 | And 8-bits is such a common size in computing, it has a special word: a byte. 191 | 8 位是如此常见,以至于有专门的名字:字节 192 | 193 | A byte is 8 bits. 194 | 1 字节 = 8 位 \N 1 bytes = 8 bits 195 | 196 | If you've got 10 bytes, it means you've really got 80 bits. 197 | 如果有 10 个字节,意味着有 80 位 198 | 199 | You've heard of kilobytes, megabytes, gigabytes and so on. 200 | 你听过 千字节(KB)兆字节(MB)千兆字节(GB)等等 201 | 202 | These prefixes denote different scales of data. 203 | 不同前缀代表不同数量级 204 | 205 | Just like one kilogram is a thousand grams, 206 | 就像 1 千克 = 1000 克,1 千字节 = 1000 字节 207 | 208 | 1 kilobyte is a thousand bytes. 209 | 就像 1 千克 = 1000 克,1 千字节 = 1000 字节 210 | 211 | or really 8000 bits. 212 | 或 8000 位 213 | 214 | Mega is a million bytes (MB), and giga is a billion bytes (GB). 215 | Mega 是百万字节(MB), Giga 是十亿字节(GB) 216 | 217 | Today you might even have a hard drive that has 1 terabyte (TB) of storage. 218 | 如今你可能有 1 TB 的硬盘 219 | 220 | That's 8 trillion ones and zeros. 221 | 8 万亿个1和0 222 | 223 | But hold on! 224 | 等等,我们有另一种计算方法 225 | 226 | That's not always true. 227 | 等等,我们有另一种计算方法 228 | 229 | In binary, a kilobyte has two to the power of 10 bytes, or 1024. 230 | 二进制里,1 千字节 = 2的10次方 = 1024 字节 231 | 232 | 1000 is also right when talking about kilobytes, 233 | 1000 也是千字节(KB)的正确单位 234 | 235 | but we should acknowledge it isn't the only correct definition. 236 | 1000 和 1024 都对 237 | 238 | You've probably also heard the term 32-bit or 64-bit computers 239 | 你可能听过 32 位 或 64 位计算机 240 | 241 | you're almost certainly using one right now. 242 | 你现在用的电脑几乎肯定是其中一种 243 | 244 | What this means is that they operate in chunks of 32 or 64 bits. 245 | 意思是一块块处理数据,每块是 32 位或 64 位 246 | 247 | That's a lot of bits! 248 | 这可是很多位 249 | 250 | The largest number you can represent with 32 bits is just under 4.3 billion. 251 | 32 位能表示的最大数,是 43 亿左右 252 | 253 | Which is thirty-two 1's in binary. 254 | 也就是 32 个 1 255 | 256 | This is why our Instagram photos are so smooth and pretty 257 | 所以 Instagram 照片很清晰 258 | 259 | - they are composed of millions of colors, 260 | - 它们有上百万种颜色 261 | 262 | because computers today use 32-bit color graphics 263 | 因为如今都用 32 位颜色 264 | 265 | Of course, not everything is a positive number 266 | 当然,不是所有数都是正数 267 | 268 | - like my bank account in college. 269 | 比如我上大学时的银行账户 T_T 270 | 271 | So we need a way to represent positive and negative numbers. 272 | 我们需要有方法表示正数和负数 273 | 274 | Most computers use the first bit for the sign: 275 | 大部分计算机用第一位表示正负: 276 | 277 | 1 for negative, 0 for positive numbers, 278 | 1 是负,0 是正 279 | 280 | and then use the remaining 31 bits for the number itself. 281 | 用剩下 31 位来表示符号外的数值 282 | 283 | That gives us a range of roughly plus or minus two billion. 284 | 能表示的数的范围大约是正 20 亿到负 20 亿 285 | 286 | While this is a pretty big range of numbers, it's not enough for many tasks. 287 | 虽然是很大的数,但许多情况下还不够用 288 | 289 | There are 7 billion people on the earth, and the US national debt is almost 20 trillion dollars after all. 290 | 全球有 70 亿人口,美国国债近 20 万亿美元 291 | 292 | This is why 64-bit numbers are useful. 293 | 所以 64 位数很有用 294 | 295 | The largest value a 64-bit number can represent is around 9.2 quintillion! 296 | 64 位能表达最大数大约是 9.2×10 ^ 18 297 | 298 | That's a lot of possible numbers and will hopefully stay above the US national debt for a while! 299 | 希望美国国债在一段时间内不会超过这个数! 300 | 301 | Most importantly, as we'll discuss in a later episode, 302 | 重要的是(我们之后的视频会深入讲) 303 | 304 | computers must label locations in their memory, 305 | 计算机必须给内存中每一个位置,做一个 "标记" 306 | 307 | known as addresses, in order to store and retrieve values. 308 | 这个标记叫 "地址", 目的是为了方便存取数据 309 | 310 | As computer memory has grown to gigabytes and terabytes - that's trillions of bytes 311 | 如今硬盘已经增长到 GB 和 TB,上万亿个字节! 312 | 313 | it was necessary to have 64-bit memory addresses as well. 314 | 内存地址也应该有 64 位 315 | 316 | In addition to negative and positive numbers, 317 | 除了负数和正数,计算机也要处理非整数 318 | 319 | computers must deal with numbers that are not whole numbers, 320 | 除了负数和正数,计算机也要处理非整数 321 | 322 | like 12.7 and 3.14, or maybe even stardate: 43989.1. 323 | 比如 12.7 和 3.14,或"星历 43989.1" 324 | 325 | These are called "floating point" numbers, 326 | 这叫 浮点数 327 | 328 | because the decimal point can float around in the middle of number. 329 | 因为小数点可以在数字间浮动 330 | 331 | Several methods have been developed to represent floating point numbers. 332 | 有好几种方法 表示浮点数 333 | 334 | The most common of which is the IEEE 754 standard. 335 | 最常见的是 IEEE 754 标准 336 | 337 | And you thought historians were the only people bad at naming things! 338 | 你以为只有历史学家取名很烂吗? 339 | 340 | In essence, this standard stores decimal values sort of like scientific notation. 341 | 它用类似科学计数法的方法,来存十进制值 342 | 343 | For example, 625.9 can be written as 0.6259 x 10^3. 344 | 例如,625.9 可以写成 0.6259×10 ^ 3 345 | 346 | There are two important numbers here: the .6259 is called the significand. 347 | 这里有两个重要的数:.6259 叫 "有效位数" , 3 是指数 348 | 349 | And 3 is the exponent. 350 | 这里有两个重要的数:.6259 叫 "有效位数" , 3 是指数 351 | 352 | In a 32-bit floating point number, 353 | 在 32 位浮点数中 354 | 355 | the first bit is used for the sign of the number -- positive or negative. 356 | 第 1 位表示数的符号——正或负 357 | 358 | The next 8 bits are used to store the exponent 359 | 接下来 8 位存指数 360 | 361 | and the remaining 23 bits are used to store the significand. 362 | 剩下 23 位存有效位数 363 | 364 | Ok, we've talked a lot about numbers, but your name is probably composed of letters, 365 | 好了,聊够数了,但你的名字是字母组成的 366 | 367 | so it's really useful for computers to also have a way to represent text. 368 | 所以我们也要表示文字 369 | 370 | However, rather than have a special form of storage for letters, 371 | 与其用特殊方式来表示字母 \N 计算机可以用数表示字母 372 | 373 | computers simply use numbers to represent letters. 374 | 与其用特殊方式来表示字母 \N 计算机可以用数表示字母 375 | 376 | The most straightforward approach might be to simply number the letters of the alphabet: 377 | 最直接的方法是给字母编号: 378 | 379 | A being 1, B being 2, C 3, and so on. 380 | A是1,B是2,C是3,以此类推 381 | 382 | In fact, Francis Bacon, the famous English writer, 383 | 著名英国作家 弗朗西斯·培根(Francis Bacon) 384 | 385 | used five-bit sequences to encode all 26 letters of the English alphabet 386 | 曾用 5位序列 来编码英文的 26 个字母 387 | 388 | to send secret messages back in the 1600s. 389 | 在十六世纪传递机密信件 390 | 391 | And five bits can store 32 possible values - so that's enough for the 26 letters, 392 | 五位(bit)可以存 32 个可能值(2^5) - 这对26个字母够了 393 | 394 | but not enough for punctuation, digits, and upper and lower case letters. 395 | 但不能表示 标点符号,数字和大小写字母 396 | 397 | Enter ASCII, the American Standard Code for Information Interchange. 398 | ASCII,美国信息交换标准代码 399 | 400 | Invented in 1963, ASCII was a 7-bit code, enough to store 128 different values. 401 | 发明于 1963 年,ASCII 是 7 位代码,足够存 128 个不同值 402 | 403 | With this expanded range, it could encode capital letters, lowercase letters, 404 | 范围扩大之后,可以表示大写字母,小写字母, 405 | 406 | digits 0 through 9, and symbols like the @ sign and punctuation marks. 407 | 数字 0 到 9, @ 这样的符号, 以及标点符号 408 | 409 | For example, a lowercase 'a' is represented by the number 97, while a capital 'A' is 65. 410 | 举例,小写字母 a 用数字 97 表示,大写字母 A 是 65 411 | 412 | A colon is 58 and a closed parenthesis is 41. 413 | : 是58 \n ) 是41 414 | 415 | ASCII even had a selection of special command codes, 416 | ASCII 甚至有特殊命令符号 417 | 418 | such as a newline character to tell the computer where to wrap a line to the next row. 419 | 比如换行符,用来告诉计算机换行 420 | 421 | In older computer systems, 422 | 在老计算机系统中 423 | 424 | the line of text would literally continue off the edge of the screen if you didn't include a new line character! 425 | 如果没换行符,文字会超出屏幕 426 | 427 | Because ASCII was such an early standard, 428 | 因为 ASCII 是个很早的标准 429 | 430 | it became widely used, 431 | 所以它被广泛使用 432 | 433 | and critically, allowed different computers built by different companies to exchange data. 434 | 让不同公司制作的计算机,能互相交换数据 435 | 436 | This ability to universally exchange information is called "interoperability". 437 | 这种通用交换信息的能力叫 "互操作性" 438 | 439 | However, it did have a major limitation: it was really only designed for English. 440 | 但有个限制:它是为英语设计的 441 | 442 | Fortunately, there are 8 bits in a byte, not 7, 443 | 幸运的是,一个字节有8位,而不是7位 444 | 445 | and it soon became popular to use codes 128 through 255, 446 | 128 到 255 的字符渐渐变得常用 447 | 448 | previously unused, for "national" characters. 449 | 这些字符以前是空的,是给各个国家自己 "保留使用的" 450 | 451 | In the US, those extra numbers were largely used to encode additional symbols, 452 | 在美国,这些额外的数字主要用于编码附加符号 453 | 454 | like mathematical notation, graphical elements, and common accented characters. 455 | 比如数学符号,图形元素和常用的重音字符 456 | 457 | On the other hand, while the Latin characters were used universally, 458 | 另一方面,虽然拉丁字符被普遍使用 459 | 460 | Russian computers used the extra codes to encode Cyrillic characters, 461 | 在俄罗斯,他们用这些额外的字符表示西里尔字符 462 | 463 | and Greek computers, Greek letters, and so on. 464 | 而希腊电脑用希腊字母,等等 465 | 466 | And national character codes worked pretty well for most countries. 467 | 这些保留下来给每个国家自己安排的空位,\N 对大部分国家都够用 468 | 469 | The problem was, 470 | 问题是 471 | 472 | if you opened an email written in Latvian on a Turkish computer, 473 | 如果在 土耳其 电脑上打开 拉脱维亚语 写的电子邮件 474 | 475 | the result was completely incomprehensible. 476 | 会显示乱码 477 | 478 | And things totally broke with the rise of computing in Asia, 479 | 随着计算机在亚洲兴起,这种做法彻底失效了 480 | 481 | as languages like Chinese and Japanese have thousands of characters. 482 | 中文和日文这样的语言有数千个字符 483 | 484 | There was no way to encode all those characters in 8-bits! 485 | 根本没办法用 8 位来表示所有字符! 486 | 487 | In response, each country invented multi-byte encoding schemes, 488 | 为了解决这个问题,每个国家都发明了多字节编码方案 489 | 490 | all of which were mutually incompatible. 491 | 但相互不兼容 492 | 493 | The Japanese were so familiar with this encoding problem that they had a special name for it: 494 | 日本人总是碰到编码问题,以至于专门有词来称呼: 495 | 496 | "mojibake", which means "scrambled text". 497 | "mojibake" 意思是 乱码 498 | 499 | And so it was born - Unicode - one format to rule them all. 500 | 所以 Unicode 诞生了 - 统一所有编码的标准 501 | 502 | Devised in 1992 to finally do away with all of the different international schemes 503 | 设计于 1992 年,解决了不同国家不同标准的问题 504 | 505 | it replaced them with one universal encoding scheme. 506 | Unicode 用一个统一编码方案 507 | 508 | The most common version of Unicode uses 16 bits with space for over a million codes - 509 | 最常见的 Unicode 是 16 位的,有超过一百万个位置 - 510 | 511 | enough for every single character from every language ever used 512 | 对所有语言的每个字符都够了 513 | 514 | more than 120,000 of them in over 100 types of script 515 | 100 多种字母表加起来占了 12 万个位置。 516 | 517 | plus space for mathematical symbols and even graphical characters like Emoji. 518 | 还有位置放数学符号,甚至 Emoji 519 | 520 | And in the same way that ASCII defines a scheme for encoding letters as binary numbers, 521 | 就像 ASCII 用二进制来表示字母一样 522 | 523 | other file formats - like MP3s or GIFs - 524 | 其他格式 - 比如 MP3 或 GIF - 525 | 526 | use binary numbers to encode sounds or colors of a pixel in our photos, movies, and music. 527 | 用二进制编码声音/颜色,表示照片,电影,音乐 528 | 529 | Most importantly, under the hood it all comes down to long sequences of bits. 530 | 重要的是,这些标准归根到底是一长串位 531 | 532 | Text messages, this YouTube video, every webpage on the internet, 533 | 短信,这个 YouTube 视频,互联网上的每个网页 534 | 535 | and even your computer's operating system, are nothing but long sequences of 1s and 0s. 536 | 甚至操作系统,只不过是一长串 1 和 0 537 | 538 | So next week, 539 | 下周 540 | 541 | we'll start talking about how your computer starts manipulating those binary sequences, 542 | 我们会聊计算机怎么操作二进制 543 | 544 | for our first true taste of computation. 545 | 初尝"计算"的滋味 546 | 547 | Thanks for watching. See you next week. 548 | 感谢观看,下周见 549 | 550 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/05. 算术逻辑单元-How Computers Calculate-the ALU.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Ann and this is Crash Course Computer Science. 2 | 嗨,我是 Carrie Anne,欢迎收看计算机科学速成课 3 | 4 | So last episode, we talked about how numbers can be represented in binary. 5 | 上集,我们谈了如何用二进制表示数字 6 | 7 | Representing Like, 00101010 is 42 in decimal. 8 | 比如二进制 00101010 是十进制的 42 9 | 10 | Representing and storing numbers is an important function of a computer, 11 | 表示和存储数字是计算机的重要功能 12 | 13 | but the real goal is computation, or manipulating numbers in a structured and purposeful way, 14 | 但真正的目标是计算,有意义的处理数字 15 | 16 | like adding two numbers together. 17 | 比如把两个数字相加 18 | 19 | These operations are handled by a computer's Arithmetic and Logic Unit, 20 | 这些操作由计算机的 "算术逻辑单元 "处理 21 | 22 | but most people call it by its street name: 23 | 但大家会简称:ALU 24 | 25 | the ALU. 26 | 但大家会简称:ALU 27 | 28 | The ALU is the mathematical brain of a computer. 29 | ALU 是计算机的数学大脑 30 | 31 | When you understand an ALU's design and function, 32 | 等你理解了 ALU 的设计和功能之后 33 | 34 | you'll understand a fundamental part of modern computers. 35 | 你就理解了现代计算机的基石 36 | 37 | It is THE thing that does all of the computation in a computer, 38 | ALU *就是* 计算机里负责运算的组件\N 基本其他所有部件都用到了它 39 | 40 | so basically everything uses it. 41 | ALU *就是* 计算机里负责运算的组件\N 基本其他所有部件都用到了它 42 | 43 | First though, look at this beauty. 44 | 先来看看这个美人 45 | 46 | This is perhaps the most famous ALU ever, the Intel 74181. 47 | 这可能是最著名的 ALU,英特尔 74181 48 | 49 | When it was released in 1970, 50 | 1970 年发布时,它是第一个封装在单个芯片内的完整 ALU 51 | 52 | it was It was the first complete ALU that fit entirely inside of a single chip - 53 | 1970 年发布时,它是第一个封装在单个芯片内的完整 ALU 54 | 55 | Which was a huge engineering feat at the time. 56 | 这在当时是惊人的工程壮举 57 | 58 | So today we're going to take those Boolean logic gates we learned about last week 59 | 今天我们用上周学的布尔逻辑门 60 | 61 | to build a simple ALU circuit with much of the same functionality as the 74181. 62 | 做一个简单的 ALU 电路,功能和 74181 一样 63 | 64 | And over the next few episodes we'll use this to construct a computer from scratch. 65 | 然后接下来几集,用它从头做出一台电脑 66 | 67 | So it's going to get a little bit complicated, 68 | 所以会有点复杂 69 | 70 | but I think you guys can handle it. 71 | 但我觉得你们搞的定 72 | 73 | An ALU is really two units in one 74 | ALU 有 2 个单元,1 个算术单元和 1 个逻辑单元 75 | 76 | -- there's an arithmetic unit and a logic unit. 77 | ALU 有 2 个单元,1 个算术单元和 1 个逻辑单元 78 | 79 | Let's start with the arithmetic unit, 80 | 我们先讲"算术单元",它负责计算机里的所有数字操作 81 | 82 | which is responsible for handling all numerical operations in a computer, 83 | 我们先讲"算术单元",它负责计算机里的所有数字操作 84 | 85 | like addition and subtraction. 86 | 比如加减法 87 | 88 | It also does a bunch of other simple things like add one to a number, 89 | 它还做很多其他事情,比如给某个数字+1 90 | 91 | which is called an increment operation, but we'll talk about those later. 92 | 这叫增量运算,我们之后会说 93 | 94 | Today, we're going to focus on the piece of rsistance, the crme de la crme of operations 95 | 今天的重点是一切的根本 - "把两个数字相加" 96 | 97 | that underlies almost everything else a computer does - adding two numbers together. 98 | 今天的重点是一切的根本 - "把两个数字相加" 99 | 100 | We could build this circuit entirely out of individual transistors, 101 | 我们可以用单个晶体管一个个拼,把这个电路做出来,\N 但很快就会复杂的难以理解 102 | 103 | but that would get confusing really fast. 104 | 我们可以用单个晶体管一个个拼,把这个电路做出来,\N 但很快就会复杂的难以理解 105 | 106 | So instead as we talked about in Episode 3 107 | 所以与其用晶体管,我们会像第 3 集 108 | 109 | - we can use a high-level of abstraction and build our components out of logic gates, 110 | - 用更高层的抽象,用逻辑门来做 111 | 112 | in this case: AND, OR, NOT and XOR gates. 113 | 我们会用到 AND,OR,NOT 和 XOR 逻辑门 114 | 115 | The simplest adding circuit that we can build takes two binary digits, and adds them together. 116 | 最简单的加法电路, \N 是拿 2 个 bit 加在一起(bit 是 0 或 1) 117 | 118 | So we have two inputs, A and B, and one output, which is the sum of those two digits. 119 | 有 2 个输入:A 和 B, 1 个输出:就是两个数字的和 120 | 121 | Just to clarify: A, B and the output are all single bits. 122 | 需要注意的是:A, B, 输出,这3个都是单个 Bit ( 0 或 1 ) 123 | 124 | There are only four possible input combinations. 125 | 输入只有四种可能 126 | 127 | The first three are: 0+0 = 0 128 | 前三个是\N 0 + 0 = 0 \N 1 + 0 = 1 \N 0 + 1 = 1 129 | 130 | 1+0 = 1 0+1 = 1 131 | 前三个是\N 0 + 0 = 0 \N 1 + 0 = 1 \N 0 + 1 = 1 132 | 133 | Remember that in binary, 1 is the same as true, and 0 is the same as false. 134 | 记住二进制里,1 与 true 相同,0 与 false 相同 135 | 136 | So this set of inputs exactly matches the boolean logic of an XOR gate, 137 | 这组输入和输出,和 XOR 门的逻辑完全一样 138 | 139 | and we can use it as our 1-bit adder. 140 | 所以我们可以把 XOR 用作 1 位加法器(adder) 141 | 142 | But the fourth input combination, 1 + 1, is a special case. 1 + 1 is 2 (obviously) 143 | 但第四个输入组合,1+1,是个特例 \N 1+1=2(显然) 144 | 145 | but there's no 2 digit in binary, 146 | 但二进制里没有 2 147 | 148 | so as we talked about last episode, the result is 0 and the 1 is carried to the next column. 149 | 上集说过,二进制 1+1 的结果是0,1进到下一位 150 | 151 | So the sum is really 10 in binary. 152 | 和是 10 (二进制) 153 | 154 | Now, the output of our XOR gate is partially correct - 1 plus 1, outputs 0. 155 | XOR 门的输出,只对了一部分, 1+1 输出 0 156 | 157 | But, we need an extra output wire for that carry bit. 158 | 但我们需要一根额外的线代表 "进位" 159 | 160 | The carry bit is only "true" when the inputs are 1 AND 1, 161 | 只有输入是 1 和 1 时,进位才是 "true" 162 | 163 | because that's the only time when the result (two) is bigger than 1 bit can store 164 | 因为算出来的结果用 1 个 bit 存不下 165 | 166 | and conveniently we have a gate for that! 167 | 方便的是,我们刚好有个逻辑门能做这个事! 168 | 169 | It's not that complicated - just two logic gates - 170 | 没那么复杂 - 就两个逻辑门而已 171 | 172 | but let's abstract away even this level of detail 173 | 让我们抽象化 174 | 175 | and encapsulate our newly minted half adder as its own component, 176 | 把 "半加器" 封装成一个单独组件 177 | 178 | with two inputs - bits A and B - and two outputs, the sum and the carry bits. 179 | 两个输入 A 和 B 都是 1 位 \N 两个输出 "总和" 与 "进位" 180 | 181 | This takes us to another level of abstraction 182 | 这进入了另一层抽象 183 | 184 | heh I feel like I say that a lot. 185 | 我好像说了很多次,说不定会变成一个梗 186 | 187 | I wonder if this is going to become a thing. 188 | 我好像说了很多次,说不定会变成一个梗 189 | 190 | Anyway, If you want to add more than 1 + 1 191 | 如果想处理超过 1+1 的运算,我们需要"全加器" 192 | 193 | we're going to need a "Full Adder." 194 | 如果想处理超过 1+1 的运算,我们需要"全加器" 195 | 196 | That half-adder left us with a carry bit as output. 197 | 半加器 输出了进位 198 | 199 | That means that when we move on to the next column in a multi-column addition, 200 | 意味着,我们算下一列的时候 201 | 202 | and every column after that, we are going to have to add three bits together, no two. 203 | 还有之后的每一列,我们得加 3 个位在一起,并不是 2 个 204 | 205 | A full adder is a bit more complicated 206 | 全加器复杂了一点点 207 | 208 | 全加器表格 209 | 210 | - it takes three bits as inputs: A, B and C. 211 | 有 3 个输入:A, B, C (都是 1 个 bit) 212 | 213 | So the maximum possible input is 1 + 1 + 1, 214 | 所以最大的可能是 1 + 1 + 1 215 | 216 | which equals 1 carry out 1, so we still only need two output wires: sum and carry. 217 | "总和"1 "进位"1 \N 所以要两条输出线: "总和"和"进位" 218 | 219 | We can build a full adder using half adders. 220 | 我们可以用 半加器 做 全加器 221 | 222 | To do this, we use a half adder to add A plus B 223 | 我们先用半加器将 A 和 B 相加 224 | 225 | just like before - but then feed that result and input C into a second half adder. 226 | 然后把 C 输入到第二个半加器 227 | 228 | Lastly, we need a OR gate to check if either one of the carry bits was true. 229 | 最后用一个 OR 门检查进位是不是 true 230 | 231 | That's it, we just made a full adder! 232 | 这样就做出了一个全加器! 233 | 234 | Again,we can go up a level of abstraction and wrap up this full adder as its own component. 235 | 我们可以再提升一层抽象,把全加器作为独立组件 236 | 237 | It takes three inputs, adds them, and outputs the sum and the carry, if there is one. 238 | 全加器会把 A,B,C 三个输入加起来 \N 输出 "总和" 和 "进位" 239 | 240 | Armed with our new components, we can now build a circuit that takes two, 8-bit numbers 241 | 现在有了新组件,我们可以相加两个 8 位数字 242 | 243 | Let's call them A and B and adds them together. 244 | 叫两个数字叫 A 和 B 好了 245 | 246 | Let's start with the very first bit of A and B, 247 | 我们从 A 和 B 的第一位开始 248 | 249 | which we'll call A0 and B0. 250 | 叫 A0 和 B0 好了 251 | 252 | At this point, there is no carry bit to deal with, 253 | 现在不用处理任何进位,因为是第一次加法 254 | 255 | because this is our first addition. 256 | 现在不用处理任何进位,因为是第一次加法 257 | 258 | So we can use our half adder to add these two bits together. 259 | 所以我们可以用半加器,来加这2个数字 260 | 261 | The output is sum0. 262 | 输出叫 sum0 263 | 264 | Now we want to add A1 and B1 together. 265 | 现在加 A1 和 B1 266 | 267 | It's possible there was a carry from the previous addition of A0 and B0, 268 | 因为 A0 和 B0 的结果有可能进位 269 | 270 | so this time we need to use a full adder that also inputs the carry bit. 271 | 所以这次要用全加器,除了 A1 和 B1,还要连上进位 272 | 273 | We output this result as sum1. 274 | 输出叫 sum1 275 | 276 | Then, we take any carry from this full adder, 277 | 然后,把这个全加器的进位 \N 连到下个全加器的输入,处理 A2 和 B2 278 | 279 | and run it into the next full adder that handles A2 and B2. 280 | 然后,把这个全加器的进位 \N 连到下个全加器的输入,处理 A2 和 B2 281 | 282 | And we just keep doing this in a big chain until all 8 bits have been added. 283 | 以此类推,把 8 个 bit 都搞定 284 | 285 | Notice how the carry bits ripple forward to each subsequent adder. 286 | 注意每个进位是怎么连到下一个全加器的 287 | 288 | For this reason, this is called an 8-bit ripple carry adder. 289 | 所以叫 "8位行波进位加法器" 290 | 291 | Notice how our last full adder has a carry out. 292 | 注意最后一个全加器有 "进位" 的输出 293 | 294 | 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. 295 | 如果第 9 位有进位,代表着 2 个数字的和太大了,超过了 8 位 296 | 297 | This is called an overflow. 298 | 这叫 "溢出" (overflow) 299 | 300 | In general, an overflow occurs when the result of an addition is too large 301 | 一般来说 "溢出" 的意思是, 两个数字的和太大了 302 | 303 | to be represented by the number of bits you are using. 304 | 超过了用来表示的位数 305 | 306 | This can usually cause errors and unexpected behavior. 307 | 这会导致错误和不可预期的结果 308 | 309 | Famously, the original PacMan arcade game used 8 bits to keep track of what level you were on. 310 | 著名的例子是,吃豆人用 8 位存当前关卡数 311 | 312 | This meant that if you made it past level 255 - the largest number storablein 8 bits - to level 256, 313 | 如果你玩到了第 256 关( 8 位 bit 最大表示 255) 314 | 315 | the ALU overflowed. 316 | ALU 会溢出 317 | 318 | This caused a bunch of errors and glitches making the level unbeatable. 319 | 造成一连串错误和乱码,使得该关卡无法进行 320 | 321 | The bug became a rite of passage for the greatest PacMan players. 322 | 这个 bug 成了厉害吃豆人玩家的代表 323 | 324 | So if we want to avoid overflows, 325 | 如果想避免溢出 326 | 327 | we can extend our circuit with more full adders, allowing us to add 16 or 32 bit numbers. 328 | 我们可以加更多全加器,可以操作 16 或 32 位数字 329 | 330 | This makes overflows less likely to happen, but at the expense of more gates. 331 | 让溢出更难发生,但代价是更多逻辑门 332 | 333 | An additional downside is that it takes a little bit of time for each of the carries to ripple forward. 334 | 另外一个缺点是,每次进位都要一点时间 335 | 336 | Admittedly, not very much time, electrons move pretty fast, 337 | 当然时间不久,因为电子移动的很快 338 | 339 | so we're talking about billionths of a second, 340 | 但如今的量级是每秒几十亿次运算,所以会造成影响 341 | 342 | but that's enough to make a difference in today's fast computers. 343 | 但如今的量级是每秒几十亿次运算,所以会造成影响 344 | 345 | For this reason, modern computers use a slightly different adding circuit 346 | 所以,现代计算机用的加法电路有点不同 347 | 348 | called a 'carry-look-ahead' adder 349 | 叫 "超前进位加法器" 350 | 351 | which is faster, but ultimately does exactly the same thing 352 | 它更快,做的事情是一样的 - 把二进制数相加 353 | 354 | -- adds binary numbers. 355 | 它更快,做的事情是一样的 - 把二进制数相加 356 | 357 | The ALU's arithmetic unit also has circuits for other math operations 358 | ALU 的算术单元,也能做一些其他数学运算 359 | 360 | and in general these 8 operations are always supported. 361 | 一般支持这 8 个操作 362 | 363 | And like our adder, these other operations are built from individual logic gates. 364 | 就像加法器一样,这些操作也是由逻辑门构成的 365 | 366 | Interestingly, you may have noticed that there are no multiply and divide operations. 367 | 有趣的是,你可能注意到没有乘法和除法 368 | 369 | That's because simple ALUs don't have a circuit for this, 370 | 因为简单的 ALU 没有专门的电路来处理 371 | 372 | and instead just perform a series of additions. 373 | 而是把乘法用多次加法来实现 374 | 375 | Let's say you want to multiply 12 by 5. 376 | 假设想算 12x5 377 | 378 | That's the same thing as adding 12 to itself 5 times. 379 | 这和把 "12" 加 5 次是一样的 380 | 381 | So it would take 5 passes through the ALU to do this one multiplication. 382 | 所以要 5 次 ALU 操作来实现这个乘法 383 | 384 | And this is how many simple processors, 385 | 很多简单处理器都是这样做的 386 | 387 | like those in your thermostat, TV remote, and microwave, do multiplication. 388 | 比如恒温器,电视遥控器和微波炉 389 | 390 | It's slow, but it gets the job done. 391 | 慢是慢,但是搞的定 392 | 393 | However, fancier processors, like those in your laptop or smartphone, 394 | 然而笔记本和手机有更好的处理器 395 | 396 | have arithmetic units with dedicated circuits for multiplication. 397 | 有专门做乘法的算术单元 398 | 399 | And as you might expect, the circuit is more complicated than addition 400 | 你可能猜到了,乘法电路比加法复杂 401 | 402 | -- there's no magic, it just takes a lot more logic gates 403 | - 没什么魔法,只是更多逻辑门 404 | 405 | which is why less expensive processors don't have this feature. 406 | 所以便宜的处理器没有. 407 | 408 | Ok, let's move on to the other half of the ALU: 409 | 好了,我们现在讲 ALU 的另一半:逻辑单元 410 | 411 | the Logic Unit. 412 | 好了,我们现在讲 ALU 的另一半:逻辑单元 413 | 414 | Instead of arithmetic operations, the Logic Unit performs well... 415 | 逻辑单元执行逻辑操作 416 | 417 | logical operations, like AND, OR and NOT, which we've talked about previously. 418 | 比如之前讨论过的 AND,OR 和 NOT 操作 419 | 420 | It also performs simple numerical tests, 421 | 它也能做简单的数值测试 422 | 423 | like checking if a number is negative. 424 | 比如一个数字是不是负数 425 | 426 | For example, here's a circuit that tests if the output of the ALU is zero. 427 | 例如,这是检查 ALU 输出是否为 0 的电路 428 | 429 | It does this using a bunch of OR gates to see if any of the bits are 1. 430 | 它用一堆 OR 门检查其中一位是否为 1 431 | 432 | Even if one single bit is 1, 433 | 哪怕只有一个 Bit (位) 是1, 434 | 435 | we know the number can't be zero and then we use a final NOT gate to flip this input 436 | 我们就知道那个数字肯定不是 0,然后用一个 NOT 门取反 437 | 438 | so the output is 1 only if the input number is 0. 439 | 所以只有输入的数字是 0,输出才为 1 440 | 441 | So that's a high level overview of what makes up an ALU. 442 | 以上就是 ALU 的一个高层次概括 443 | 444 | We even built several of the main components from scratch, like our ripple adder. 445 | 我们甚至从零做了几个主要组件,比如行波进位加法器 446 | 447 | As you saw, it's just a big bunch of logic gates connected in clever ways. 448 | 它们只是一大堆逻辑门巧妙的连在一起而已. 449 | 450 | Which brings us back to that ALU you admired so much at the beginning of the episode. 451 | 让我们回到视频开始时的 ALU,英特尔 74181 452 | 453 | The Intel 74181. 454 | 让我们回到视频开始时的 ALU,英特尔 74181 455 | 456 | Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs, 457 | 和我们刚刚做的 8 位 ALU 不同,74181 只能处理 4 位输入 458 | 459 | which means 460 | 也就是说 461 | 462 | YOU BUILT AN ALU THAT'S LIKE TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH YOUR MIND! 463 | 你刚做了一个比英特尔 74181 还好的 ALU ! 464 | 465 | Well.. sort of. 466 | 其实 差不多啦.. 467 | 468 | We didn't build the whole thing 469 | 我们虽然没有全部造出来 470 | 471 | but you get the idea. 472 | 但你理解了整体概念 473 | 474 | The 74181 used about 70 logic gates, and it couldn't multiply or divide. 475 | 74181 用了大概 70 个逻辑门,但不能执行乘除. 476 | 477 | But it was a huge step forward in miniaturization, 478 | 但它向小型化迈出了一大步 479 | 480 | opening the doors to more capable and less expensive computers. 481 | 让计算机可以更强大更便宜 482 | 483 | This 4-bit ALU circuit is already a lot to take in, 484 | 4 位 ALU 已经要很多逻辑门了 485 | 486 | but our 8-bit ALU would require hundreds of logic gates to fully build 487 | 但我们的 8 位 ALU 会需要数百个逻辑门 488 | 489 | and engineers don't want to see all that complexity when using an ALU, 490 | 工程师不想在用 ALU 时去想那些事情, 491 | 492 | so they came up with a special symbol to wrap it all up, which looks like a big V'. 493 | 所以想了一个特殊符号来代表它,看起来像一个大 "V" 494 | 495 | Just another level of abstraction! 496 | 又一层抽象! 497 | 498 | Our 8-bit ALU has two inputs, A and B, each with 8 bits. 499 | 我们的 8 位 ALU 有两个输入,A和B,都是 8 位 (bits) 500 | 501 | We also need a way to specify what operation the ALU should perform, 502 | 我们还需要告诉 ALU 执行什么操作 503 | 504 | for example, addition or subtraction. 505 | 例如加法或减法 506 | 507 | For that, we use a 4-bit operation code. 508 | 所以我们用 4 位的操作代码 509 | 510 | We'll talk about this more in a later episode, 511 | 我们之后的视频会再细说 512 | 513 | but in brief, 1000 might be the command to add, while 1100 is the command for subtract. 514 | 简言之,"1000"可能代表加法命令 \N "1100"代表减法命令 515 | 516 | Basically, the operation code tells the ALU what operation to perform. 517 | 操作代码告诉 ALU 执行什么操作 518 | 519 | And the result of that operation on inputs A and B is an 8-bit output. 520 | 输出结果是 8 位的 521 | 522 | ALUs also output a series of Flags, 523 | ALU 还会输出一堆标志(Flag) 524 | 525 | which are 1-bit outputs for particular states and statuses. 526 | "标志"是1位的,代表特定状态. 527 | 528 | For example, if we subtract two numbers, and the result is 0, 529 | 比如相减两个数字,结果为 0 530 | 531 | our zero-testing circuit, the one we made earlier, sets the Zero Flag to True (1). 532 | 我们的零测试电路(前面做的)\N 会将零标志设为 True(1) 533 | 534 | This is useful if we are trying to determine if two numbers are are equal. 535 | 如果想知道两个数字是否相等,这个非常有用 536 | 537 | If we wanted to test if A was less than B, 538 | 如果想知道: A 是否小于 B 539 | 540 | we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true. 541 | 可以用 ALU 来算 A 减 B,看负标志是否为 true 542 | 543 | If it was, we know that A was smaller than B. 544 | 如果是 true,我们就知道 A 小于 B 545 | 546 | And finally, there's also a wire attached to the carry out on the adder we built, 547 | 最后,还有一条线连到加法器的进位 548 | 549 | so if there is an overflow, we'll know about it. 550 | 如果有溢出,我们就知道 551 | 552 | This is called the Overflow Flag. 553 | 这叫溢出标志 554 | 555 | Fancier ALUs will have more flags, 556 | 高级 ALU 有更多标志 557 | 558 | but these three flags are universal and frequently used. 559 | 但这 3 个标志是 ALU 普遍用的 560 | 561 | In fact, we'll be using them soon in a future episode. 562 | 其实,我们之后的视频会用到它们 563 | 564 | So now you know how your computer does all its basic mathematical operations digitally 565 | 现在你知道了\N 计算机是怎样在没有齿轮或杠杆的情况下 进行运算 566 | 567 | with no gears or levers required. 568 | 现在你知道了\N 计算机是怎样在没有齿轮或杠杆的情况下 进行运算 569 | 570 | We're going to use this ALU when we construct our CPU two episodes from now. 571 | 接下来两集 我们会用 ALU 做 CPU 572 | 573 | But before that, our computer is going to need some memory! 574 | 但在此之前,计算机需要一些 "记忆" ! 575 | 576 | We'll talk about that next week. 577 | 我们下周会讲 578 | 579 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/06. 寄存器 & 内存-Registers and RAM.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne and welcome to Crash Course Computer Science. 2 | 嗨,我是 Carrie Anne,欢迎收看计算机科学速成课 3 | 4 | So last episode, using just logic gates, we built a simple ALU, 5 | 上集,我们用逻辑门做了个简单 ALU 6 | 7 | which performs arithmetic and logic operations, hence the 'A' and the 'L'. 8 | 它能执行算术(Arithmetic)和逻辑(Logic)运算 \N ALU 里的 A 和 L 因此得名 9 | 10 | But of course, there's not much point in calculating a result only to throw it away 11 | 当然,算出来之后如果扔掉就没什么意义了 12 | 13 | - it would be useful to store that value somehow, 14 | 得找个方法存起来 15 | 16 | and maybe even run several operations in a row. 17 | 可能还要进行多个连续操作 18 | 19 | That's where computer memory comes in! 20 | 这就用到计算机内存了 21 | 22 | If you've ever been in the middle of a long RPG campaign on your console, 23 | 如果你在主机上打过一场长时间的对局 24 | 25 | or slogging through a difficult level on Minesweeper on your desktop, 26 | 或玩困难模式的 "扫雷" 27 | 28 | and your dog came by, tripped and pulled the power cord out of the wall, 29 | 然后狗跑过来,被电源线绊倒,把插头拔了出来 30 | 31 | you know the agony of losing all your progress. 32 | 你知道失去进度的痛苦 33 | 34 | Condolences. 35 | 真同情你 :( 36 | 37 | But the reason for your loss is that your console, your laptop and your computers 38 | 你损失数据的原因是 \N 电脑用的是"随机存取存储器",简称"RAM" 39 | 40 | make use of Random Access Memory, or RAM, 41 | 你损失数据的原因是 \N 电脑用的是"随机存取存储器",简称"RAM" 42 | 43 | which stores things like game state - as long as the power stays on. 44 | 它只能在有电的情况下存储东西,比如游戏状态 45 | 46 | Another type of memory, called persistent memory, can survive without power, 47 | 另一种存储 (memory) 叫持久存储,电源关闭时数据也不会丢失 48 | 49 | and it's used for different things; 50 | 它用来存其他东西. 51 | 52 | We'll talk about the persistence of memory in a later episode. 53 | 我们之后会讨论存储 (memory) 的持久性问题 54 | 55 | Today, we're going to start small 56 | 今天我们从简单开始 57 | 58 | - literally by building a circuit that can store one.. single.. bit of information. 59 | - 做只能存储 1 位的电路 60 | 61 | After that, we'll scale up, and build our very own memory module, 62 | 之后再扩大,做出我们的内存模块 63 | 64 | and we'll combine it with our ALU next time, when we finally build our very own CPU! 65 | 下次和 ALU 结合起来,做出 CPU! 66 | 67 | All of the logic circuits we've discussed so far go in one direction 68 | 我们至今说过的电路都是单向的 69 | 70 | - always flowing forward. 71 | - 总是向前流动 72 | 73 | like our 8-bit ripple adder from last episode. 74 | 比如上集的 8 位 "脉动进位加法器" 75 | 76 | But we can also create circuits that loop back on themselves. 77 | 但也可以做回向电路,把输出连回输入 78 | 79 | Let's try taking an ordinary OR gate, and feed the output back into one of its inputs 80 | 我们拿一个 OR 门试试,把输出连回输入 81 | 82 | and see what happens. 83 | 看看会发生什么 84 | 85 | First, let's set both inputs to 0. 86 | 首先,两个输入都设为 0 87 | 88 | So 0 OR 0 is 0, and so this circuit always outputs 0. 89 | "0 OR 0" 是 0,所以电路输出0 90 | 91 | If we were to flip input A to 1. 92 | 如果将 A 变成1 93 | 94 | 1 OR 0 is 1, so now the output of the OR gate is 1. 95 | "1 OR 0" 为 1,所以输出 1 96 | 97 | A fraction of a second later, that loops back around into input B, 98 | 一转眼的功夫,输出回到 B 99 | 100 | so the OR gate sees that both of its inputs are now 1. 101 | OR 门看到两个输入都是 1 102 | 103 | 1 OR 1 is still 1, so there is no change in output. 104 | "1 OR 1" 仍然为1,所以输出不变 105 | 106 | If we flip input A back to 0, the OR gate still outputs 1. 107 | 如果将 A 变成 0,OR 门依然输出 1 108 | 109 | So now we've got a circuit that records a "1" for us. 110 | 现在我们有个电路能记录 "1" 111 | 112 | Except, we've got a teensy tiny problem - this change is permanent! 113 | 然而有个小问题:这是永久的! 114 | 115 | No matter how hard we try, there's no way to get this circuit to flip back from a 1 to a 0. 116 | 无论怎么试,都没法从 1 变回 0 117 | 118 | Now let's look at this same circuit, but with an AND gate instead. 119 | 我们换成 AND 门看看会怎样 120 | 121 | We'll start inputs A and B both at 1. 122 | 开始时,A 和 B 都设 1 123 | 124 | 1 AND 1 outputs 1 forever. 125 | "1 AND 1" 永远输出 1 126 | 127 | But, if we then flip input A to 0, because it's an AND gate, the output will go to 0. 128 | 如果之后 A 设为 0,由于是 AND 门,输出会变成 0 129 | 130 | So this circuit records a 0, the opposite of our other circuit. 131 | 这个电路能记录 0,和之前那个相反 132 | 133 | Like before, no matter what input we apply to input A afterwards, the circuit will always output 0. 134 | 就像之前,无论 A 设什么值,电路始终输出 0 135 | 136 | Now we've got circuits that can record both 0s and 1s. 137 | 现在有了能存 0 和 1 的电路 138 | 139 | The key to making this a useful piece of memory is to combine our two circuits into what is 140 | 为了做出有用的存储 (memory) \N 我们把两个电路结合起来 141 | 142 | called the AND-OR Latch. 143 | 这叫 "AND-OR 锁存器" 144 | 145 | It has two inputs, a "set" input, which sets the output to a 1,\Ncalled the AND-OR Latch. 146 | It has two inputs, a "set" input, which sets the output to a 1, 147 | 它有两个输入\N "设置"输入, 把输出变成 1\N "复位"输入, 把输出变成 0 148 | 149 | and a "reset" input, which resets the output to a 0. 150 | 它有两个输入\N "设置"输入, 把输出变成 1\N "复位"输入, 把输出变成 0 151 | 152 | If set and reset are both 0, the circuit just outputs whatever was last put in it. 153 | 如果"设置"和"复位"都是 0,电路会输出最后放入的内容 154 | 155 | In other words, it remembers a single bit of information! 156 | 也就是说,它存住了 1 位的信息! 157 | 158 | Memory! 159 | 存储! 160 | 161 | This is called a "latch" because it "latches onto" a particular value and stays that way. 162 | 这叫"锁存", 因为它"锁定"了一个值 163 | 164 | The action of putting data into memory is called writing, whereas getting the data out is called reading. 165 | 放入数据的动作叫 "写入" ,拿出数据的动作叫 "读取" 166 | 167 | Ok, so we've got a way to store a single bit of information! 168 | 现在我们终于有办法存一个位了! 169 | 170 | Great! 171 | 超棒! 172 | 173 | Unfortunately, having two different wires for input - set and reset - is a bit confusing. 174 | 麻烦的是, 用两条线 "设置"和"复位" 来输入, 有点难理解 175 | 176 | To make this a little easier to use, we really want a single wire to input data, 177 | 为了更容易用,我们希望只有一条输入线 178 | 179 | that we can set to either 0 or 1 to store the value. 180 | 将它设为 0 或 1 来存储值 181 | 182 | Additionally, we are going to need a wire that enables the memory 183 | 还需要一根线来"启用"内存 184 | 185 | to be either available for writing or "locked" down 186 | 启用时允许写入,没启用时就 "锁定" 187 | 188 | - which is called the write enable line. 189 | - 这条线叫 "允许写入线" 190 | 191 | By adding a few extra logic gates, we can build this circuit, 192 | 加一些额外逻辑门,可以做出这个电路 193 | 194 | which is called a Gated Latch since the "gate" can be opened or closed. 195 | 这叫"门锁",因为门可以打开和关上 196 | 197 | Now this circuit is starting to get a little complicated. 198 | 现在有点复杂了 199 | 200 | We don't want to have to deal with all the individual logic gates... 201 | 我们不想关心单独的逻辑门 202 | 203 | so as before, we're going to bump up a level of abstraction, 204 | 所以我们提升一层抽象 205 | 206 | and put our whole Gated Latch circuit in a box -- a box that stores one bit. 207 | 把 "门锁" 放到盒子里 - 这个盒子能存一个 bit 208 | 209 | Let's test out our new component! 210 | 我们来测一下新组件! 211 | 212 | Let's start everything at 0. 213 | 一切从 0 开始 214 | 215 | If we toggle the Data wire from 0 to 1 or 1 to 0, 216 | 数据输入从0换到1, 从1换到0 217 | 218 | nothing happens - the output stays at 0. 219 | 什么也不会发生 - 输出依然是 0 220 | 221 | That's because the write enable wire is off, which prevents any change to the memory. 222 | 因为 "允许写入线" 是关闭的,所以内容不会变化 223 | 224 | So we need to "open" the "gate" by turning the write enable wire to 1. 225 | 所以要给 "允许写入线" 输入 1, "打开" 门 226 | 227 | Now we can put a 1 on the data line to save the value 1 to our latch. 228 | 现在往 "数据线" 放 1,1 就能存起来了 229 | 230 | Notice how the output is now 1.\NNow we can put a 1 on the data line to save the value 1 to our latch. 231 | Notice how the output is now 1. 232 | 注意输出现在是 1 了 233 | 234 | Success! 235 | 成功! 236 | 237 | We can turn off the enable line and the output stays as 1. 238 | 现在可以关掉 "允许写入线" ,输出会保持 1 239 | 240 | Once again, we can toggle the value on the data line all we want, 241 | 现在不管给 "数据线" 什么值 242 | 243 | but the output will stay the same. 244 | 输出都不会变 245 | 246 | The value is saved in memory. 247 | 值存起来了 248 | 249 | Now let's turn the enable line on again use our data line to set the latch to 0. 250 | 现在又打开 "允许写入线" \N "数据线" 设为0 251 | 252 | Done. 253 | 完成 254 | 255 | Enable line off, and the output is 0. 256 | "允许写入线" 关闭,输出 0 257 | 258 | And it works! 259 | 成功了! 260 | 261 | Now, of course, computer memory that only stores one bit of information isn't very useful 262 | 当然,只能存 1 bit 没什么大用 263 | 264 | -- definitely not enough to run Frogger. 265 | - 肯定玩不了游戏 266 | 267 | Or anything, really. 268 | 或做其它事情 269 | 270 | But we're not limited to using only one latch. 271 | 但我们没限制只能用一个锁存器 272 | 273 | If we put 8 latches side-by-side, we can store 8 bits of information like an 8-bit number. 274 | 如果我们并排放 8 个锁存器,\N 可以存 8 位信息,比如一个 8 bit 数字 275 | 276 | A group of latches operating like this is called a register, 277 | 一组这样的锁存器叫 "寄存器" 278 | 279 | which holds a single number, and the number of bits in a register is called its width. 280 | 寄存器能存一个数字,这个数字有多少位,叫"位宽" 281 | 282 | Early computers had 8-bit registers, then 16, 32, 283 | 早期电脑用 8 位寄存器,然后是 16 位,32 位 284 | 285 | and today, many computers have registers that are 64-bits wide. 286 | 如今许多计算机都有 64 位宽的寄存器 287 | 288 | To write to our register, we first have to enable all of the latches. 289 | 写入寄存器前,要先启用里面所有锁存器 290 | 291 | We can do this with a single wire that connects to all of their enable inputs, which we set to 1. 292 | 我们可以用一根线连接所有 "允许输入线", 把它设为 1 293 | 294 | We then send our data in using the 8 data wires, and then set enable back to 0, 295 | 然后用 8 条数据线发数据,然后将 "允许写入线" 设回 0 296 | 297 | and the 8 bit value is now saved in memory. 298 | 现在 8 位的值就存起来了 299 | 300 | Putting latches side-by-side works ok for a small-ish number of bits. 301 | 如果只有很少的位(bits),把锁存器并排放置,也勉强够用了. 302 | 303 | A 64-bit register would need 64 wires running to the data pins, and 64 wires running to the outputs. 304 | 64 位寄存器要 64 根数据线,64 根连到输出端 305 | 306 | Luckily we only need 1 wire to enable all the latches, but that's still 129 wires. 307 | 幸运的是,我们只要 1 根线启用所有锁存器 \N 但加起来也有 129 条线了 308 | 309 | For 256 bits, we end up with 513 wires! 310 | 如果存 256 位要 513 条线! 311 | 312 | The solution is a matrix! 313 | 解决方法是矩阵! 314 | 315 | In this matrix, we don't arrange our latches in a row, 316 | 在矩阵中,我们不并列排放锁存器 317 | 318 | we put them in a grid. 319 | 而是做成网格 320 | 321 | For 256 bits, we need a 16 by 16 grid of latches with 16 rows and columns of wires. 322 | 存 256 位,我们用 16x16 网格的锁存器,有 16 行 16 列 323 | 324 | To activate any one latch, we must turn on the corresponding row AND column wire. 325 | 要启用某个锁存器,就打开相应的 行线 和 列线 326 | 327 | Let's zoom in and see how this works. 328 | 放大看看怎么做的 329 | 330 | We only want the latch at the intersection of the two active wires to be enabled, 331 | 我们只想打开交叉处 锁存器的 "允许写入线" 332 | 333 | but all of the other latches should stay disabled. 334 | 所有其他锁存器,保持关闭 335 | 336 | For this, we can use our trusty AND gate! 337 | 我们可以用 AND 门! 338 | 339 | The AND gate will output a 1 only if the row and the column wires are both 1. 340 | 只有 行线和列线 均为 1 ,AND 门才输出 1 341 | 342 | So we can use this signal to uniquely select a single latch. 343 | 所以可以用选择单个锁存器 344 | 345 | This row/column setup connects all our latches with a single, shared, write enable wire. 346 | 这种行/列排列法,用一根 "允许写入线" 连所有锁存器 347 | 348 | In order for a latch to become write enabled, 349 | 为了让锁存器变成 "允许写入" 350 | 351 | the row wire, the column wire, and the write enable wire must all be 1. 352 | 行线,列线和 "允许写入线" 都必须是 1 353 | 354 | That should only ever be true for one single latch at any given time. 355 | 每次只有 1 个锁存器会这样 356 | 357 | This means we can use a single, shared wire for data. 358 | 代表我们可以只用一根 "数据线" \N 连所有锁存器来传数据 359 | 360 | Because only one latch will ever be write enabled, only one will ever save the data 361 | 因为只有一个锁存器会启用,只有那个会存数据 362 | 363 | -- the rest of the latches will simply ignore values on the data wire because they are not write enabled. 364 | 其他锁存器会忽略数据线上的值,因为没有 "允许写入" 365 | 366 | We can use the same trick with a read enable wire to read the data later, 367 | 我们可以用类似的技巧, 做"允许读取线"来读数据 368 | 369 | to get the data out of one specific latch. 370 | 从一个指定的锁存器,读取数据 371 | 372 | This means in total, for 256 bits of memory, 373 | 所以对于 256 位的存储 374 | 375 | we only need 35 wires - 1 data wire, 1 write enable wire, 1 read enable wire, 376 | 只要 35 条线 \N1条"数据线", 1条"允许写入线", 1条"允许读取线" 377 | 378 | and 16 rows and columns for the selection. 379 | 还有16行16列的线用于选择锁存器 \N (16+16=32, 32+3=35) 380 | 381 | That's significant wire savings! 382 | 这省了好多线! 383 | 384 | But we need a way to uniquely specify each intersection. 385 | 但我们需要某种方法来 唯一指定 交叉路口 386 | 387 | We can think of this like a city, 388 | 我们可以想成城市 389 | 390 | where you might want to meet someone at 12th avenue and 8th street 391 | 你可能想和别人 在第 12 大道和第 8 街的交界碰面 392 | 393 | -- that's an address that defines an intersection. 394 | - 这是一个交叉点的地址 395 | 396 | The latch we just saved our one bit into has an address of row 12 and column 8. 397 | 我们刚刚存了一位的地址是 "12行 8列" 398 | 399 | Since there is a maximum of 16 rows, we store the row address in a 4 bit number. 400 | 由于最多 16 行, 用 4 位就够了 401 | 402 | 12 is 1100 in binary. 403 | 12 用二进制表示为 1100 404 | 405 | We can do the same for the column address: 8 is 1000 in binary. 406 | 列地址也可以这样: 8 用二进制表示为 1000 407 | 408 | So the address for the particular latch we just used can be written as 11001000. 409 | 刚才说的"12行 8列"可以写成 11001000 410 | 411 | To convert from an address into something that selects the right row or column, 412 | 为了将地址转成 行和列 413 | 414 | we need a special component called a multiplexer 415 | 我们需要 "多路复用器" 416 | 417 | -- which is the computer component with a pretty cool name at least compared to the ALU. 418 | - 这个名字起码比 ALU 酷一点 419 | 420 | Multiplexers come in all different sizes, 421 | 多路复用器有不同大小 422 | 423 | but because we have 16 rows, we need a 1 to 16 multiplexer. 424 | 因为有 16 行,我们需要 1 到 16 多路复用器 425 | 426 | It works like this. 427 | 工作方式是 428 | 429 | You feed it a 4 bit number, and it connects the input line to a corresponding output line. 430 | 输入一个 4 位数字,它会把那根线,连到相应的输出线 431 | 432 | So if we pass in 0000, it will select the very first column for us. 433 | 如果输入 0000,它会选择第一列 434 | 435 | If we pass in 0001, the next column is selected, and so on. 436 | 如果输入 0001,会选择下一列,依此类推 437 | 438 | We need one multiplexer to handle our rows and another multiplexer to handle the columns. 439 | 一个多路复用器处理行(row) \N 另一个多路复用器处理列(column) 440 | 441 | Ok, it's starting to get complicated again, 442 | 好吧,开始有点复杂了 443 | 444 | so let's make our 256-bit memory its own component. 445 | 那么把 256 位内存当成一个整体好了 446 | 447 | Once again a new level of abstraction! 448 | 又提升了一层抽象! 449 | 450 | It takes an 8-bit address for input - the 4 bits for the column and 4 for the row. 451 | 它输入一个 8 位地址:4 位代表列,4 位代表行 452 | 453 | We also need write and read enable wires. 454 | 我们还需要 "允许写入线" 和 "允许读取线" 455 | 456 | And finally, we need just one data wire, which can be used to read or write data. 457 | 最后,还需要一条数据线,用于读/写数据 458 | 459 | Unfortunately, even 256-bits of memory isn't enough to run much of anything, 460 | 不幸的是, 256 位的内存也没法做什么事 461 | 462 | so we need to scale up even more! 463 | 所以还要扩大规模 464 | 465 | We're going to put them in a row. 466 | 把它们并排放置 467 | 468 | Just like with the registers. 469 | 就像寄存器一样 470 | 471 | We'll make a row of 8 of them, so we can store an 8 bit number - also known as a byte. 472 | 一行8个,可以存一个 8 位数字 \N 8 位也叫一个字节(byte) 473 | 474 | To do this, we feed the exact same address into all 8 of our 256-bit memory components at the same time, 475 | 为了存一个 8 位数字,我们同时给 8 个 256 位内存一样的地址 476 | 477 | and each one saves one bit of the number. 478 | 每个地址存 1 位 479 | 480 | That means the component we just made can store 256 bytes at 256 different addresses. 481 | 意味着这里总共能存 256 个字节 (byte) 482 | 483 | Again, to keep things simple, we want to leave behind this inner complexity. 484 | 再次,为了简单,我们不管内部 485 | 486 | Instead of thinking of this as a series of individual memory modules and circuits, 487 | 不看作是一堆独立的存储模块和电路 488 | 489 | we'll think of it as a uniform bank of addressable memory. 490 | 而是看成一个整体的可寻址内存 491 | 492 | We have 256 addresses, 493 | 我们有 256 个地址 494 | 495 | and at each address, we can read or write an 8-bit value. 496 | 每个地址能读或写一个 8 位值 497 | 498 | We're going to use this memory component next episode when we build our CPU. 499 | 我们下集做 CPU 时会用到这个内存 500 | 501 | The way that modern computers scale to megabytes and gigabytes of memory 502 | 现代计算机的内存 \N 扩展到上兆字节(MB)和千兆字节(GB)的方式 503 | 504 | is by doing the same thing we've been doing here 505 | 和我们这里做的一样 506 | 507 | -- keep packaging up little bundles of memory into larger, and larger, and larger arrangements. 508 | 不断把内存打包到更大规模 509 | 510 | As the number of memory locations grow, our addresses have to grow as well. 511 | 随着内存地址增多,内存地址也必须增长 512 | 513 | 8 bits hold enough numbers to provide addresses for 256 bytes of our memory, 514 | 8 位最多能代表 256 个内存地址 \N(1111 1111 是255,0~255 一共 256 个数字) 515 | 516 | but that's all. 517 | 只有这么多 518 | 519 | To address a gigabyte - or a billion bytes of memory - we need 32-bit addresses. 520 | 要给千兆或十亿字节的内存寻址,需要 32 位的地址 521 | 522 | An important property of this memory is that we can access any memory location, at any time, and in a random order. 523 | 内存的一个重要特性是:可以随时访问任何位置 524 | 525 | For this reason, it's called Random-Access Memory or RAM. 526 | 因此叫 "随机存取存储器" ,简称 RAM 527 | 528 | When you hear people talking about how much RAM a computer has 529 | 当你听到有人说 RAM 有多大 530 | 531 | - that's the computer's memory. 532 | 他的意思是内存有多大 533 | 534 | RAM is like a human's short term or working memory, 535 | RAM 就像人类的短期记忆 536 | 537 | where you keep track of things going on right now 538 | 记录当前在做什么事 539 | 540 | - like whether or not you had lunch or paid your phone bill. 541 | 比如吃了午饭没,或有没有交电话费 542 | 543 | Here's an actual stick of RAM - with 8 memory modules soldered onto the board. 544 | 这是一条真的内存,上面焊了 8 个内存模块 545 | 546 | If we carefully opened up one of these modules and zoomed in, 547 | 如果打开其中一个,然后放大 548 | 549 | The first thing you would see are 32 squares of memory. 550 | 会看到 32 个内存方块 551 | 552 | Zoom into one of those squares, and we can see each one is comprised of 4 smaller blocks. 553 | 放大其中一个方块,可以看到有 4 个小块 554 | 555 | If we zoom in again, we get down to the matrix of individual bits. 556 | 如果再放大,可以看到存一个"位"的矩阵 557 | 558 | This is a matrix of 128 by 64 bits. 559 | 这个矩阵是 128 位 x 64 位 560 | 561 | That's 8192 bits in total. 562 | 总共 8192 位 563 | 564 | Each of our 32 squares has 4 matrices, so that's 32 thousand, 7 hundred and 68 bits. 565 | 每个方格 4 个矩阵 \N 所以一个方格有 32768 个位 (8192 x 4 = 32768) 566 | 567 | And there are 32 squares in total. 568 | 而一共 32 个方格 569 | 570 | So all in all, that's roughly 1 million bits of memory in each chip. 571 | 总而言之,1 个芯片大约存 100 万位 572 | 573 | Our RAM stick has 8 of these chips, so in total, this RAM can store 8 millions bits, 574 | RAM 有 8 个芯片,所以总共 800 万位 575 | 576 | otherwise known as 1 megabyte. 577 | 也就是 1 兆字节(1 MB) 578 | 579 | That's not a lot of memory these days -- this is a RAM module from the 1980's. 580 | 1 MB 如今不算大 - 这是 1980 年代的 RAM 581 | 582 | Today you can buy RAM that has a gigabyte or more of memory 583 | 如今你可以买到千兆字节(GB)的 RAM 584 | 585 | - that's billions of bytes of memory. 586 | 那可是数十亿字节的内存 587 | 588 | So, today, we built a piece of SRAM - Static Random-Access Memory - which uses latches. 589 | 今天,我们用锁存器做了一块 SRAM(静态随机存取存储器) 590 | 591 | There are other types of RAM, such as DRAM, Flash memory, and NVRAM. 592 | 还有其他类型的 RAM,如 DRAM,闪存和 NVRAM 593 | 594 | These are very similar in function to SRAM, 595 | 它们在功能上与 SRAM 相似 596 | 597 | but use different circuits to store the individual bits 598 | 但用不同的电路存单个位 599 | 600 | - for example, using different logic gates, capacitors, charge traps, or memristors. 601 | - 比如用不同的逻辑门,电容器,电荷捕获或忆阻器 602 | 603 | But fundamentally, all of these technologies store bits of information 604 | 但根本上 这些技术都是矩阵层层嵌套,来存储大量信息 605 | 606 | in massively nested matrices of memory cells. 607 | 但根本上 这些技术都是矩阵层层嵌套,来存储大量信息 608 | 609 | Like many things in computing, the fundamental operation is relatively simple. 610 | 就像计算机中的很多事情,底层其实都很简单 611 | 612 | it's the layers and layers of abstraction that's mind blowing 613 | 让人难以理解的,是一层层精妙的抽象 614 | 615 | -- like a russian doll that keeps getting smaller and smaller and smaller. 616 | 像一个越来越小的俄罗斯套娃 617 | 618 | I'll see you next week. 619 | 下周见 620 | 621 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/07. 中央处理器-The Central Processing Unit(CPU).ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne, this is Crash Course Computer Science 2 | 嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | and today, we're talking about processors. 5 | 今天我们讲 处理器 6 | 7 | Just a warning though - this is probably the most complicated episode in the series. 8 | 提示下 - 这集可能是最难的一集 9 | 10 | So once you get this, you're golden. 11 | 所以一旦你理解了,就会变得超厉害der~ 12 | 13 | We've already made a Arithmetic and Logic Unit, 14 | 我们已经做了一个算术逻辑单元(ALU) 15 | 16 | which takes in binary numbers and performs calculations, 17 | 输入二进制,它会执行计算 18 | 19 | and we've made two types of computer memory: 20 | 我们还做了两种内存: 21 | 22 | Registers -- small, linear chunks of memory, useful for storing a single value 23 | 寄存器 - 很小的一块内存,能存一个值 24 | 25 | and then we scaled up, and made some RAM, 26 | 之后我们增大做出了 RAM 27 | 28 | a larger bank of memory that can store a lot of numbers located at different addresses. 29 | RAM 是一大块内存,能在不同地址存大量数字 30 | 31 | Now it's time to put it all together and build ourselves the heart of any computer, 32 | 现在是时候把这些放在一起,组建计算机的 "心脏" 了 33 | 34 | but without any of the emotional baggage that comes with human hearts. 35 | 但这个 "心脏" 不会有任何包袱,比如人类情感. 36 | 37 | For computers, this is the Central Processing Unit, most commonly called the CPU. 38 | 计算机的心脏是"中央处理单元",简称 "CPU" 39 | 40 | A CPU's job is to execute programs. 41 | CPU 负责执行程序 42 | 43 | Programs, like Microsoft Office, Safari, or your beloved copy of Half Life: 2, 44 | 比如 Office,Safari 浏览器,你最爱的 《半条命2》 45 | 46 | are made up of a series of individual operations, 47 | 程序由一个个操作组成 48 | 49 | called instructions, because they "instruct" the computer what to do. 50 | 这些操作叫"指令"(Instruction) \N 因为它们"指示"计算机要做什么 51 | 52 | If these are mathematical instructions, like add or subtract, 53 | 如果是数学指令,比如加/减 54 | 55 | the CPU will configure its ALU to do the mathematical operation. 56 | CPU 会让 ALU 进行数学运算 57 | 58 | Or it might be a memory instruction, 59 | 也可能是内存指令,CPU 会和内存通信,然后读/写值 60 | 61 | in which case the CPU will talk with memory to read and write values. 62 | 也可能是内存指令,CPU 会和内存通信,然后读/写值 63 | 64 | There are a lot of parts in a CPU, 65 | CPU 里有很多组件. 66 | 67 | so we're going to lay it out piece by piece, building up as we go. 68 | 所以我们一边说一边建 69 | 70 | We'll focus on functional blocks, rather than showing every single wire. 71 | 我们把重点放在功能,而不是一根根线具体怎么连 72 | 73 | When we do connect two components with a line, 74 | 当我们用一条线连接两个组件时 75 | 76 | this is an abstraction for all of the necessary wires. 77 | 这条线只是所有必须线路的一个抽象 78 | 79 | This high level view is called the microarchitecture. 80 | 这种高层次视角叫 "微体系架构" 81 | 82 | OK, first, we're going to need some memory. 83 | 好,我们首先要一些内存,把上集做的 RAM 拿来就行 84 | 85 | Lets drop in the RAM module we created last episode. 86 | 好,我们首先要一些内存,把上集做的 RAM 拿来就行 87 | 88 | To keep things simple, we'll assume it only has 16 memory locations, each containing 8 bits. 89 | 为了保持简单,假设它只有 16 个位置,每个位置存 8 位 90 | 91 | Let's also give our processor four, 8-bit memory registers, labeled A, B, C and D 92 | 再来四个 8 位寄存器,叫 A,B,C,D 93 | 94 | which will be used to temporarily store and manipulate values. 95 | 寄存器用来 临时存数据 和 操作数据 96 | 97 | We already know that data can be stored in memory as binary values 98 | 我们已经知道数据 是以二进制值存在内存里 99 | 100 | and programs can be stored in memory too. 101 | 程序也可以存在内存里 102 | 103 | We can assign an ID to each instruction supported by our CPU. 104 | 我们可以给 CPU 支持的所有指令,分配一个 ID 105 | 106 | 指令表 107 | 108 | 指令 109 | 110 | 描述 111 | 112 | 4位操作码 113 | 114 | 地址或寄存器 115 | 116 | In our hypothetical example, we use the first four bits to store the "operation code", 117 | 在这个假设的例子 \N 我们用前四位存 "操作代码" (operation code) 118 | 119 | or opcode for short. 120 | 简称 "操作码" (opcode) 121 | 122 | The final four bits specify where the data for that operation should come from - 123 | 后四位代表数据来自哪里 124 | 125 | this could be registers or an address in memory. 126 | - 可以是寄存器或内存地址 127 | 128 | We also need two more registers to complete our CPU. 129 | 我们还需要两个寄存器,来完成 CPU. 130 | 131 | First, we need a register to keep track of where we are in a program. 132 | 1. 一个寄存器追踪程序运行到哪里了,我们叫它 "指令地址寄存器" 133 | 134 | For this, we use an instruction address register, 135 | 1. 一个寄存器追踪程序运行到哪里了,我们叫它 "指令地址寄存器" 136 | 137 | which as the name suggests, stores the memory address of the current instruction. 138 | 顾名思义,存当前指令的内存地址 139 | 140 | And then we need the other register to store the current instruction, 141 | 2. 另一个寄存器存当前指令,叫 "指令寄存器" 142 | 143 | which we'll call the instruction register. 144 | 2. 另一个寄存器存当前指令,叫 "指令寄存器" 145 | 146 | When we first boot up our computer, all of our registers start at 0. 147 | 当启动计算机时,所有寄存器从 0 开始 148 | 149 | As an example, we've initialized our RAM with a simple computer program that we'll to through today. 150 | 为了举例,我们在 RAM 里放了一个程序,我们今天会过一遍 151 | 152 | The first phase of a CPU's operation is called the fetch phase. 153 | CPU 的第一个阶段叫 "取指令阶段" 154 | 155 | This is where we retrieve our first instruction. 156 | 负责拿到指令 157 | 158 | First, we wire our Instruction Address Register to our RAM module. 159 | 首先,将 "指令地址寄存器" 连到 RAM 160 | 161 | The register's value is 0, so the RAM returns whatever value is stored in address 0. 162 | 寄存器的值为 0,因此 RAM 返回地址 0 的值 163 | 164 | In this case, 0010 1110. 165 | 0010 1110 会复制到 "指令寄存器" 里 166 | 167 | Then this value is copied into our instruction register. 168 | 0010 1110 会复制到 "指令寄存器" 里 169 | 170 | Now that we've fetched an instruction from memory, 171 | 现在指令拿到了 172 | 173 | we need to figure out what that instruction is 174 | 要弄清是什么指令,才能执行(execute) 175 | 176 | so we can execute it. 177 | 要弄清是什么指令,才能执行(execute) 178 | 179 | That is run it. 180 | 而不是杀死(kill)它 181 | 182 | Not kill it. 183 | 而不是杀死(kill)它 184 | 185 | This is called the decode phase. 186 | 这是 "解码阶段" 187 | 188 | 指令表 189 | 190 | 指令 191 | 192 | 描述 193 | 194 | 4位操作码 195 | 196 | 地址或寄存器 197 | 198 | In this case the opcode, which is the first four bits, is: 0010. 199 | 前 4 位 0010 是 LOAD A 指令 200 | 201 | This opcode corresponds to the "LOAD A" instruction, 202 | 前 4 位 0010 是 LOAD A 指令 203 | 204 | which loads a value from RAM into Register A. 205 | 意思是,把 RAM 的值放入寄存器 A 206 | 207 | The RAM address is the last four bits of our instruction which are 1110, or 14 in decimal. 208 | 后 4 位 1110 是 RAM 的地址, 转成十进制是 14 209 | 210 | Next, instructions are decoded and interpreted by a Control Unit. 211 | 接下来,指令由 "控制单元" 进行解码 212 | 213 | Like everything else we've built, it too is made out of logic gates. 214 | 就像之前的所有东西 \N "控制单元" 也是逻辑门组成的 215 | 216 | For example, to recognize a LOAD A instruction, 217 | 比如,为了识别 "LOAD A" 指令 218 | 219 | we need a circuit that checks if the opcode matches 0010 220 | 需要一个电路,检查操作码是不是 0010 221 | 222 | which we can do with a handful of logic gates. 223 | 我们可以用很少的逻辑门来实现. 224 | 225 | Now that we know what instruction we're dealing with, 226 | 现在知道了是什么指令 227 | 228 | we can go ahead and perform that instruction which is the beginning of the execute phase! 229 | 就可以开始执行了,开始 "执行阶段" 230 | 231 | Using the output of our LOAD_A checking circuit, 232 | 用 "检查是否 LOAD_A 指令的电路" 233 | 234 | we can turn on the RAM's read enable line and send in address 14. 235 | 可以打开 RAM 的 "允许读取线", 把地址 14 传过去 236 | 237 | The RAM retrieves the value at that address, which is 00000011, or 3 in decimal. 238 | RAM 拿到值,0000 0011,十进制的 3 239 | 240 | Now, because this is a LOAD_A instruction, 241 | 因为是 LOAD_A 指令 \N 我们想把这个值只放到寄存器 A,其他寄存器不受影响 242 | 243 | we want that value to only be saved into Register A and not any of the other registers. 244 | 因为是 LOAD_A 指令 \N 我们想把这个值只放到寄存器 A,其他寄存器不受影响 245 | 246 | So if we connect the RAM's data wires to our four data registers, 247 | 所以需要一根线,把 RAM 连到 4 个寄存器 248 | 249 | we can use our LOAD_A check circuit to enable the write enable only for Register A. 250 | 用 "检查是否 LOAD_A 指令的电路" \N 启用寄存器 A 的 "允许写入线" 251 | 252 | And there you have it 253 | 这就成功了 254 | 255 | -- we've successfully loaded the value at RAM address 14 into Register A. 256 | - 把 RAM 地址 14 的值,放到了寄存器 A. 257 | 258 | We've completed the instruction, so we can turn all of our wires off, 259 | 既然指令完成了,我们可以关掉所有线路 260 | 261 | and we are ready to fetch the next instruction in memory. 262 | 去拿下一条指令 263 | 264 | To do this, we increment the Instruction Address Register by 1 which completes the execute phase. 265 | 我们把 "指令地址寄存器"+1,"执行阶段"就此结束. 266 | 267 | LOAD_A is just one of several possible instructions that our CPU can execute. 268 | LOAD_A 只是 CPU 可以执行的各种指令之一 269 | 270 | Different instructions are decoded by different logic circuits, 271 | 不同指令由不同逻辑电路解码 272 | 273 | which configure the CPU's components to perform that action. 274 | 这些逻辑电路会配置 CPU 内的组件来执行对应操作 275 | 276 | Looking at all those individual decode circuits is too much detail, 277 | 具体分析这些解码电路太繁琐了 278 | 279 | so since we looked at one example, 280 | 既然已经看了 1 个例子, 281 | 282 | we're going to go head and package them all up as a single Control Unit to keep things simple. 283 | 干脆把 "控制单元 "包成一个整体,简洁一些. 284 | 285 | That's right a new level of abstraction. 286 | 没错,一层新抽象 287 | 288 | The Control Unit is comparable to the conductor of an orchestra, 289 | 控制单元就像管弦乐队的指挥 290 | 291 | directing all of the different parts of the CPU. 292 | "指挥" CPU 的所有组件 293 | 294 | Having completed one full fetch/decode/execute cycle, 295 | "取指令→解码→执行" 完成后 296 | 297 | we're ready to start all over again, beginning with the fetch phase. 298 | 现在可以再来一次,从 "取指令" 开始 299 | 300 | The Instruction Address Register now has the value 1 in it, 301 | "指令地址寄存器" 现在的值是 1 302 | 303 | so the RAM gives us the value stored at address 1, which is 0001 1111. 304 | 所以 RAM 返回地址 1 里的值:0001 1111 305 | 306 | On to the decode phase! 307 | 到 "解码" 阶段! 308 | 309 | 0001 is the "LOAD B" instruction, which moves a value from RAM into Register B. 310 | 0001 是 LOAD B 指令 \N 从 RAM 里把一个值复制到寄存器 B 311 | 312 | The memory location this time is 1111, which is 15 in decimal. 313 | 这次内存地址是 1111,十进制的 15 314 | 315 | Now to the execute phase! 316 | 现在到 "执行阶段"! 317 | 318 | The Control Unit configures the RAM to read address 15 and configures Register B to receive the data. 319 | "控制单元" 叫 RAM 读地址 15,并配置寄存器 B 接收数据 320 | 321 | Bingo, we just saved the value 00001110, or the number 14 in decimal, into Register B. 322 | 成功,我们把值 0000 1110 \N 也就是十进制的 14 存到了寄存器 B 323 | 324 | Last thing to do is increment our instruction address register by 1, 325 | 最后一件事是 "指令地址寄存器" +1 326 | 327 | and we're done with another cycle. 328 | 我们又完成了一个循环 329 | 330 | Our next instruction is a bit different. 331 | 下一条指令有点不同 332 | 333 | Let's fetch it. 334 | 来取它吧 335 | 336 | 1000 0100. 337 | 1000 0100 338 | 339 | That opcode 1000 is an ADD instruction. 340 | 1000 是 ADD 指令 341 | 342 | Instead of an 4-bit RAM address, this instruction uses two sets of 2 bits. 343 | 这次后面的 4 位不是 RAM 地址,\N 而是 2 位 2 位分别代表 2 个寄存器 344 | 345 | Remember that 2 bits can encode 4 values, 346 | 2 位可以表示 4 个值 347 | 348 | so 2 bits is enough to select any one of our 4 registers. 349 | 所以足够表示 4 个寄存器 350 | 351 | The first set of 2 bits is 01, which in this case corresponds to Register B, 352 | 第一个地址是 01, 代表寄存器B 353 | 354 | and 00, which is Register A. 355 | 第二个地址是 00, 代表寄存器A 356 | 357 | So "1000 01 00" is the instruction for adding the value in Register B into the value in register A. 358 | 因此,1000 0100,代表把寄存器 B 的值,加到寄存器 A 里 359 | 360 | So to execute this instruction, we need to integrate the ALU we made in Episode 5 into our CPU. 361 | 为了执行这个指令,我们要整合第 5 集的 ALU 362 | 363 | The Control Unit is responsible for selecting the right registers to pass in as inputs, 364 | "控制单元" 负责选择正确的寄存器作为输入 365 | 366 | and configuring the ALU to perform the right operation. 367 | 并配置 ALU 执行正确的操作 368 | 369 | For this ADD instruction, the Control Unit enables Register B 370 | 对于 "ADD" 指令, "控制单元" 会 371 | 372 | and feeds its value into the first input of the ALU. 373 | 启用寄存器 B,作为 ALU 的第一个输入 374 | 375 | It also enables Register A and feeds it into the second ALU input. 376 | 还启用寄存器 A,作为 ALU 的第二个输入 377 | 378 | As we already discussed, the ALU itself can perform several different operations, 379 | 之前说过,ALU 可以执行不同操作 380 | 381 | so the Control Unit must configure it to perform an ADD operation by passing in the ADD opcode. 382 | 所以控制单元必须传递 ADD 操作码告诉它要做什么 383 | 384 | Finally, the output should be saved into Register A. 385 | 最后,结果应该存到寄存器 A 386 | 387 | But it can't be written directly 388 | 但不能直接写入寄存器 A 389 | 390 | because the new value would ripple back into the ALU and then keep adding to itself. 391 | 这样新值会进入 ALU ,不断和自己相加 392 | 393 | So the Control Unit uses an internal register to temporarily save the output, 394 | 因此,控制单元用一个自己的寄存器暂时保存结果 395 | 396 | turn off the ALU, and then write the value into the proper destination register. 397 | 关闭 ALU,然后把值写入正确的寄存器 398 | 399 | In this case, our inputs were 3 and 14, and so the sum is 17, or 00010001 in binary, 400 | 这里 3+14=17,二进制是 0001 0001 401 | 402 | which is now sitting in Register A. 403 | 现在存到了寄存器 A 404 | 405 | As before, the last thing to do is increment our instruction address by 1, 406 | 和之前一样,最后一件事是把指令地址 + 1 407 | 408 | and another cycle is complete. 409 | 这个循环就完成了 410 | 411 | Okay, so let's fetch one last instruction: 0100 1101. 412 | 好,来看最后一个指令:0100 1101 413 | 414 | When we decode it we see that 0100 is a STORE_A instruction, with a RAM address of 13. 415 | 解码得知是 STORE A 指令(把寄存器 A 的值放入内存) \N RAM 地址 13 416 | 417 | As usual, we pass the address to the RAM module, 418 | 接下来,把地址传给 RAM 419 | 420 | but instead of read-enabling the memory, we write-enable it. 421 | 但这次不是 "允许读取" ,而是 "允许写入" 422 | 423 | At the same time, we read-enable Register A. 424 | 同时,打开寄存器 A 的 "允许读取" 425 | 426 | This allows us to use the data line to pass in the value stored in register A. 427 | 这样就可以把寄存器 A 里的值,传给 RAM 428 | 429 | Congrats, we just ran our first computer program! 430 | 恭喜,我们刚运行了第一个电脑程序! 431 | 432 | It loaded two values from memory, added them together, 433 | 它从内存中加载两个值,相加,然后把结果放回内存 434 | 435 | and then saved that sum back into memory. 436 | 它从内存中加载两个值,相加,然后把结果放回内存 437 | 438 | Of course, by me talking you through the individual steps, 439 | 刚刚是我一步步来讲的, 440 | 441 | I was manually transitioning the CPU through its fetch, decode and execute phases. 442 | 我们人工切换 CPU 的状态 "取指令→解码→执行" 443 | 444 | But there isn't a mini Carrie Anne inside of every computer. 445 | 但不是每台电脑里都有一个迷你 Carrie Anne 446 | 447 | So the responsibility of keeping the CPU ticking along falls to a component called the clock. 448 | 其实是 "时钟" 来负责管理 CPU 的节奏 449 | 450 | As it's name suggests, the clock triggers an electrical signal at a precise and regular interval. 451 | 时钟以精确的间隔 触发电信号 452 | 453 | Its signal is used by the Control Unit to advance the internal operation of the CPU, 454 | 控制单元会用这个信号,推进 CPU 的内部操作 455 | 456 | keeping everything in lock-step 457 | 确保一切按步骤进行 458 | 459 | - like the dude on a Roman galley drumming rhythmically at the front, 460 | - 就像罗马帆船的船头,有一个人负责按节奏的击鼓, 461 | 462 | keeping all the rowers synchronized... or a metronome. 463 | 让所有划船的人同步... 就像节拍器一样 464 | 465 | Of course you can't go too fast, 466 | 节奏不能太快 467 | 468 | because even electricity takes some time to travel down wires and for the signal to settle. 469 | 因为就算是电也要一定时间来传输 470 | 471 | The speed at which a CPU can carry out each step of the fetch-decode-execute cycle 472 | CPU "取指令→解码→执行" 的速度叫 "时钟速度" 473 | 474 | is called its Clock Speed. 475 | CPU "取指令→解码→执行" 的速度叫 "时钟速度" 476 | 477 | This speed is measured in Hertz - a unit of frequency. 478 | 单位是赫兹 - 赫兹是用来表示频率的单位 479 | 480 | One Hertz means one cycle per second. 481 | 1 赫兹代表一秒 1 个周期 482 | 483 | Given that it took me about 6 minutes to talk you through 4 instructions 484 | 因为我花了大概 6 分钟,给你讲了 4 条指令 485 | 486 | LOAD, LOAD, ADD and STORE 487 | 读取→读取→相加→存储 488 | 489 | that means I have an effective clock speed of roughly .03 Hertz. 490 | 所以我的时钟速度大概是 0.03 赫兹 491 | 492 | Admittedly, I'm not a great computer 493 | 我承认我算数不快 494 | 495 | but even someone handy with math might only be able to do one calculation in their head every second or 1 Hertz. 496 | 但哪怕有人算数很快,最多也就是一秒一次,或 1 赫兹 497 | 498 | The very first, single-chip CPU was the Intel 4004, a 4-bit CPU released in 1971. 499 | 第一个单芯片 CPU 是 "英特尔 4004" \N 1971 年发布的 4 位CPU 500 | 501 | It's microarchitecture is actually pretty similar to our example CPU. 502 | 它的微架构 很像我们之前说的 CPU 503 | 504 | Despite being the first processor of its kind, 505 | 虽然是第一个单芯片的处理器 506 | 507 | it had a mind-blowing clock speed of 740 Kilohertz 508 | 但它的时钟速度达到了 740 千赫兹 - 每秒 74 万次 509 | 510 | -- that's 740 thousand cycles per second. 511 | 但它的时钟速度达到了 740 千赫兹 - 每秒 74 万次 512 | 513 | You might think that's fast, 514 | 你可能觉得很快 515 | 516 | but it's nothing compared to the processors that we use today. 517 | 但和如今的处理器相比不值一提 518 | 519 | One megahertz is one million clock cycles per second, 520 | 一兆赫兹是 1 秒 1 百万个时钟周期 521 | 522 | and the computer or even phone that you are watching this video on right now is no doubt a few gigahertz 523 | 你现在看视频的电脑或手机,肯定有几千兆赫兹 524 | 525 | -- that's BILLIONs of CPU cycles every single... second. 526 | - 1 秒 10 亿次时钟周期 527 | 528 | Also, you may have heard of people overclocking their computers. 529 | 你可能听过有人会把计算机超频 530 | 531 | This is when you modify the clock to speed up the tempo of the CPU 532 | 意思是修改时钟速度,加快 CPU 的速度 533 | 534 | -- like when the drummer speeds up when the Roman Galley needs to ram another ship. 535 | - 就像罗马帆船要撞另一艘船时,鼓手会加快敲鼓速度 536 | 537 | Chip makers often design CPUs with enough tolerance to handle a little bit of overclocking, 538 | 芯片制造商经常给 CPU 留一点余地,可以接受一点超频 539 | 540 | but too much can either overheat the CPU, 541 | 但超频太多会让 CPU 过热 542 | 543 | or produce gobbledygook as the signals fall behind the clock. 544 | 或产生乱码,因为信号跟不上时钟 545 | 546 | And although you don't hear very much about underclocking, 547 | 你可能很少听说降频 548 | 549 | it's actually super useful. 550 | 但降频其实很有用 551 | 552 | Sometimes it's not necessary to run the processor at full speed... 553 | 有时没必要让处理器全速运行 554 | 555 | maybe the user has stepped away, or just not running a particularly demanding program. 556 | 可能用户走开了,或者在跑一个性能要求较低的程序 557 | 558 | By slowing the CPU down, you can save a lot of power, 559 | 把 CPU 的速度降下来,可以省很多电 560 | 561 | which is important for computers that run on batteries, like laptops and smartphones. 562 | 省电对用电池的设备很重要,比如笔记本和手机 563 | 564 | To meet these needs, 565 | 为了尽可能省电 566 | 567 | many modern processors can increase or decrease their clock speed based on demand, 568 | 很多现代处理器可以按需求 加快或减慢时钟速度 569 | 570 | which is called dynamic frequency scaling. 571 | 这叫 "动态调整频率" 572 | 573 | So, with the addition of a clock, our CPU is complete. 574 | 加上时钟后,CPU 才是完整的. 575 | 576 | We can now put a box around it, and make it its own component. 577 | 现在可以放到盒子里,变成一个独立组件 578 | 579 | Yup. 580 | 对 581 | 582 | A new level of abstraction! 583 | 一层新的抽象! 584 | 585 | RAM, as I showed you last episode, 586 | RAM,上集说过,是在 CPU 外面的独立组件 587 | 588 | lies outside the CPU as its own component, 589 | RAM,上集说过,是在 CPU 外面的独立组件 590 | 591 | and they communicate with each other using address, data and enable wires. 592 | CPU 和 RAM 之间 \N 用 "地址线" "数据线" 和 "允许读/写线" 进行通信 593 | 594 | Although the CPU we designed today is a simplified example, 595 | 虽然今天我们设计的 CPU 是简化版的, 596 | 597 | many of the basic mechanics we discussed are still found in modern processors. 598 | 但我们提到的很多机制,依然存在于现代处理器里 599 | 600 | Next episode, we're going to beef up our CPU, 601 | 下一集,我们要加强 CPU,给它扩展更多指令. 602 | 603 | extending it with more instructions as we take our first baby steps into software. 604 | 同时开始讲软件. 605 | 606 | I'll see you next week. 607 | 下周见 608 | 609 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/08. 指令和程序-Instructions & Programs.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I’m Carrie Anne and this is Crash Course Computer Science! 2 | (。・∀・)ノ゙嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | Last episode, we combined an ALU, control unit, some memory, and a clock together to 5 | 上集我们把 ALU, 控制单元, RAM, 时钟 结合在一起 6 | 7 | make a basic, but functional Central Processing Unit – or CPU 8 | 做了个基本,但可用的"中央处理单元", 简称 CPU 9 | 10 | – the beating, ticking heart of a computer. 11 | 它是计算机的核心 12 | 13 | We’ve done all the hard work of building many of these components from the electronic 14 | 我们已经用电路做了很多组件. 15 | 16 | circuits up, and now it’s time to give our CPU some actual instructions to process! 17 | 这次我们给 CPU 一些指令来运行! 18 | 19 | The thing that makes a CPU powerful is the fact that it is programmable – 20 | CPU 之所以强大,是因为它是可编程的 - 21 | 22 | if you write a different sequence of instructions, then the CPU will perform a different task. 23 | 如果写入不同指令,就会执行不同任务 24 | 25 | So the CPU is a piece of hardware which is controlled by easy-to-modify software! 26 | CPU 是一块硬件,可以被软件控制! 27 | 28 | Let’s quickly revisit the simple program that we stepped through last episode. 29 | 我们重新看一下上集的简单程序 30 | 31 | The computer memory looked like this. 32 | 内存里有这些值 33 | 34 | Each address contained 8 bits of data. 35 | 每个地址可以存 8 位数据 36 | 37 | For our hypothetical CPU, the first four bits specified the operation code, or opcode, and 38 | 因为我们的 CPU 是假设的,这里前4位是"操作码" 39 | 40 | the second set of four bits specified an address or registers. 41 | 后4位指定一个内存地址,或寄存器. 42 | 43 | In memory address zero we have 0010 1110. 44 | 内存地址 0 是 0010 1110 45 | 46 | Again, those first four bits are our opcode which corresponds to a "LOAD_A" instruction. 47 | 前 4 位代表 LOAD_A 指令 48 | 49 | This instruction reads data from a location of memory specified in those last four bits 50 | 意思是:把后 4 位指定的内存地址的值,放入寄存器 A 51 | 52 | of the instruction and saves it into Register A. In this case, 1110, or 14 in decimal. 53 | 后 4 位是 1110,十进制的 14 54 | 55 | So let’s not think of this of memory address 0 as "0010 1110", but rather as the instruction 56 | 我们来把 0010 1110 看成 "LOAD_A 14" 指令 57 | 58 | "LOAD_A 14". 59 | 我们来把 0010 1110 看成 "LOAD_A 14" 指令 60 | 61 | That’s much easier to read and understand! 62 | 这样更好理解! 63 | 64 | And for me to say! 65 | 也更方便说清楚 66 | 67 | And we can do the same thing for the rest of the data in memory. 68 | 可以对内存里剩下的数也这样转换. 69 | 70 | In this case, our program is just four instructions long, 71 | 这里,我们的程序只有4个指令 72 | 73 | and we’ve put some numbers into memory too, 3 and 14. 74 | 还有数字 3 和 14 75 | 76 | So now let’s step through this program: 77 | 现在一步步看 78 | 79 | First is LOAD_A 14, which takes the value in address 14, which is the number 3, 80 | "LOAD_A 14" 是从地址 14 中拿到数字3,放入寄存器A 81 | 82 | and stores it into Register A. 83 | "LOAD_A 14" 是从地址 14 中拿到数字3,放入寄存器A 84 | 85 | Then we have a "LOAD_B 15" instruction, which takes the value in memory location 15, 86 | "LOAD_B 15" 是从地址 15 中拿到数字14,放入寄存器B 87 | 88 | which is the number 14, and saves it into Register B. 89 | "LOAD_B 15" 是从地址 15 中拿到数字14,放入寄存器B 90 | 91 | Okay. 92 | 好. 93 | 94 | Easy enough. 95 | 挺简单的! 96 | 97 | But now we have an "ADD" instruction. 98 | 下一个是 ADD 指令 99 | 100 | This tells the processor to use the ALU to add two registers together, 101 | "ADD B A" 告诉 ALU \N 把寄存器 B 和寄存器 A 里的数字加起来 102 | 103 | in this case, B and A are specified. 104 | "ADD B A" 告诉 ALU \N 把寄存器 B 和寄存器 A 里的数字加起来 105 | 106 | The ordering is important, because the resulting sum is saved into the second register that’s specified. 107 | (B和A的)顺序很重要,因为结果会存在第二个寄存器 108 | 109 | So in this case, the resulting sum is saved into Register A. 110 | 也就是寄存器 A 111 | 112 | And finally, our last instruction is "STORE_A 13", which instructs the CPU to write whatever 113 | 最后一条指令是 "STORE_A 13" \N 把寄存器 A 的值存入内存地址 13 114 | 115 | value is in Register A into memory location 13. 116 | 最后一条指令是 "STORE_A 13" \N 把寄存器 A 的值存入内存地址 13 117 | 118 | Yesss! 119 | 好棒! 120 | 121 | Our program adds two numbers together. 122 | 我们把 2 个数加在了一起! 123 | 124 | That’s about as exciting as it gets when we only have four instructions to play with. 125 | 毕竟只有4个指令,也只能做这个了. 126 | 127 | So let’s add some more! 128 | 加多一些指令吧! 129 | 130 | Now we’ve got a subtract function, which like ADD, specifies two registers to operate on. 131 | SUB 是减法,和 ADD 一样也要 2 个寄存器来操作. 132 | 133 | We’ve also got a fancy new instruction called JUMP. 134 | 还有 JUMP(跳转) 135 | 136 | As the name implies, this causes the program to "jump" to a new location. 137 | 让程序跳转到新位置 138 | 139 | This is useful if we want to change the order of instructions, or choose to skip some instructions. 140 | 如果想改变指令顺序,或跳过一些指令,这个很实用 141 | 142 | For example, a JUMP 0, would cause the program to go back to the beginning. 143 | 举例, JUMP 0 可以跳回开头 144 | 145 | At a low level, this is done by writing the value specified in the last four bits into 146 | JUMP 在底层的实现方式是 \N 把指令后 4 位代表的内存地址的值 147 | 148 | the instruction address register, overwriting the current value. 149 | 覆盖掉 "指令地址寄存器" 里的值 150 | 151 | We’ve also added a special version of JUMP called JUMP_NEGATIVE. 152 | 还有一个特别版的 JUMP 叫 JUMP_NEGATIVE 153 | 154 | "This only jumps the program if the ALU’s negative flag is set to true. 155 | 它只在 ALU 的 "负数标志" 为真时,进行 JUMP 156 | 157 | As we talked about in Episode 5, the negative flag is only set 158 | 第5集讲过,算术结果为负,"负数标志"才是真 159 | 160 | when the result of an arithmetic operation is negative. 161 | 第5集讲过,算术结果为负,"负数标志"才是真 162 | 163 | If the result of the arithmetic was zero or positive, the negative flag would not be set. 164 | 结果不是负数时, "负数标志"为假 165 | 166 | So the JUMP NEGATIVE won’t jump anywhere, and the CPU will just continue on to the next instruction. 167 | 如果是假,JUMP_NEGATIVE 就不会执行 \N 程序照常进行 168 | 169 | Our previous program really should have looked like this to be correct, 170 | 我们之前的例子程序,其实应该是这样,才能正确工作. 171 | 172 | otherwise the CPU would have just continued on after the STORE instruction, processing all those 0’s. 173 | 否则跑完 STORE_A 13 之后,\N CPU 会不停运行下去,处理后面的 0 174 | 175 | But there is no instruction with an opcode of 0, and so the computer would have crashed! 176 | 因为 0 不是操作码,所以电脑会崩掉! 177 | 178 | It’s important to point out here that we’re storing 179 | 我还想指出一点,指令和数据都是存在同一个内存里的. 180 | 181 | both instructions and data in the same memory. 182 | 我还想指出一点,指令和数据都是存在同一个内存里的. 183 | 184 | There is no difference fundamentally -- it’s all just binary numbers. 185 | 它们在根本层面上毫无区别 - 都是二进制数 186 | 187 | So the HALT instruction is really important because it allows us to separate the two. 188 | HALT 很重要,能区分指令和数据 189 | 190 | Okay, so let’s make our program a bit more interesting, by adding a JUMP. 191 | 好,现在用 JUMP 让程序更有趣一些. 192 | 193 | We’ll also modify our two starting values in memory to 1 and 1. 194 | 我们还把内存中 3 和 14 两个数字,改成 1 和 1 195 | 196 | Lets step through this program just as our CPU would. 197 | 现在来从 CPU 的视角走一遍程序 198 | 199 | First, LOAD_A 14 loads the value 1 into Register A. 200 | 首先 LOAD_A 14,把 1 存入寄存器A \N(因为地址 14 里的值是 1) 201 | 202 | Next, LOAD_B 15 loads the value 1 into Register B. 203 | 然后 LOAD_B 15,把 1 存入寄存器B\N(因为地址 15 里的值也是 1) 204 | 205 | As before, we ADD registers B and A together, with the sum going into Register A. 1+1 = 2, 206 | 然后 ADD B A 把寄存器 B 和 A 相加 \N 结果放到寄存器 A 里 207 | 208 | so now Register A has the value 2 in it (stored in binary of course) 209 | 现在寄存器 A 的值是 2 \N (当然是以二进制存的) 210 | 211 | Then the STORE instruction saves that into memory location 13. 212 | 然后 STORE_A 13 指令,把寄存器 A 的值存入内存地址 13 213 | 214 | Now we hit a "JUMP 2" instruction. 215 | 现在遇到 JUMP 2 指令 216 | 217 | This causes the processor to overwrite the value in the instruction address register, 218 | CPU 会把"指令地址寄存器"的值,现在是 4,改成 2 219 | 220 | which is currently 4, with the new value, 2. 221 | CPU 会把"指令地址寄存器"的值,现在是 4,改成 2 222 | 223 | Now, on the processor’s next fetch cycle, we don’t fetch HALT, 224 | 因此下一步不再是 HALT 225 | 226 | instead we fetch the instruction at memory location 2, which is ADD B A. 227 | 而是读内存地址 2 里的指令,也就是 ADD B A 228 | 229 | We’ve jumped! 230 | 我们跳转了! 231 | 232 | Register A contains the value 2, and register B contains the value 1. 233 | 寄存器 A 里是 2,寄存器 B 里是 1 234 | 235 | So 1+2 = 3, so now Register A has the value 3. 236 | 1+2=3,寄存器 A 变成 3 237 | 238 | We store that into memory. 239 | 存入内存 240 | 241 | And we’ve hit the JUMP again, back to ADD B A. 242 | 又碰到 JUMP 2,又回到 ADD B A. 243 | 244 | 1+3=4 245 | 1+3=4 246 | 247 | So now register A has the value 4. 248 | 现在寄存器 A 是 4 249 | 250 | See what's happening here? 251 | 发现了吗? 252 | 253 | Every loop, we’re adding one. 254 | 每次循环都+1 255 | 256 | Its counting up! 257 | 不断增多 258 | 259 | Cooooool. 260 | 酷 261 | 262 | But notice there’s no way to ever escape. 263 | 但没法结束啊 264 | 265 | We’re never.. ever.. going to get to that halt instruction, 266 | 永远不会碰到 HALT 267 | 268 | because we’re always going to hit that JUMP. 269 | 总是会碰到 JUMP 270 | 271 | This is called an infinite loop – a program that runs forever… ever… ever… ever… 272 | 这叫无限循环 - 这个程序会永远跑下去.. 下去.. 下去.. 下去 273 | 274 | ever 275 | 下去 276 | 277 | To break the loop, we need a conditional jump. 278 | 为了停下来,我们需要有条件的 JUMP 279 | 280 | A jump that only happens if a certain condition is met. 281 | 只有特定条件满足了,才执行 JUMP. 282 | 283 | Our JUMP_NEGATIVE is one example of a conditional jump, 284 | 比如 JUMP NEGATIVE 就是条件跳转的一个例子 285 | 286 | but computers have other types too - like JUMP IF EQUAL and JUMP IF GREATER. 287 | 还有其他类型的条件跳转,比如\N JUMP IF EQUAL(如果相等)\N JUMP IF GREATER(如果更大) 288 | 289 | So let’s make our code a little fancier and step through it. 290 | 现在把代码弄花哨一点,再过一遍代码 291 | 292 | Like before, the program starts by loading values from memory into registers A and B. 293 | 就像之前,程序先把内存值放入寄存器 A 和 B. 294 | 295 | In this example, the number 11 gets loaded into Register A, and 5 gets loaded into Register B. 296 | 寄存器 A 是 11,寄存器 B 是 5 297 | 298 | Now we subtract register B from register A. That’s 11 minus 5, which is 6, 299 | SUB B A,用 A 减 B,11-5=6 300 | 301 | and so 6 gets saved into Register A. 302 | 6 存入寄存器 A 303 | 304 | Now we hit our JUMP NEGATIVE. 305 | JUMP NEGATIVE 出场 306 | 307 | The last ALU result was 6. 308 | 上一次 ALU 运算的结果是 6 309 | 310 | That’s a positive number, so the the negative flag is false. 311 | 是正数,所以 "负数标志" 是假 312 | 313 | That means the processor does not jump. 314 | 因此处理器不会执行 JUMP 315 | 316 | So we continue on to the next instruction... 317 | 继续下一条指令 318 | 319 | ...which is a JUMP 2. 320 | JUMP 2 321 | 322 | No conditional on this one, so we jump to instruction 2 no matter what. 323 | JUMP 2 没有条件,直接执行! 324 | 325 | Ok, so we’re back at our SUBTRACT Register B from Register A. 6 minus 5 equals 1. 326 | 又回到寄存器 A-B,6-5=1 327 | 328 | So 1 gets saved into register A. 329 | A 变成 1 330 | 331 | Next instruction. 332 | 下一条指令 333 | 334 | We’re back again at our JUMP NEGATIVE. 335 | 又是 JUMP NEGATIVE 336 | 337 | 1 is also a positive number, so the CPU continues on to the JUMP 2, looping back around again 338 | 因为 1 还是正数,因此 JUMP NEGATIVE 不会执行 \N 来到下一条指令,JUMP 2 339 | 340 | to the SUBTRACT instruction. 341 | 又来减一次 342 | 343 | This time is different though. 344 | 这次就不一样了 345 | 346 | 1 minus 5 is negative 4. 347 | 1-5=-4 348 | 349 | And so the ALU sets its negative flag to true for the first time. 350 | 这次ALU的 "负数标志" 是真 351 | 352 | Now, when we advance to the next instruction, 353 | 现在下一条指令 354 | 355 | JUMP_NEGATIVE 5, the CPU executes the jump to memory location 5. 356 | JUMP NEGATIVE 5, CPU 的执行跳到内存地址 5 357 | 358 | We’re out of the infinite loop! 359 | 跳出了无限循环! 360 | 361 | Now we have a ADD B to A. Negative 4 plus 5, is positive 1, and we save that into Register A. 362 | 现在的指令是 ADD B A,-4+5=1,1 存入寄存器 A 363 | 364 | Next we have a STORE instruction that saves Register A into memory address 13. 365 | 下一条指令 STORE_A 13,把 A 的值存入内存地址 13 366 | 367 | Lastly, we hit our HALT instruction and the computer rests. 368 | 最后碰到 HALT 指令,停下来. 369 | 370 | So even though this program is only 7 instructions long, the CPU ended up executing 13 instructions, 371 | 虽然程序只有 7 个指令,但 CPU 执行了 13 个指令, 372 | 373 | and that's because it looped twice internally. 374 | 因为在内部循环了 2 次. 375 | 376 | This code calculated the remainder if we divide 5 into 11, which is one. 377 | 这些代码其实是算余数的,11除5余1 378 | 379 | With a few extra lines of code, we could also keep track of how many loops we did, the count 380 | 如果加多几行指令,我们还可以跟踪循环了多少次 381 | 382 | of which would be how many times 5 went into 11… we did two loops, so that means 5 goes 383 | 11除5,循环2次 384 | 385 | into 11 two times... with a remainder of 1. 386 | 余1 387 | 388 | And of course this code could work for any two numbers, which we can just change in memory 389 | 当然,我们可以用任意2个数,7和81,18和54,什么都行 390 | 391 | to whatever we want: 7 and 81, 18 and 54, it doesn’t matter 392 | 当然,我们可以用任意2个数,7和81,18和54,什么都行 393 | 394 | -- that’s the power of software! 395 | 这就是软件的强大之处 396 | 397 | Software also allowed us to do something our hardware could not. 398 | 软件还让我们做到硬件做不到的事 399 | 400 | Remember, our ALU didn’t have the functionality to divide two numbers, 401 | ALU 可没有除法功能 402 | 403 | instead it’s the program we made that gave us that functionality. 404 | 是程序给了我们这个功能. 405 | 406 | And then other programs can use our divide program to do even fancier things. 407 | 别的程序也可以用我们的除法程序,来做其他事情 408 | 409 | And you know what that means. 410 | 这意味着 一层新抽象! 411 | 412 | New levels of abstraction! 413 | 这意味着 一层新抽象! 414 | 415 | So, our hypothetical CPU is very basic – all of its instructions are 8 bits long, 416 | 我们这里假设的 CPU 很基础,所有指令都是 8 位, 417 | 418 | with the opcode occupying only the first four bits. 419 | 操作码只占了前面 4 位 420 | 421 | So even if we used every combination of 4 bits, our CPU would only be able to support 422 | 即便用尽 4 位,也只能代表 16 个指令 423 | 424 | a maximum of 16 different instructions. 425 | 即便用尽 4 位,也只能代表 16 个指令 426 | 427 | On top of that, several of our instructions used the last 4 bits to specify a memory location. 428 | 而且我们有几条指令,是用后 4 位来指定内存地址 429 | 430 | But again, 4 bits can only encode 16 different values, 431 | 因为 4 位最多只能表示 16 个值, 432 | 433 | meaning we can address a maximum of 16 memory locations - that’s not a lot to work with. 434 | 所以我们只能操作 16 个地址,这可不多. 435 | 436 | For example, we couldn’t even JUMP to location 17, 437 | 我们甚至不能 JUMP 17 438 | 439 | because we literally can’t fit the number 17 into 4 bits. 440 | 因为 4 位二进制无法表示数字 17 441 | 442 | For this reason, real, modern CPUs use two strategies. 443 | 因此,真正的现代 CPU 用两种策略 444 | 445 | The most straightforward approach is just to have bigger instructions, with more bits, 446 | 最直接的方法是用更多位来代表指令,比如 32 位或 64 位 447 | 448 | like 32 or 64 bits. 449 | 最直接的方法是用更多位来代表指令,比如 32 位或 64 位 450 | 451 | This is called the instruction length. 452 | 这叫 指令长度 453 | 454 | Unsurprisingly. 455 | 毫不意外 456 | 457 | The second approach is to use variable length instructions. 458 | 第二个策略是 "可变指令长度" 459 | 460 | For example, imagine a CPU that uses 8 bit opcodes. 461 | 举个例子,比如某个 CPU 用 8 位长度的操作码 462 | 463 | When the CPU sees an instruction that needs no extra values, like the HALT instruction, 464 | 如果看到 HALT 指令,HALT 不需要额外数据 465 | 466 | it can just execute it immediately. 467 | 那么会马上执行. 468 | 469 | However, if it sees something like a JUMP instruction, it knows it must also fetch 470 | 如果看到 JUMP,它得知道位置值 471 | 472 | the address to jump to, which is saved immediately behind the JUMP instruction in memory. 473 | 这个值在 JUMP 的后面 474 | 475 | This is called, logically enough, an Immediate Value. 476 | 这叫 "立即值" 477 | 478 | In such processor designs, instructions can be any number of bytes long, 479 | 这样设计,指令可以是任意长度 480 | 481 | which makes the fetch cycle of the CPU a tad more complicated. 482 | 但会让读取阶段复杂一点点 483 | 484 | Now, our example CPU and instruction set is hypothetical, 485 | 要说明的是,我们拿来举例的 CPU 和指令集都是假设的, 486 | 487 | designed to illustrate key working principles. 488 | 是为了展示核心原理 489 | 490 | So I want to leave you with a real CPU example. 491 | 所以我们来看个真的 CPU 例子. 492 | 493 | In 1971, Intel released the 4004 processor. 494 | 1971年,英特尔发布了 4004 处理器. 495 | 496 | It was the first CPU put all into a single chip 497 | 这是第一次把 CPU 做成一个芯片 \N 给后来的英特尔处理器打下了基础 498 | 499 | and paved the path to the intel processors we know and love today. 500 | 这是第一次把 CPU 做成一个芯片 \N 给后来的英特尔处理器打下了基础 501 | 502 | It supported 46 instructions, shown here. 503 | 它支持 46 个指令 504 | 505 | Which was enough to build an entire working computer. 506 | 足够做一台能用的电脑 507 | 508 | And it used many of the instructions we’ve talked about like JUMP ADD SUBTRACT and LOAD. 509 | 它用了很多我们说过的指令,比如 JUMP ADD SUB LOAD 510 | 511 | It also uses 8-bit immediate values, like we just talked about, for things like JUMPs, 512 | 它也用 8 位的"立即值"来执行 JUMP, 以表示更多内存地址. 513 | 514 | in order to address more memory. 515 | 它也用 8 位的"立即值"来执行 JUMP, 以表示更多内存地址. 516 | 517 | And processors have come a long way since 1971. 518 | 处理器从 1971 年到现在发展巨大. 519 | 520 | A modern computer processor, like an Intel Core i7, has thousands of different instructions 521 | 现代 CPU, 比如英特尔酷睿 i7, 有上千个指令和指令变种 522 | 523 | and instruction variants, ranging from one to fifteen bytes long. 524 | 长度从1到15个字节. 525 | 526 | For example, there’s over a dozens different opcodes just for variants of ADD! 527 | 举例,光 ADD 指令就有很多变种! 528 | 529 | And this huge growth in instruction set size is due in large part to extra bells and whistles 530 | 指令越来越多,是因为给 CPU 设计了越来越多功能 531 | 532 | that have been added to processor designs overtime, which we’ll talk about next episode. 533 | 下集我们会讲 534 | 535 | See you next week! 536 | 下周见 537 | 538 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/10. 早期的编程方式-Early Programming.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne and welcome to Crash Course Computer Science. 2 | (。・∀・)ノ゙嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | Over the last few episodes, 5 | 前几集我们把重点放在计算机的原理 6 | 7 | We've talked a lot about the mechanics of how computers work. 8 | 前几集我们把重点放在计算机的原理 9 | 10 | How they use complex circuits to save and retrieve values from memory, 11 | 怎么从内存读写数据,执行操作 12 | 13 | and perform operations on those values 14 | 怎么从内存读写数据,执行操作 15 | 16 | like adding two numbers together. 17 | 比如把两个数字加在一起 18 | 19 | We've even briefly talked about sequences of operations, 20 | 还简单讲了下指令的执行,也就是计算机程序 21 | 22 | which is a computer program 23 | 还简单讲了下指令的执行,也就是计算机程序 24 | 25 | What we haven't talked about is how a program gets into a computer? 26 | 但我们还没讲的是:程序如何"进入"计算机 27 | 28 | You might remember in episode 7 and 8 , 29 | 你应该记得在第 7, 8 集,我们一步步讲了例子程序 30 | 31 | we step through some simple example programs for the CPU that we had created 32 | 你应该记得在第 7, 8 集,我们一步步讲了例子程序 33 | 34 | For simplicity, we just waved our hands and said that the program was already magically in memory 35 | 当时为了简单,我们假设程序已经魔法般在内存里了 36 | 37 | But in reality, programs have to be loaded into a computer's memory. 38 | 但事实是,程序需要加载进内存 39 | 40 | It's not magic. It's computer science 41 | 这不是魔法,是计算机科学! 42 | 43 | The need to program machines existed way before the development of computers. 44 | 给机器编程这个需求,早在计算机出现之前就有了 45 | 46 | The most famous example of this was in textile manufacturing 47 | 最著名的例子来自纺织业 48 | 49 | If you just wanted to weave a big red tablecloth 50 | 如果你只想织一块红色大桌布 51 | 52 | You could simply feed red thread into a loom and let it run 53 | 可以直接放红线进织布机 54 | 55 | What about if you wanted the cloth to have a pattern like stripes or plaid? 56 | 但如果想要图案怎么办? 比如条纹或者方格 57 | 58 | Workers would have to periodically reconfigure the loom as dictated by the pattern, 59 | 工人要每隔一会儿 调整一次织布机 60 | 61 | but this was labor intensive which made patterned fabrics expensive. 62 | 因为非常消耗劳动力,所以图案纺织品很贵 63 | 64 | The presence or absence of a hole in the card determined if a specific thread was held high or low in the loom 65 | 特定位置有没有穿孔,决定了线是高是低 66 | 67 | Such as the cross thread, called the weft, passed above or below the thread 68 | 横线是从上/从下穿过 69 | 70 | To vary the pattern across rows these punch cards were arranged in long chains 71 | 为了让每行图案不同,纸卡连成长条 72 | 73 | Forming a sequence of commands for the loom. 74 | 形成连续指令 75 | 76 | Sound familiar? 77 | 听起来很熟? 78 | 79 | Many consider Jacquard loom to be one of the earliest forms of programming. 80 | 很多人认为雅卡尔织布机是最早的编程 81 | 82 | Punched cards, turned out to be a cheap, reliable, fairly human-readable way to store data. 83 | 事实证明 穿孔纸卡便宜、可靠、也易懂 84 | 85 | Nearly a century later, 86 | 近一个世纪后 87 | 88 | punch cards were used to help tabulate the 1890 US census 89 | 穿孔纸卡用于 1890 年美国人口普查 90 | 91 | which we talked about in episode 1 92 | 我们在第一集提过 93 | 94 | Each card held an individual person's data. 95 | 一张卡存一个人的信息 96 | 97 | things like race 98 | 比如种族 99 | 100 | marital status 101 | 婚姻状况 102 | 103 | number of children 104 | 子女数量 105 | 106 | country of birth and so on 107 | 出生国家 等等 108 | 109 | for each demographic question 110 | 针对每个问题,人口普查工作者会在对应位置打孔 111 | 112 | a census worker would punch out a hole of the appropriate position 113 | 针对每个问题,人口普查工作者会在对应位置打孔 114 | 115 | when a card was fed into the tabulating machine 116 | 当卡片插入汇总机 117 | 118 | a hole would cause the running total for that specific answer to be increased by one 119 | 孔会让对应总和值+1 120 | 121 | in this way you could afeed the entire counties worth of people 122 | 可以插入整个国家人口的卡片 123 | 124 | and at the end you'd have running totals for all of the questions that you ask 125 | 在结束后得到各个总值 126 | 127 | It is important to note here that early tabulating machines were not truly computers 128 | 值得注意的是,早期汇总机不算计算机 129 | 130 | as they can only do one thing-tabulate 131 | 因为它们只做一件事 - 汇总数据 132 | 133 | their operation was fixed and not programmable 134 | 操作是固定的,不能编程 135 | 136 | punched cards stored data, but not a program 137 | 穿孔纸卡存的是数据,不是程序. 138 | 139 | over the next 60 years, these business machines grew in capability 140 | 之后60年,这些机器被加强,可以做减、乘、除 141 | 142 | Adding features to subtract multiply divide 143 | 之后60年,这些机器被加强,可以做减、乘、除 144 | 145 | and even make simple decisions about when to perform certain operations. 146 | 甚至可以做一些小决定,决定何时执行某指令 147 | 148 | To trigger these functions appropriately 149 | 为了正确执行不同计算,程序员需要某种控制面板 150 | 151 | so that different calculations could be performed, a programmer accessed a control panel 152 | 为了正确执行不同计算,程序员需要某种控制面板 153 | 154 | this panel was full of little sockets into which a programmer would plug cables 155 | 面板有很多小插孔,程序员可以插电线 156 | 157 | to pass values and signals between different parts of the machine 158 | 让机器的不同部分 互相传数据和信号 159 | 160 | for this reason they were also called plug boards 161 | 因此也叫 "插线板" 162 | 163 | Unfortunately this meant having to rewire the machine each time a different program needed to be run 164 | 不幸的是, 这意味着 运行不同程序要重新接线 165 | 166 | And so by the 1920s these plug boards were made swappable 167 | 所以到 1920 年代,控制面板变成了可拔插 168 | 169 | This not only made programming a lot more comfortable 170 | 让编程更方便 171 | 172 | but also allowed for different programs be plugged into a machine 173 | 可以给机器插入不同程序 174 | 175 | For example one board might be wired to calculate sales tax 176 | 比如,一个插线板算销售税,另一个算工资单 177 | 178 | While another helps with payroll 179 | 比如,一个插线板算销售税,另一个算工资单 180 | 181 | But plug boards were fiendishly complicated to program 182 | 但给插线板编程很复杂 183 | 184 | This tangle of wires is a program for calculating a profit loss summary using an IBM 402 accounting machine 185 | 图中乱成一团的线 负责算盈亏总额 \N 用于 IBM 402 核算机 186 | 187 | which were popular in the 1940s 188 | 在 1940 年代这样做很流行 189 | 190 | And this style of plug board programming wasn't unique through electromechanical computers 191 | 用插线板编程 不只在机电计算机流行 192 | 193 | The world's first general-purpose electronic computer, the ENIAC, completed in 1946 194 | 世上第一台通用电子计算机,ENIAC,完成于 1946 年 195 | 196 | used a ton of them 197 | 用了一大堆插线板 198 | 199 | Even after a program had been completely figured out on paper 200 | 程序在纸上设计好之后 201 | 202 | Physically wiring up the ENIAC and getting the program to run could take upwards of three weeks 203 | 给 ENIAC 连线,最多可能花三个星期 204 | 205 | Given the enormous cost of these early computers, weeks of downtime simply to switch programs was unacceptable 206 | 因为早期计算机非常昂贵 \N 停机几个星期只为换程序 完全无法接受 207 | 208 | and the new faster more flexible way to program machines was badly needed 209 | 人们急需更快、更灵活的新方式来编程 210 | 211 | Fortunately by the late 1940s and into the 50s 212 | 幸运的是,到 1940 年代晚期 1950 年代初 213 | 214 | electronic memory was becoming feasible 215 | 内存变得可行 216 | 217 | As costs fell, memory size grew, instead of storing a program as a physical plug board of wires 218 | 价格下降, 容量上升. 与其把程序存在插线板 219 | 220 | it became possible to store a program entirely in a computer's memory 221 | 存在内存变得可行 222 | 223 | where it could be easily changed by programmers and quickly accessed by the CPU 224 | 这样程序易于修改、方便 CPU 快速读取 225 | 226 | these machines were called Stored-program Computers 227 | 这类机器叫 "存储程序计算机" 228 | 229 | With enough computer memory you could store not only the program you wanted to run 230 | 如果内存足够,不仅可以存要运行的程序 231 | 232 | but also any data your program would need 233 | 还可以存程序需要的数据 234 | 235 | including new values it created along the way 236 | 包括程序运行时产生的新数据 237 | 238 | Unifying the program and data into a single shared memory is called the Von Neumann Architecture 239 | 程序和数据都存在一个地方,叫 "冯诺依曼结构" 240 | 241 | named after John Von Neumann 242 | 命名自 约翰·冯·诺依曼 243 | 244 | a prominent mathematician and physicist who worked on the Manhattan project and several early electronic computers 245 | 杰出的数学家和物理学家 \N 参与了曼哈顿计划和早期电子计算机项目 246 | 247 | and once said I am thinking about something much more important than Bombs 248 | 他曾说:我在思考比炸弹重要得多的东西 249 | 250 | I'm thinking about computers 251 | 计算机 252 | 253 | The hallmarks of a Von Neumann computer are a processing unit containing an arithmetic logic unit 254 | 冯诺依曼计算机的标志是,一个处理器(有算术逻辑单元)+ 255 | 256 | data registers and instruction register and instruction address register 257 | 数据寄存器+指令寄存器+指令地址寄存器 258 | 259 | And finally a memory to store both data and instructions 260 | +内存(负责存数据和指令) 261 | 262 | Hopefully this sounds familiar 263 | 希望这听起来很耳熟 264 | 265 | Because we actually built a Von Neumann computer in episode 7 266 | 因为第7集我们造了一个冯诺依曼计算机 267 | 268 | The very first Von Neumann Architecture Stored-program computer 269 | 第一台冯诺依曼架构的"储存程序计算机" 270 | 271 | was constructed in 1948 by the University of Manchester, nicknamed Baby. 272 | 由曼彻斯特大学于 1948 年建造完成,绰号"宝宝" 273 | 274 | and even the computer you are watching this video right now 275 | 甚至你现在看视频的计算机,也在用一样的架构 276 | 277 | uses the same architecture 278 | 甚至你现在看视频的计算机,也在用一样的架构 279 | 280 | Now electronic computer memory is great and all 281 | 虽然有内存很棒 282 | 283 | but you still have to load the program and data into the computer before it can run 284 | 但程序和数据 依然需要某种方式输入计算机 285 | 286 | and for this reason punch cards were used 287 | 所以用穿孔纸卡 288 | 289 | Let's get to the Thought bubbles 290 | 让我们进入 思维泡泡 291 | 292 | Well into the 1980s almost all computers have a punch card reader 293 | 到1980年代,几乎所有的计算机都有穿孔纸卡读取器 294 | 295 | which could suck in a single punch card at a time 296 | 可以吸入一张卡片,把卡片内容写进内存 297 | 298 | and write the contents of the card into the computer's memory 299 | 可以吸入一张卡片,把卡片内容写进内存 300 | 301 | If you load it in a stack of punch cards, 302 | 如果放了一叠卡片,读取器会一个个写进内存 303 | 304 | the reader would load them all into memory sequentially as a big block 305 | 如果放了一叠卡片,读取器会一个个写进内存 306 | 307 | once the program and data were in memory, the computer would be told to execute it 308 | 一旦程序和数据写入完毕,电脑会开始执行 309 | 310 | Of course even simple computer programs might have hundreds of instructions 311 | 即便简单程序也有几百条指令,要用一叠纸卡来存 312 | 313 | which meant that programs were stored as stacks of punch cards 314 | 即便简单程序也有几百条指令,要用一叠纸卡来存 315 | 316 | So if you ever have the misfortune of accidentally dropping your program on the floor 317 | 如果不小心摔倒弄撒了 318 | 319 | it could take you hours days or even weeks to put the code back in the right order 320 | 要花上几小时、几天、甚至几周来整理 321 | 322 | A common trick was to draw a diagonal line on the side of the card stack called striping, 323 | 有个小技巧是 在卡片侧面画对角线 324 | 325 | so you'd have at least some clue how to get it back into the right order 326 | 如果弄散了,整理起来会方便很多 327 | 328 | The largest program ever punched into punch cards was the US Air Force's SAGE air defense system, completed in 1955. 329 | 用纸卡的最大型程序 \N 是美国空军的 SAGE 防空系统,于 1955 年完成 330 | 331 | and its peak, the project is said to have employed 20% of the world's programmers 332 | 据称顶峰时期 雇佣了世上 20% 程序员 333 | 334 | Its main control program was stored on a whopping 62,500 punch cards 335 | 主控制程序用了 62500 张穿孔纸卡 336 | 337 | which is equivalent to roughly 5 megabytes of data 338 | 等同于大约 5MB 的数据 339 | 340 | Pretty underwhelming by today's standards 341 | 以如今的标准,不值一提 342 | 343 | And punch cards weren't only useful for getting data into computers 344 | 穿孔纸卡不仅可以往计算机放数据 345 | 346 | but also getting data out of them 347 | 还可以取出数据 348 | 349 | At the end of a program results could be written out of computer memory and onto punch cards by, well, punching cards 350 | 程序运行到最后,结果可以输到纸卡上,方式嘛,当然是打孔 351 | 352 | then this data could be analyzed by humans or loaded into a second program for additional computation 353 | 然后人可以分析结果,或者再次放进计算机,做进一步计算 354 | 355 | Thanks, thought-bubble 356 | 谢了 思维泡泡 357 | 358 | A close cousin to punch cards was punched paper tape 359 | 穿孔纸卡 的亲戚是纸带 360 | 361 | Which is basically the same idea, but continuous instead of being on individual cards 362 | 基本是一回事,只不过更连续,不是一张张卡. 363 | 364 | And of course we haven't talked about Hard Drives, CD-ROMs, DVDs, USB-Thumb drives and other similar goodies 365 | 当然我们还没提硬盘, 只读光盘, DVD, U盘等等 366 | 367 | We'll get to those more advanced types of data storage in a future episode 368 | 以后我们会讲这些更先进的存储方法 369 | 370 | Finally in addition to plug boards and punch paper 371 | 最后,除了插线板和穿孔纸卡 372 | 373 | there was another common way to program and control computers in pre-1980 374 | 在 1980 年代前,还有一种常见编程方式 375 | 376 | Panel programming 377 | 面板编程 378 | 379 | Rather than having to physically plug in cables to activate certain functions 380 | 与其插一堆线到插线板 381 | 382 | this could also be done with huge panels full of switches and buttons 383 | 可以用一大堆开关和按钮,做到一样的效果 384 | 385 | And there were indicator lights to display the status of various functions and values in memory 386 | 面板上有指示灯,代表各种函数的状态和内存中的值 387 | 388 | Computers of the 50s and 60s often featured huge control consoles that look like this 389 | 50和60年代的计算机,一般都有这样巨大的控制台 390 | 391 | Although it was rare to input a whole program using just switches,it was possible 392 | 很少有人只用开关来输入一整个程序,但技术上是可行的 393 | 394 | And early home computers made for the hobbyist market use switches extensively 395 | 早期针对计算机爱好者的家用计算机,大量使用了开关 396 | 397 | because most home users couldn't afford expensive peripherals like punch card readers 398 | 因为大多数家庭用户负担不起昂贵的外围设备 \N 比如穿孔纸卡读取器 399 | 400 | The first commercially successful home computer was the Altair 8800 401 | 第一款取得商业成功的家用计算机是 Altair 8800 402 | 403 | which sold in two versions: Pre-assembled and the Kit 404 | 有两种版本可以买: \N 1. 预先装好的整机 \N 2. 需要组装的组件 405 | 406 | the Kit which was popular with amateur computing enthusiasts, 407 | 计算机爱好者 喜欢买组件版 408 | 409 | sold for the then unprecedented low price are around $400 in 1975 410 | 售价极低,在 1975 年卖 400 美元左右 411 | 412 | Or about $2,000 in 2017 413 | 相当于 2017 年的 2000 美元 414 | 415 | To program the 8800, you'd literally toggle the switches on the front panel 416 | 为了给 8800 编程,你要拨动面板上的开关 417 | 418 | to enter the binary op-codes for the instruction you wanted 419 | 输入二进制操作码 420 | 421 | Then you press the deposit button to write that value into memory 422 | 然后按 "存储键" 把值存入内存 423 | 424 | Then in the next location in memory you toggle the switches again 425 | 然后会到下一个内存位置 \N 你可以再次拨开关,写下一个指令 426 | 427 | for your next instruction deposit it and so on 428 | 重复这样做 429 | 430 | When you finally entered your whole program into memory 431 | 把整个程序都写入内存之后 432 | 433 | you would toggle the switches moves back to memory address 0 434 | 可以推动开关,回到内存地址0 435 | 436 | press the run button and watch the little lights blink 437 | 然后按运行按钮,灯会闪烁 438 | 439 | That was home computing in 1975, Wow. 440 | 这就是 1975 年的家用计算机, 哇. 441 | 442 | Whether it was plug board, switches or punched paper 443 | 不管是插线板、开关或穿孔纸卡 444 | 445 | Programming these early computers was the realm of experts 446 | 早期编程都是专家活 447 | 448 | either professionals who did this for living or technology enthusiasts 449 | 不管是全职还是技术控,都要非常了解底层硬件 450 | 451 | you needed intimate knowledge of the underlying hardware, 452 | 不管是全职还是技术控,都要非常了解底层硬件 453 | 454 | so things like processor op-codes and register wits, to write programs 455 | 比如 操作码, 寄存器等, 才能写程序 456 | 457 | This meant programming was hard and tedious and even professional engineers 458 | 所以编程很难,很烦 459 | 460 | and scientists struggled to take full advantage of what computing could offer 461 | 哪怕工程师和科学家都无法 完全发挥计算机的能力 462 | 463 | What was needed was a simpler way to tell computers what to do, 464 | 我们需要一种更简单方式 告诉计算机要做什么 465 | 466 | a simpler way to write programs 467 | 一种更简单的编程方式 468 | 469 | And that brings us to programming languages, which we'll talk about next episode 470 | 这带领我们到下一个话题 - 编程语言, 我们下集会讲 471 | 472 | See you next week 473 | 下周见 474 | 475 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/14. 数据结构-Data Structures.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne, and welcome to Crash Course Computer Science! 2 | (。・∀・)ノ゙嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | Last episode, we discussed a few example classic algorithms, 5 | 上集讲了一些经典算法 6 | 7 | like sorting a list of numbers and finding the shortest path in a graph. 8 | 比如给数组排序,找图的最短路径 9 | 10 | What we didn't talk much about, 11 | 而上集没讲的是 12 | 13 | is how the data the algorithms ran on was stored in computer memory. 14 | 算法处理的数据 存在内存里的格式是什么 15 | 16 | You don't want your data to be like John Green's college dorm room, 17 | 你肯定不想数据像 John Green 的大学宿舍一样乱 \N 到处都是食物,衣服和纸 18 | 19 | with food, clothing and papers strewn everywhere. 20 | 你肯定不想数据像 John Green 的大学宿舍一样乱 \N 到处都是食物,衣服和纸 21 | 22 | Instead, we want our data to be structured, 23 | 我们希望数据是结构化的,方便读取 24 | 25 | so that it's organized, allowing things to be easily retrieved and read. 26 | 我们希望数据是结构化的,方便读取 27 | 28 | For this, computer scientists use Data Structures! 29 | 因此计算机科学家发明了 "数据结构"! 30 | 31 | We already introduced one basic data structure last episode, 32 | 上集已经介绍了一种基本数据结构: 33 | 34 | Arrays, also called lists or Vectors in some languages. 35 | 数组(Array)\N 也叫列表(list)或向量(Vector)(在其它编程语言里) 36 | 37 | These are a series of values stored in memory. 38 | 数组的值一个个连续存在内存里 39 | 40 | So instead of just a single value being saved into a variable, like 'j equals 5', 41 | 所以不像之前,一个变量里只存一个值(比如 j = 5) 42 | 43 | we can define a whole series of numbers, and save that into an array variable. 44 | 我们可以把多个值存在数组变量里 45 | 46 | To be able to find a particular value in this array, we have to specify an index. 47 | 为了拿出数组中某个值,我们要指定一个下标(index) 48 | 49 | Almost all programing languages start arrays at index 0, 50 | 大多数编程语言里,数组下标都从 0 开始 51 | 52 | and use a square bracket syntax to denote array access. 53 | 用方括号 [ ] 代表访问数组 54 | 55 | So, for example, if we want to add the values in the first and third spots of our array 'j', 56 | 如果想相加数组 J 的第一个和第三个元素 57 | 58 | and save that into a variable 'a', we would write a line of code like this. 59 | 把结果存在变量 a,可以写上图这样一行代码 60 | 61 | How an array is stored in memory is pretty straightforward. 62 | 数组存在内存里的方式 十分易懂 63 | 64 | For simplicity, let's say that the compiler chose to store ours at memory location 1,000. 65 | 为了简单,假设编译器从内存地址 1000 开始存数组 66 | 67 | The array contains 7 numbers, and these are stored one after another in memory, as seen here. 68 | 数组有7个数字,像上图一样按顺序存. 69 | 70 | So when we write "j index of 0", the computer goes to memory location 1,000, 71 | 写 j[0],会去内存地址 1000 72 | 73 | with an offset of 0, and we get the value 5. 74 | 加 0 个偏移,得到地址 1000,拿值:5 75 | 76 | If we wanted to retrieve "j index of 5", our program goes to memory location 1000, 77 | 如果写 j[5],会去内存地址 1000 78 | 79 | plus an offset of 5, which in this case, holds a value of 4. 80 | 加 5 个偏移,得到地址 1005,拿值: 4 81 | 82 | It's easy to confuse the fifth number in the array with the number at index 5. 83 | 很容易混淆 "数组中第 5 个数" 和 "数组下标为 5 的数" 84 | 85 | They are not the same. 86 | 它们不是一回事 87 | 88 | Remember, the number at index 5 is the 6th number in the array 89 | 记住,下标 5 其实是数组中第 6 个数 90 | 91 | because the first number is at index 0. 92 | 因为下标是从 0 开始算的 93 | 94 | Arrays are extremely versatile data structures, used all the time, 95 | 数组的用途广泛 96 | 97 | and so there are many functions that can handle them to do useful things. 98 | 所以几乎所有编程语言 都自带了很多函数来处理数组 99 | 100 | For example, pretty much every programming language comes with a built-in sort function, 101 | 举例,数组排序函数很常见 102 | 103 | where you just pass in your array, and it comes back sorted. 104 | 只需要传入数组,就会返回排序后的数组 105 | 106 | So there's no need to write that algorithm from scratch. 107 | 不需要写排序算法 108 | 109 | Very closely related are Strings, which are just arrays of characters, 110 | 数组的亲戚是 字符串 (string) 111 | 112 | like letters, numbers, punctuation and other written symbols. 113 | 其实就是字母,数字,标点符号等 组成的数组 114 | 115 | We talked about how computers store characters way back in Episode 4. 116 | 第 4 集讨论过计算机怎么存储字符 117 | 118 | Most often, to save a string into memory, you just put it in quotes, like so. 119 | 写代码时 用引号括起来就行了 \N j = "STAN ROCKS" 120 | 121 | Although it doesn't look like an array, it is. 122 | 虽然长的不像数组,但的确是数组 123 | 124 | Behind the scenes, the memory looks like this. 125 | 幕后看起来像这样 126 | 127 | Note that the string ends with a zero in memory. 128 | 注意,字符串在内存里以 0 结尾 129 | 130 | It's not the character zero, but the binary value 0. 131 | 不是"字符0",是"二进制值0" \N 这叫字符"null",表示字符串结尾 132 | 133 | This is called the null character, and denotes the end of the string in memory. 134 | 不是"字符0",是"二进制值0" \N 这叫字符"null",表示字符串结尾 135 | 136 | This is important because if I call a function like "print quote", 137 | 这个字符非常重要,如果调用 print 函数 138 | 139 | which writes the string to the screen, 140 | print 在屏幕上输出字符串 141 | 142 | it prints out each character in turn starting at the first memory location, 143 | 会从开始位置,逐个显示到屏幕 144 | 145 | but it needs to know when to stop! 146 | 但得知道什么时候停下来! 147 | 148 | Otherwise, it would print out every single thing in memory as text. 149 | 否则会把内存里所有东西 都显示出来 150 | 151 | The zero tells string functions when to stop. 152 | 0 告诉函数何时停下 153 | 154 | Because computers work with text so often, 155 | 因为计算机经常处理字符串,所以有很多函数专门处理字符串 156 | 157 | there are many functions that specifically handle strings. 158 | 因为计算机经常处理字符串,所以有很多函数专门处理字符串 159 | 160 | For example, many programming languages have a string concatenation function, or "strcat", 161 | 比如连接字符串的 strcat 162 | 163 | which takes in two strings, and copies the second one to the end of the first. 164 | strcat 接收两个字符串,把第二个放到第一个结尾. 165 | 166 | We can use arrays for making one dimensional lists, 167 | 我们可以用数组做一维列表 168 | 169 | but sometimes you want to manipulate data that is two dimensional, 170 | 但有时想操作二维数据 171 | 172 | like a grid of numbers in a spreadsheet, or the pixels on your computer screen. 173 | 比如电子表格,或屏幕上的像素 174 | 175 | For this, we need a Matrix. 176 | 那么需要 矩阵(Matrix) 177 | 178 | You can think of a Matrix as an array of arrays! 179 | 可以把矩阵看成 数组的数组! 180 | 181 | So a 3 by 3 matrix is really an array of size 3, with each index storing an array of size 3. 182 | 一个 3x3 矩阵就是一个长度为3的数组 \N 数组里每个元素都是一个长度为3的数组 183 | 184 | We can initialize a matrix like so. 185 | 可以这样初始化. 186 | 187 | In memory, this is packed together in order like this. 188 | 内存里是这样排列的 189 | 190 | To access a value, you need to specify two indexes, like "J index of 2, then index of 1" - 191 | 为了拿一个值,需要两个下标,比如 j[2][1] 192 | 193 | this tells the computer you're looking for the item in subarray 2 at position 1. 194 | 告诉计算机在找数组 2 里,位置是 1 的元素 195 | 196 | And this would give us the value 12. 197 | 得到数字 12 198 | 199 | The cool thing about matrices is we're not limited to 3 by 3 200 | 矩阵酷的地方是,不止能做 3x3 的矩阵 201 | 202 | -- we can make them any size we want 203 | 任何维度都行 204 | 205 | -- and we can also make them any number of dimensions we want. 206 | 任何维度都行 207 | 208 | For example, we can create a five dimensional matrix and access it like this. 209 | 可以做一个5维矩阵,然后这样访问 \N a = j[2][0][18][18][3] 210 | 211 | That's right, you now know how to access a five dimensional matrix 212 | 现在你知道了 怎么读一个 5 维矩阵 213 | 214 | -- tell your friends! 215 | 快去告诉你的朋友! 216 | 217 | So far, we've been storing individual numbers or letters into our arrays or matrices. 218 | 目前我们只存过单个数字/字符,存进数组或矩阵 219 | 220 | But often it's useful to store a block of related variables together. 221 | 但有时, 把几个有关系的变量存在一起, 会很有用 222 | 223 | Like, you might want to store a bank account number along with its balance. 224 | 比如银行账户号和余额 225 | 226 | Groups of variables like these can be bundled together into a Struct. 227 | 多个变量打包在一起叫 结构体 (Struct) 228 | 229 | Now we can create variables that aren't just single numbers, 230 | 现在多个不同类型数据,可以放在一起 231 | 232 | but are compound data structures, able to store several pieces of data at once. 233 | 现在多个不同类型数据,可以放在一起 234 | 235 | We can even make arrays of structs that we define, 236 | 甚至可以做一个数组,里面放很多结构体 237 | 238 | which are automatically bundled together in memory. 239 | 这些数据在内存里 会自动打包在一起 240 | 241 | If we access, for example, J index of 0, we get back the whole struct stored there, 242 | 如果写 j[0],能拿到 j[0] 里的结构体 243 | 244 | and we can pull the specific account number and balance data we want. 245 | 然后拿银行账户和余额 246 | 247 | This array of structs, like any other array, 248 | 存结构体的数组,和其它数组一样 249 | 250 | gets created at a fixed size that can't be enlarged to add more items. 251 | 创建时就有固定大小,不能动态增加大小 252 | 253 | Also, arrays must be stored in order in memory, 254 | 还有,数组在内存中 按顺序存储 255 | 256 | making it hard to add a new item to the middle. 257 | 在中间插入一个值很困难 258 | 259 | But, the struct data structure can be used for 260 | 但结构体可以创造更复杂的数据结构,消除这些限制 261 | 262 | building more complicated data structures that avoid these restrictions. 263 | 但结构体可以创造更复杂的数据结构,消除这些限制 264 | 265 | Let's take a look at this struct that's called a "node". 266 | 我们来看一个结构体,叫 节点(node) 267 | 268 | It stores a variable, like a number, and also a pointer. 269 | 它存一个变量 \N 一个指针(pointer) 270 | 271 | A pointer is a special variable that points, hence the name, to a location in memory. 272 | "指针" 是一种特殊变量,指向一个内存地址,因此得名. 273 | 274 | Using this struct, we can create a linked list, 275 | 用 节点 可以做 链表(linked list) 276 | 277 | which is a flexible data structure that can store many nodes. 278 | 链表是一种灵活数据结构,能存很多个 节点 (node) 279 | 280 | It does this by having each node point to the next node in the list. 281 | 灵活性是通过每个节点 指向 下一个节点实现的 282 | 283 | Let's imagine we have three node structs saved in memory, at locations 1000, 1002 and 1008. 284 | 假设有三个节点,在内存地址 1000,1002, 1008 285 | 286 | They might be spaced apart because they were created at different times, 287 | 隔开的原因 可能是创建时间不同 288 | 289 | and other data can sit between them. 290 | 它们之间有其他数据 291 | 292 | So, you see that the first node contains the value 7, and the location 1008 in its "next" pointer. 293 | 可以看到第一个节点,值是 7,指向地址 1008 294 | 295 | This means that the next node in the linked list is located at memory location 1008. 296 | 代表下一个节点,位于内存地址 1008 297 | 298 | Looking down the linked list, to the next node, 299 | 现在来到下一个节点 300 | 301 | we see it stores the value 112 and points to another node at location 1002. 302 | 值是 112,指向地址 1002 303 | 304 | If we follow that, we find a node that contains the value 14 305 | 如果跟着它,会看到一个值为 14 的节点 306 | 307 | and points back to the first node at location 1000. 308 | 这个节点 指回地址 1000,也就是第一个节点 309 | 310 | So this linked list happened to be circular, 311 | 这叫 循环链表 312 | 313 | but it could also have been terminated by using a next pointer value of 0 314 | 但链表也可以是非循环的,最后一个指针是 0 315 | 316 | -- the null value -- which would indicate we've reached the end of the list. 317 | "null",代表链表尽头 318 | 319 | When programmers use linked lists, 320 | 当程序员用链表时 321 | 322 | they rarely look at the memory values stored in the next pointers. 323 | 很少看指针具体指向哪里 324 | 325 | Instead, they can use an abstraction of a linked list, that looks like this, 326 | 而是用链表的抽象模型,就像上图 327 | 328 | which is much easier to conceptualize. 329 | 更容易看懂 330 | 331 | Unlike an array, whose size has to be pre-defined, 332 | 数组大小需要预先定好 333 | 334 | linked lists can be dynamically extended or shortened. 335 | 链表大小可以动态增减 336 | 337 | For example, we can allocate a new node in memory, 338 | 可以创建一个新节点,通过改变指针值,把新节点插入链表 339 | 340 | and insert it into this list, just by changing the next pointers. 341 | 可以创建一个新节点,通过改变指针值,把新节点插入链表 342 | 343 | Linked Lists can also easily be re-ordered, trimmed, split, reversed, and so on. 344 | 链表也很容易重新排序,两端缩减,分割,倒序等 345 | 346 | Which is pretty nifty! 347 | 超方便! 348 | 349 | And pretty useful for algorithms like sorting, which we talked about last week. 350 | 链表也适合上集的排序算法 351 | 352 | Owing to this flexibility, many more-complex data structures are built on top of linked lists 353 | 因为灵活,很多复杂数据结构 都用链表 354 | 355 | The most famous and universal are queues and stacks. 356 | 最出名的是 队列(queue)和 栈(stack) 357 | 358 | A queue - like the line at your post office - goes in order of arrival. 359 | "队列" 就像邮局排队,谁先来就排前面 360 | 361 | The person who has been waiting the longest, gets served first. 362 | "队列" 就像邮局排队,谁先来就排前面 363 | 364 | No matter how frustrating it is that all you want to do is buy stamps 365 | 虽然你可能只想买邮票,而前面的人要寄 23 个包裹 366 | 367 | and the person in front of you seems to be mailing 23 packages. 368 | 虽然你可能只想买邮票,而前面的人要寄 23 个包裹 369 | 370 | But, regardless, this behavior is called First-In First-Out, or FIFO. 371 | 这叫 先进先出(FIFO) 372 | 373 | That's the first part. 374 | 我指队列,不是指那 23 个包裹 375 | 376 | Not the 23 packages thing. 377 | 我指队列,不是指那 23 个包裹 378 | 379 | Imagine we have a pointer, named "post office queue", that points to the first node in our linked list. 380 | 想象有个指针叫"邮局队列",指向链表第一个节点 381 | 382 | Once we're done serving Hank, we can read Hank's next pointer, 383 | 第一个节点是 Hank,服务完 Hank 之后 \N 读取 Hank 的指针 384 | 385 | and update our "post office queue" pointer to the next person in the line. 386 | 把"邮局队列"指向下一个人 387 | 388 | We've successfully dequeued Hank -- he's gone, done, finished. 389 | 这样就把 Hank "出队"(dequeue)了 390 | 391 | If we want to enqueue someone, that is, add them to the line, 392 | 如果我们想把某人"入队"(enqueue) \N 意思是加到队列里 393 | 394 | we have to traverse down the linked list until we hit the end, 395 | 要遍历整个链表到结尾 396 | 397 | and then change that next pointer to point to the new person. 398 | 然后把结尾的指针,指向新人(Nick) 399 | 400 | With just a small change, we can use linked lists as stacks, which are LIFO… 401 | 只要稍作修改,就能用链表做 栈,\N 栈是后进先出(LIFO) 402 | 403 | Last-In First-Out. 404 | 只要稍作修改,就能用链表做 栈,\N 栈是后进先出(LIFO) 405 | 406 | You can think of this like a stack of pancakes... 407 | 可以把"栈"想成一堆松饼 408 | 409 | as you make them, you add them to the top of stack. 410 | 做好一个新松饼,就堆在之前上面 411 | 412 | And when you want to eat one, you take them from the top of the stack. 413 | 吃的时候,是从最上面开始 414 | 415 | Delicious! 416 | 美味! 417 | 418 | Instead of enqueueing and dequeuing, 419 | 栈就不叫"入队""出队"了 420 | 421 | data is pushed onto the stack and popped from the stacks. 422 | 叫"入栈"(push) "出栈"(pop) 423 | 424 | Yep, those are the official terms! 425 | 对,这些是正确术语! 426 | 427 | If we update our node struct to contain not just one, but two pointers, 428 | 如果节点改一下,改成 2 个指针 429 | 430 | we can build trees, 431 | 就能做 树(tree) 432 | 433 | another data structure that's used in many algorithms. 434 | 很多算法用了 "树" 这种数据结构 435 | 436 | Again, programmers rarely look at the values of these pointers, 437 | 同样,程序员很少看指针的具体值 438 | 439 | and instead conceptualize trees like this: The top most node is called the root. 440 | 而是把"树"抽象成这样:最高的节点叫"根节点"(root) 441 | 442 | And any nodes that hang from other nodes are called children nodes. 443 | "根节点"下的所有节点 都叫"子节点"(children) 444 | 445 | As you might expect, nodes above children are called parent nodes. 446 | 任何子节点的直属上层节点,叫"母节点"(parent node) 447 | 448 | Does this example imply that Thomas Jefferson is the parent of Aaron Burr? 449 | 这个例子能说明 托马斯·杰斐逊 是 阿龙·伯尔 的父亲吗? 450 | 451 | I'll leave that to your fanfiction to decide. 452 | 我让你们的同人文来决定 453 | 454 | And finally, any nodes that have no children 455 | 没有任何"子节点"的节点 456 | 457 | -- where the tree ends -- are called Leaf Nodes. 458 | 也就是"树"结束的地方,叫"叶节点"(leaf) 459 | 460 | In our example, nodes can have up to two children, 461 | 在这里的例子中,节点最多只可以有 2 个子节点 462 | 463 | and for that reason, this particular data structure is called a binary tree. 464 | 因此叫 二叉树(binary tree) 465 | 466 | But you could just as easily have trees with three, four or any number of children 467 | 但你可以随便改,弄成 3个,4个,或更多 468 | 469 | by modifying the data structure accordingly. 470 | 但你可以随便改,弄成 3个,4个,或更多 471 | 472 | You can even have tree nodes that use linked lists to store all the nodes they point to. 473 | 甚至节点 可以用链表存所有子节点 474 | 475 | An important property of trees - both in real life and in data structures - is that 476 | "树"的一个重要性质是(不管现实中还是数据结构中) 477 | 478 | there's a one-way path from roots to leaves. 479 | "根"到"叶"是 单向 的 480 | 481 | It'd be weird if roots connected to leaves, that connected to roots. 482 | 如果根连到叶,叶连到根 就很奇怪 483 | 484 | For data that links arbitrarily, that include things like loops, 485 | 如果数据随意连接,包括循环 486 | 487 | we can use a graph data structure instead. 488 | 可以用"图"表示 489 | 490 | Remember our graph from last episode of cities connected by roads? 491 | 还记得上集用路连接城市的"图"吗? 492 | 493 | This can be stored as nodes with many pointers, very much like a tree, 494 | 这种结构 可以用有多个指针的节点表示 495 | 496 | but there is no notion of roots and leaves, and children and parents… 497 | 因此没有 根 叶 子节点 父节点 这些概念 498 | 499 | Anything can point to anything! 500 | 可以随意指向! 501 | 502 | So that's a whirlwind overview 503 | 以上概述了计算机科学中,最主要的一些数据结构 504 | 505 | of pretty much all of the fundamental data structures used in computer science. 506 | 以上概述了计算机科学中,最主要的一些数据结构 507 | 508 | On top of these basic building blocks, 509 | 这些基本结构之上,程序员做了各种新变体,有不同性质. 510 | 511 | programmers have built all sorts of clever variants, with slightly different properties 512 | 这些基本结构之上,程序员做了各种新变体,有不同性质. 513 | 514 | -- data structures like red-black trees and heaps, which we don't have time to cover. 515 | 比如"红黑树"和"堆",我们没时间讲 516 | 517 | These different data structures have properties that are useful for particular computations. 518 | 不同数据结构适用于不同场景 519 | 520 | The right choice of data structure can make your job a lot easier, 521 | 选择正确数据结构会让工作更简单 522 | 523 | so it pays off to think about how you want to structure your data before you jump in. 524 | 所以花时间考虑用什么数据结构是值得的 525 | 526 | Fortunately, most programming languages come with 527 | 幸运的是,大多数编程语言自带了预先做好的数据结构 528 | 529 | libraries packed full of ready-made data structures. 530 | 幸运的是,大多数编程语言自带了预先做好的数据结构 531 | 532 | For example, C++ has its Standard Template Library, and Java has the Java Class Library. 533 | 比如,C++有"标准模板库",Java有"Java 类库" 534 | 535 | These mean programmers don't have to waste time implementing things from scratch, 536 | 程序员不用浪费时间从零写 537 | 538 | and can instead wield the power of data structures to do more interesting things, 539 | 时间可以花在更有趣的事情 540 | 541 | once again allowing us to operate at a new level of abstraction! 542 | 又提升了一层抽象! 543 | 544 | I'll see you next week. 545 | 下周见! 546 | 547 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/16. 软件工程-Software Engineering.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne, and welcome to CrashCourse Computer Science! 2 | (。・∀・)ノ゙嗨,我是 Carrie Anne,欢迎收看计算机科学速成课! 3 | 4 | So we've talked a lot about sorting in this series 5 | 之前花了很多时间讲排序 6 | 7 | and often code to sort a list of numbers might only be ten lines long, 8 | 也写了些 10 行左右的排序代码 9 | 10 | which is easy enough for a single programmer to write. 11 | 对1个程序员来说很容易写 12 | 13 | Plus, it's short enough that you don't need any special tools 14 | 而且代码很短,不必用专门工具 15 | 16 | - you could do it in Notepad. 17 | - 记事本就够了 18 | 19 | Really! 20 | 真的! 21 | 22 | But, a sorting algorithm isn't a program; 23 | 但排序算法很少会是独立程序 \N 更可能是大项目的一小部分 24 | 25 | it's likely only a small part of a much larger program. 26 | 但排序算法很少会是独立程序 \N 更可能是大项目的一小部分 27 | 28 | For example, Microsoft Office has roughly 40 millions lines of code. 29 | 举个例子,微软的 Office 大约有 4000 万代码 30 | 31 | 40 MILLION! 32 | 4000 万! 33 | 34 | That's way too big for any one person to figure out and write! 35 | 太多了,一个人不可能做到 36 | 37 | To build huge programs like this, programmers use a set of tools and practices. 38 | 为了写大型程序,程序员用各种工具和方法 39 | 40 | Taken together, these form the discipline of Software Engineering 41 | 所有这些形成了"软件工程"学科 42 | 43 | - a term coined by engineer Margaret Hamilton, 44 | - 这个词由工程师 Margaret Hamilton 创造 45 | 46 | who helped NASA prevent serious problems during the Apollo missions to the moon. 47 | 她帮助 NASA 在阿波罗计划中避免了严重问题 48 | 49 | She once explained it this way: 50 | 她曾说过: 51 | 52 | "It's kind of like a root canal: you waited till the end, 53 | "有点像牙根管治疗:你总是拖到最后才做 \N 但有些事可以预先做好 54 | 55 | [but] there are things you could have done beforehand. 56 | "有点像牙根管治疗:你总是拖到最后才做 \N 但有些事可以预先做好 57 | 58 | It's like preventative healthcare, 59 | 有点像预防性体检, 只不过是预防软件出错" 60 | 61 | but it's preventative software." 62 | 有点像预防性体检, 只不过是预防软件出错" 63 | 64 | As I mentioned in episode 12, 65 | 第 12 集提过 66 | 67 | breaking big programs into smaller functions allows many people to work simultaneously. 68 | 把大项目分解成小函数 可以让多人同时工作 69 | 70 | They don't have to worry about the whole thing, 71 | 不用关心整个项目,关心自己的函数就好了 72 | 73 | just the function they're working on. 74 | 不用关心整个项目,关心自己的函数就好了 75 | 76 | So, if you're tasked with writing a sort algorithm, 77 | 如果你的任务是写排序算法 78 | 79 | you only need to make sure it sorts properly and efficiently. 80 | 你只需要确保高效和正确就可以了 81 | 82 | However, even packing code up into functions isn't enough. 83 | 然而把代码打包成函数 依然不够 84 | 85 | Microsoft Office probably contains hundreds of thousands of them. 86 | 如果只是这样,微软 Office 会有几十万个函数 87 | 88 | That's better than dealing with 40 million lines of code, 89 | 虽然比 4000 万行代码要好一些,但还是太多了 90 | 91 | but it's still way too many "things" for one person or team to manage. 92 | 虽然比 4000 万行代码要好一些,但还是太多了 93 | 94 | The solution is to package functions into hierarchies, 95 | 解决办法是:把函数打包成层级. \N 把相关代码都放在一起,打包成对象(objects) 96 | 97 | pulling related code together into "objects". 98 | 解决办法是:把函数打包成层级. \N 把相关代码都放在一起,打包成对象(objects) 99 | 100 | For example, car's software might have several functions related to cruise control, 101 | 例如,汽车软件中 可能有几个和定速巡航有关的函数 102 | 103 | like setting speed, nudging speed up or down, and stopping cruise control altogether. 104 | 比如 设定速度,逐渐加速减速,停止定速巡航 105 | 106 | Since they're all related, 107 | 因为这些函数都相关,可以包装成一个"定速巡航对象" 108 | 109 | we can wrap them up into a unified cruise control object. 110 | 因为这些函数都相关,可以包装成一个"定速巡航对象" 111 | 112 | But, we don't have to stop there, 113 | 但不止如此,我们还可以做更多 114 | 115 | cruise control is just one part of the engine's software. 116 | "定速巡航"只是引擎软件的一部分 117 | 118 | There might also be sets of functions that control spark plug ignition, 119 | 可能还有 "火花塞点火" "燃油泵" 和 "散热器" 120 | 121 | fuel pumps, and the radiator. 122 | 可能还有 "火花塞点火" "燃油泵" 和 "散热器" 123 | 124 | So we can create a "parent" Engine Object 125 | 我们可以做一个"引擎对象" 来包括所有"子"对象 126 | 127 | that contains all of these "children" objects. 128 | 我们可以做一个"引擎对象" 来包括所有"子"对象 129 | 130 | In addition to children *objects*, 131 | 除了子对象,"引擎对象"可能有自己的函数 132 | 133 | the engine itself might have its *own* functions. 134 | 除了子对象,"引擎对象"可能有自己的函数 135 | 136 | You want to be able to stop and start it, for example. 137 | 比如 开关引擎 138 | 139 | It'll also have its own variables, 140 | 它也会有自己的变量,比如汽车行驶了多少英里 141 | 142 | like how many miles the car has traveled. 143 | 它也会有自己的变量,比如汽车行驶了多少英里 144 | 145 | In general, objects can contain other objects, functions and variables. 146 | 总的来说,对象可以包其它对象,函数和变量 147 | 148 | And of course, the engine is just one part of a Car Object. 149 | 当然,"引擎对象"只是"汽车对象"的一部分 150 | 151 | There's also the transmission, wheels, doors, windows, and so on. 152 | 还有传动装置,车轮,门,窗等 153 | 154 | Now, as a programmer, if I want to set the cruise control, 155 | 作为程序员,如果想设"定速巡航" 156 | 157 | I navigate down the object hierarchy, 158 | 要一层层向下 159 | 160 | from the outermost objects to more and more deeply nested ones. 161 | 从最外面的对象往里找 162 | 163 | Eventually, I reach the function I want to trigger: 164 | 最后找到想执行的函数: 165 | 166 | "Car, then engine, then cruise control, then set cruise speed to 55". 167 | Car. Engine. CruiseControl. setCruiseSpeed(55) 168 | 169 | Programming languages often use something equivalent to the syntax shown here. 170 | 编程语言经常用类似这样的语法 171 | 172 | The idea of packing up functional units into nested objects is called 173 | 把函数打包成对象的思想叫 "面向对象编程" 174 | 175 | Object Oriented Programming. 176 | 把函数打包成对象的思想叫 "面向对象编程" 177 | 178 | This is very similar to what we've done all series long: 179 | 这种思想和之前类似 180 | 181 | hide complexity by encapsulating low-level details in higher-order components. 182 | 通过封装组件,隐藏复杂度 183 | 184 | Before we packed up things like transistor circuits into higher-level boolean gates. 185 | 之前把晶体管 打包成了逻辑门 186 | 187 | Now we're doing the same thing with software. 188 | 现在软件也这样做 189 | 190 | Yet again, it's a way to move up a new level of abstraction! 191 | 又提升了一层抽象! 192 | 193 | Breaking up a big program, like a car's software, 194 | 把大型软件(如汽车软件)\N 拆成一个个更小单元,适合团队合作 195 | 196 | into functional units is perfect for teams. 197 | 把大型软件(如汽车软件)\N 拆成一个个更小单元,适合团队合作 198 | 199 | One team might be responsible for the cruise control system, 200 | 一个团队负责定速巡航系统 201 | 202 | and a single programmer on that team tackles a handful of functions. 203 | 团队里的一位程序员负责其中一些函数 204 | 205 | This is similar to how big, physical things are built, like skyscrapers. 206 | 类似建摩天大楼 207 | 208 | You'll have electricians running wires, 209 | 有电工装电线 210 | 211 | plumbers fitting pipes, 212 | 管道工配管 213 | 214 | welders welding, 215 | 焊接工焊接 216 | 217 | painters painting, 218 | 油漆工涂油漆 219 | 220 | and hundreds of other people teeming all over the hull. 221 | 还有成百上千人做其他事情 222 | 223 | They work together on different parts simultaneously, 224 | 在不同岗位同时工作,各尽其能 225 | 226 | leveraging their different skills. 227 | 在不同岗位同时工作,各尽其能 228 | 229 | Until one day, you've got a whole working building! 230 | 直到整栋楼完成 231 | 232 | But, returning to our cruise control example 233 | 回到定速巡航的例子 234 | 235 | its code is going to have to make use of functions in other parts of the engine's software, 236 | 定速巡航 要用到引擎的其它函数,来保持车速 237 | 238 | to, you know, keep the car at a constant speed. 239 | 定速巡航 要用到引擎的其它函数,来保持车速 240 | 241 | That code isn't part of the cruise control team's responsibility. 242 | 定速巡航 团队不负责这些代码,另一个团队负责 243 | 244 | It's another team's code. 245 | 定速巡航 团队不负责这些代码,另一个团队负责 246 | 247 | Because the cruise control team didn't write that, 248 | 因为是其他团队的代码, 249 | 250 | they're going to need good documentation about what each function in the code does, 251 | 定速巡航 团队需要文档 帮助理解代码都做什么 252 | 253 | and a well-defined Application Programming Interface 254 | 以及定义好的 "程序编程接口" -简称 API 255 | 256 | -- or API for short. 257 | 以及定义好的 "程序编程接口" -简称 API 258 | 259 | You can think of an API as the way that 260 | API 帮助不同程序员合作 \N 不用知道具体细节,只要知道怎么使用就行了 261 | 262 | collaborating programmers interact across various parts of the code. 263 | API 帮助不同程序员合作 \N 不用知道具体细节,只要知道怎么使用就行了 264 | 265 | For example, in the IgnitionControl object, 266 | 例如"点火控制"对象中,可能有"设置发动机转数"函数 267 | 268 | there might be functions to set the RPM of the engine, 269 | 例如"点火控制"对象中,可能有"设置发动机转数"函数 270 | 271 | check the spark plug voltage, 272 | "检查火花塞电压"函数 273 | 274 | as well as fire the individual spark plugs. 275 | "点燃单个火花塞"函数 276 | 277 | Being able to set the motor's RPM is really useful, 278 | "设置发动机转速"非常有用 279 | 280 | the cruise control team is going to need to call that function. 281 | "定速巡航"团队要用到这个函数 282 | 283 | But, they don't know much about how the ignition system works. 284 | 但他们对点火系统不怎么了解 285 | 286 | It's not a good idea to let them call functions that fire the individual spark plugs. 287 | 让他们调用"点燃单个火花塞"函数,不是好主意 288 | 289 | Or the engine might explode! 290 | 引擎可能会炸! 291 | 292 | Maybe. 293 | 可能啦 294 | 295 | The API allows the right people access to the right functions and data. 296 | API 控制哪些函数和数据让外部访问 \N 哪些仅供内部 297 | 298 | Object Oriented Programming languages do this 299 | "面向对象"的编程语言 \N 可以指定函数是 public 或 private,来设置权限 300 | 301 | by letting you specify whether functions are public or private. 302 | "面向对象"的编程语言 \N 可以指定函数是 public 或 private,来设置权限 303 | 304 | If a function is marked as "private", 305 | 如果函数标记成 private 306 | 307 | it means only functions inside that object can call it. 308 | 意味着 只有同一个对象内的其他函数能调用它 309 | 310 | So, in this example, only other functions inside of IgnitionControl, 311 | 在这个例子里,只有内部函数比如 setRPM 312 | 313 | like the setRPM function, 314 | 在这个例子里,只有内部函数比如 setRPM 315 | 316 | can fire the sparkplugs. 317 | 才能调用 fireSparkplug 函数 318 | 319 | On the other hand, because the setRPM function is marked as public, 320 | 而 setRPM 函数是 public \N 所以其它对象可以调用它,比如 定速巡航 321 | 322 | other objects can call it, like cruise control. 323 | 而 setRPM 函数是 public \N 所以其它对象可以调用它,比如 定速巡航 324 | 325 | This ability to hide complexity, and selectively reveal it, 326 | "面向对象编程"的核心是 \N 隐藏复杂度,选择性的公布功能 327 | 328 | is the essence of Object Oriented Programming, 329 | "面向对象"的核心是 \N 隐藏复杂度,选择性的公布功能 330 | 331 | and it's a powerful and popular way to tackle building large and complex programs. 332 | 因为做大型项目很有效,所以广受欢迎 333 | 334 | Pretty much every piece of software on your computer, or game running on your console, 335 | 计算机上几乎所有软件,游戏机里几乎所有游戏 336 | 337 | was built using an Object Oriented Programming Language, 338 | 都是 "面向对象" 编程语言写的 339 | 340 | like C++, C# or Objective-C. 341 | 比如 C++, C#, Objective-C 等 342 | 343 | Other popular "OO" languages you may have heard of are Python and Java. 344 | 其他流行 OO 语言,你可能听过 Python 和 Java 345 | 346 | It's important to remember that code, before being compiled, is just text. 347 | 有一点很重要:代码在编译前就只是文字而已 348 | 349 | As I mentioned earlier, 350 | 前面提过,你可以用记事本或任何文字处理器 351 | 352 | you could write code in Notepad or any old word processor. 353 | 前面提过,你可以用记事本或任何文字处理器 354 | 355 | Some people do. 356 | 有人确实这样做 357 | 358 | But generally, today's software developers use special-purpose applications for writing programs, 359 | 但一般来说,现代软件开发者 会用专门的工具来写代码 360 | 361 | ones that integrate many useful tools for writing, organizing, compiling and testing code. 362 | 工具里集成了很多有用功能\N 帮助写代码,整理,编译和测代码 363 | 364 | Because they put everything you need in one place, 365 | 因为集成了所有东西 366 | 367 | they're called Integrated Development Environments , 368 | 因此叫 集成开发环境,简称 IDE 369 | 370 | or IDEs for short. 371 | 因此叫 集成开发环境,简称 IDE 372 | 373 | All IDEs provide a text editor for writing code, 374 | 所有 IDE 都有写代码的界面 375 | 376 | often with useful features like automatic color-coding to improve readability. 377 | 还带一些有用功能,比如代码高亮,来提高可读性 378 | 379 | Many even check for syntax errors as you type, like spell check for code. 380 | 许多 IDE 提供实时检查,比如拼写 381 | 382 | Big programs contain lots of individual source files, 383 | 大型项目有很多源代码文件 384 | 385 | so IDEs allow programmers to organize and efficiently navigate everything. 386 | IDE 帮助开发者整理和看代码 387 | 388 | Also built right into the IDE is the ability to compile and run code. 389 | 很多 IDE 还可以直接编译和运行代码 390 | 391 | And if your program crashes, 392 | 如果程序崩了,因为你还没写完呢 393 | 394 | because it's still a work in progress, 395 | 如果程序崩了,因为你还没写完呢 396 | 397 | the IDE can take you back to the line of code where it happened, 398 | IDE 可以定位到出错代码 399 | 400 | and often provide additional information to help you track down and fix the bug, 401 | 还会提供信息 帮你解决问题 402 | 403 | which is a process called debugging. 404 | 这叫 调试(debug) 405 | 406 | This is important 407 | 调试很重要 408 | 409 | because most programmers spend 70 to 80% of their time testing and debugging, 410 | 大多数程序员会花 70%~80% 时间调试,而不是在写代码 411 | 412 | not writing new code. 413 | 大多数程序员会花 70%~80% 时间调试,而不是在写代码 414 | 415 | Good tools, contained in IDEs, 416 | 好工具能极大帮助程序员防止和解决错误 417 | 418 | can go a long way when it comes to helping programmers prevent and find errors. 419 | 好工具能极大帮助程序员防止和解决错误 420 | 421 | Many computer programmers can be pretty loyal to their IDEs though 422 | 很多开发者只用一款 IDE 423 | 424 | But let's be honest. 425 | 但承认吧,VIM 才是最棒的编辑器 426 | 427 | VIM is where it's at. 428 | 但承认吧,VIM 才是最棒的编辑器 429 | 430 | Providing you know how to quit. 431 | 如果你知道怎么退出的话 432 | 433 | In addition to coding and debugging, 434 | 除了写代码和调试 435 | 436 | another important part of a programmer's job is documenting their code. 437 | 程序员工作的另一个重要部分是 给代码写文档 438 | 439 | This can be done in standalone files called "readme", 440 | 文档一般放在一个叫 README 的文件里 441 | 442 | which tell other programmers to read that help file before diving in. 443 | 告诉其他程序员,看代码前先看这个文件 444 | 445 | It can also happen right in the code itself with comment 446 | 文档也可以直接写成"注释",放在源代码里 447 | 448 | These are specially-marked statements that the program knows 449 | 注释是标记过的一段文字 450 | 451 | to ignore when the code is compiled. 452 | 编译代码时 注释会被忽略 453 | 454 | They exist only to help programmers figure out what's what in the source code. 455 | 注释存在的唯一作用 \N 就是帮助开发者理解代码 456 | 457 | Good documentation helps programmers when they revisit code they haven't seen for awhile, 458 | 好文档能帮助开发者 \N 几个月后理解自己的代码,对其他人也很重要 459 | 460 | but it's also crucial for programmers who are totally new to it. 461 | 好文档能帮助开发者 \N 几个月后理解自己的代码,对其他人也很重要 462 | 463 | I just want to take a second here and reiterate that it's THE WORST 464 | 我想花一秒 再强调一下注释很重要 465 | 466 | when someone parachutes a load of uncommented and undocumented code into your lap, 467 | 最糟糕的就是拿到一堆代码,没有任何注释和文档 468 | 469 | and you literally have to go line by line to understand what the code is doing. 470 | 结果得逐行读代码,理解到底干嘛的 471 | 472 | Seriously. 473 | 我是认真的 474 | 475 | Don't be that person. 476 | 别做那种人 477 | 478 | Documentation also promotes code reuse. 479 | 文档还可以提高复用性 480 | 481 | So, instead of having programmers constantly write the same things over and over, 482 | 与其让程序员一遍遍写同样的东西 483 | 484 | they can track down someone else's code that does what they need. 485 | 可以直接用别人写好的来解决问题 486 | 487 | Then, thanks to documentation, 488 | 读文档看怎么用就行,不用读代码 489 | 490 | they can put it to work in their program, without ever having to read through the code. 491 | 读文档看怎么用就行,不用读代码 492 | 493 | "Read the docs" as they say. 494 | "读文档啊" 495 | 496 | In addition to IDEs, 497 | 除了 IDE,还有一个重要软件帮助团队协作 498 | 499 | another important piece of software that 500 | 除了 IDE,还有一个重要软件帮助团队协作 501 | 502 | helps big teams work collaboratively on big coding projects is called 503 | 除了 IDE,还有一个重要软件帮助团队协作 504 | 505 | Source Control, 506 | 源代码管理 507 | 508 | also known as version control or revision control. 509 | 也叫"版本控制" 510 | 511 | Most often, at a big software company like Apple or Microsoft, 512 | 苹果或微软这样的大型软件公司 513 | 514 | code for projects is stored on centralized servers, 515 | 会把代码放到一个中心服务器上 516 | 517 | called a code repository . 518 | 叫"代码仓库" 519 | 520 | When a programmer wants to work on a piece of code, 521 | 程序员想改一段代码时 522 | 523 | they can check it out, 524 | 可以 check out 525 | 526 | sort of like checking out a book out from a library. 527 | 有点像从图书馆借书 528 | 529 | Often, this can be done right in an IDE. 530 | 一般这种操作,可以直接在 IDE 内完成 531 | 532 | Then, they can edit this code all they want on their personal computer, 533 | 然后开发者在自己的电脑上编辑代码 534 | 535 | adding new features and testing if they work. 536 | 加新功能,测试 537 | 538 | When the programmer is confident their changes are working and there are no loose ends, 539 | 如果代码没问题了,所有测试通过了 540 | 541 | they can check the code back into the repository, 542 | 可以把代码放回去 543 | 544 | known as committing code, for everyone else to use. 545 | 这叫 提交 (commit) 546 | 547 | While a piece of code is checked out, 548 | 当代码被 check out,而且可能被改过了 549 | 550 | and presumably getting updated or modified, 551 | 当代码被 check out,而且可能被改过了 552 | 553 | other programmers leave it alone. 554 | 其他开发者不会动这段代码 555 | 556 | This prevents weird conflicts and duplicated work. 557 | 防止代码冲突和重复劳动 558 | 559 | In this way, hundreds of programmers can be simultaneously checking in and out pieces of code, 560 | 这样多名程序员可以同时写代码,建立庞大的系统 561 | 562 | iteratively building up huge systems. 563 | 这样多名程序员可以同时写代码,建立庞大的系统 564 | 565 | Critically, you don't want someone committing buggy code, 566 | 重要的是,你不希望提交的代码里有问题 \N 因为其他人可能用到了这些代码 567 | 568 | because other people and teams may rely on it. 569 | 重要的是,你不希望提交的代码里有问题 \N 因为其他人可能用到了这些代码 570 | 571 | Their code could crash, creating confusion and lost time. 572 | 导致他们的代码崩溃,造成困惑而且浪费时间 573 | 574 | The master version of the code, stored on the server, 575 | 代码的主版本 (master) 576 | 577 | should always compile without errors and run with minimal bugs. 578 | 应该总是编译正常,尽可能少 bug 579 | 580 | But sometimes bugs creep in. 581 | 但有时 bug 还是会出现 582 | 583 | Fortunately, source control software keeps track of all changes, 584 | 幸运的是,源代码管理可以跟踪所有变化 585 | 586 | and if a bug is found, 587 | 如果发现 bug 588 | 589 | the whole code, or just a piece, 590 | 全部或部分代码,可以"回滚"到之前的稳定版 591 | 592 | can be rolled back to an earlier, stable version. 593 | 全部或部分代码,可以"回滚"到之前的稳定版 594 | 595 | It also keeps track of who made each change, 596 | "源代码管理" 也记录了谁改了什么代码 597 | 598 | so coworkers can send nasty, 599 | 所以同事可以给你发 讨厌的 600 | 601 | I mean, helpful 602 | 我是说"有帮助的" 邮件给写代码的人 603 | 604 | and encouraging emails to the offending person. 605 | 我是说"有帮助的" 邮件给写代码的人 606 | 607 | Debugging goes hand in hand with writing code, 608 | 写代码和测代码密不可分 609 | 610 | and it's most often done by an individual or small team. 611 | 测试一般由个人或小团队完成 612 | 613 | The big picture version of debugging is Quality Assurance testing, or QA. 614 | 测试可以统称 "质量保证测试",简称 QA 615 | 616 | This is where a team rigorously tests out a piece of software, 617 | 严格测试软件的方方面面 618 | 619 | attempting to create unforeseen conditions that might trip it up. 620 | 模拟各种可能情况,看软件会不会出错 621 | 622 | Basically, they elicit bugs. 623 | 基本上就是找 bug 624 | 625 | Getting all the wrinkles out is a huge effort, 626 | 解决大大小小的错误需要很多工作 627 | 628 | but vital in making sure the software works 629 | 但对确保软件质量至关重要 630 | 631 | as intended for as many users in as many situations as imaginable before it ships. 632 | 让软件在各种情况下按预期运行 633 | 634 | You've probably heard of beta software. 635 | 你可能听过 "beta 版" 软件 636 | 637 | This is a version of software that's mostly complete, 638 | 意思是软件接近完成 639 | 640 | but not 100% fully tested. 641 | 但不是 100% 完全测试过 642 | 643 | Companies will sometimes release beta versions to the public to help them identify issues, 644 | 公司有时会向公众发布 beta 版,以帮助发现问题 645 | 646 | it's essentially like getting a free QA team. 647 | 用户就像免费的 QA 团队 648 | 649 | What you don't hear about as much 650 | 你听过比较少的是 \N beta 版之前的版本:alpha 版本 651 | 652 | is the version that comes before the beta: the alpha version. 653 | 你听过比较少的是 \N beta 版之前的版本:alpha 版本 654 | 655 | This is usually so rough and buggy, it's only tested internally. 656 | alpha 版一般很粗糙,错误很多,\N 经常只在公司内部测试 657 | 658 | So, that's the tip of the iceberg in terms of the tools, tricks and techniques 659 | 以上只是软件工程师用的工具和技巧的冰山一角 660 | 661 | that allow software engineers to construct the huge pieces of software that we know and love today, 662 | 它们帮助软件工程师制作令人喜爱的软件 663 | 664 | like YouTube, Grand Theft Auto 5, and Powerpoint. 665 | 如 YouTube,GTA5 和 PPT 等等 666 | 667 | As you might expect, 668 | 如你所料 669 | 670 | all those millions of lines of code needs some serious processing power to run at useful speeds, 671 | 这些代码要强大的处理能力 才能高速速度运行 672 | 673 | so next episode we'll be talking about how computers got so incredibly fast. 674 | 所以下集讨论,计算机怎么发展到如今这么快 675 | 676 | See you then. 677 | 到时见 678 | 679 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/25. 个人计算机革命-The Personal Computer Revolution.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne, and welcome to CrashCourse Computer Science! 2 | (。・∀・)ノ゙嗨,我是 Carrie Anne \N 欢迎收看计算机科学速成课! 3 | 4 | As we discussed last week, the idea of having a computer all to yourself a personal 5 | 上周说过"个人计算机"的概念 \N 在计算机发展的头 30 年难以想象 6 | 7 | computer was elusive for the first three decades of electronic computing. 8 | 上周说过"个人计算机"的概念 \N 在计算机发展的头 30 年难以想象 9 | 10 | It was just way too expensive for a computer to be owned and used by one single person. 11 | 如果只让一个人用,成本实在太高 12 | 13 | But, by the early 1970s, all the required components had fallen into place to build 14 | 但到 70 年代初,各种组件的成本都下降了 \N 可以做出低成本 同时性能足够强大的计算机 15 | 16 | a low cost, but still usefully powerful computer. 17 | 但到 70 年代初,各种组件的成本都下降了 \N 可以做出低成本 同时性能足够强大的计算机 18 | 19 | Not a toy, but a tool. 20 | 不是玩具级计算机,是真正能用的计算机 21 | 22 | Most influential in this transition was the advent of single-chip CPUs, 23 | 这个转变中 \N 最有影响力的是 单芯片 CPU 的出现 24 | 25 | which were surprisingly powerful, yet small and inexpensive. 26 | 强大 + 体积小 + 便宜 27 | 28 | Advances in integrated circuits also offered low-cost solid-state memory, 29 | 集成电路的进步,也提供了低成本固态存储器 30 | 31 | both for computer RAM and ROM. 32 | 可以用于计算机的 RAM 和 ROM 33 | 34 | Suddenly it was possible to have an entire computer on one circuit board, 35 | 忽然间 \N 把整台计算机做到一张电路板上成为可能 36 | 37 | dramatically reducing manufacturing costs. 38 | 大大地降低了制造成本 39 | 40 | Additionally, there was cheap and reliable computer storage, 41 | 而且,那时有便宜可靠的储存介质 \N 比如磁带和软盘 42 | 43 | like magnetic tape cassettes and floppy disks. 44 | 而且,那时有便宜可靠的储存介质 \N 比如磁带和软盘 45 | 46 | And finally, the last ingredient was low cost displays, often just repurposed televisions. 47 | 最后是 低成本的显示器 \N 通常是电视机稍作改装而成 48 | 49 | If you blended these four ingredients together in the 1970s, you got, 50 | 如果在 1970 年代 \N 将这四种原料混在一起 51 | 52 | what was called a microcomputer, 53 | 就得到了"微型计算机" 54 | 55 | because these things were so tiny compared to "normal" computers of that era, the 56 | 因为和那个时代的"普通"计算机相比 \N 这些计算机很小 57 | 58 | types you'd see in business or universities. 59 | "普通"计算机就是公司或大学里的那种 60 | 61 | But more important than their size was their cost. 62 | 但比大小更重要的是成本 63 | 64 | These were, for the first time, sufficiently cheap. 65 | 这是有史以来第一次,计算机的价格足够低 66 | 67 | It was practical to buy one and only have one person ever use it. 68 | "一个人专用"的想法变得可行 69 | 70 | No time sharing, 71 | 不用划分时间和别人公用计算机 72 | 73 | no multi-user logins, just a single owner and user. 74 | 没有多用户登录,计算机只属于一个人,只有一个用户 75 | 76 | The personal computer era had arrived. 77 | 个人计算机时代到来 78 | 79 | Computer cost and performance eventually reached the point 80 | 计算机成本下降+性能提升, 让个人计算机成为可能 81 | 82 | where personal computing became viable. 83 | 计算机成本下降+性能提升, 让个人计算机成为可能 84 | 85 | But, it's hard to define exactly when that happened. 86 | 但这个时间点很难准确定义, 并没有一个具体时间点 87 | 88 | There's no one point in time. 89 | 但这个时间点很难准确定义, 并没有一个具体时间点 90 | 91 | And as such, there are many contenders for the title of "first" personal computer, 92 | 因此"第一台个人计算机"这个名号,有很多竞争者 93 | 94 | like the Kenback-1 and MCM/70. 95 | 比如 Kenback-1 和 MCM/70 96 | 97 | Less disputed, however, is the first commercially successful personal computer: The Altair 8800. 98 | 不过第一台取得商业成功的个人计算机 \N 争议较小:Altair 8800 99 | 100 | This machine debuted on the cover of Popular Electronics in 1975, 101 | 首次亮相在 1975 年《Popular Electronics》封面 102 | 103 | and was sold as a $439 kit that you built yourself. 104 | 售价 $439 美元,需要自己组装 105 | 106 | Inflation adjusted, that's about $2,000 today, 107 | 计算通货膨胀后,相当如今的 2000 美元左右 108 | 109 | which isn't chump change, but extremely cheap for a computer in 1975. 110 | 不算小钱,但比起 1975 年的其它计算机,算是非常便宜了 111 | 112 | Tens of thousands of kits were sold to computer hobbyists, 113 | 各种需要自己组装的组件包 \N卖给了计算机爱好者 114 | 115 | and because of its popularity, there were soon all sorts of nifty add-ons available... 116 | 因为买的人多,很快相关产品出现了 117 | 118 | things like extra memory, a paper tape reader and even a teletype interface. 119 | 比如内存,纸带读取器,甚至电传接口 120 | 121 | This allowed you, for example, to load a longer, more complicated program from punch tape, 122 | 让你可以从纸带上读取更长更复杂的程序 123 | 124 | and then interact with it using a teletype terminal. 125 | 然后用电传终端交互 126 | 127 | However, these programs still had to be written in machine code, 128 | 但程序还是要用 机器码 写 129 | 130 | which was really low level and nasty, even for hardcore computer enthusiasts. 131 | 写起来很麻烦,即使计算机爱好者也讨厌写 132 | 133 | This problem didn't escape a young Bill Gates and Paul Allen, 134 | 这没有吓跑年轻的比尔·盖茨和保罗·艾伦 135 | 136 | who were 19 and 22 respectively. 137 | 他们当时是19岁和22岁 138 | 139 | They contacted MITS, the company making the Altair 8800, 140 | 他们联系了制造 Altair 8800 的 MITS 公司 141 | 142 | suggesting the computer would be more attractive to hobbyists 143 | 建议说,如果能运行 BASIC 程序 \N 会对爱好者更有吸引力 144 | 145 | if it could run programs written in BASIC, 146 | 建议说,如果能运行 BASIC 程序 \N 会对爱好者更有吸引力 147 | 148 | a popular and simple programming language. 149 | BASIC 是一门更受欢迎更简单的编程语言 150 | 151 | To do this, they needed to write a program that converted BASIC instructions 152 | 为此,他们需要一个程序 \N 把 BASIC 代码转成可执行机器码 153 | 154 | into native machine code, what's called an interpreter. 155 | 这叫 解释器 (interpreter) 156 | 157 | This is very similar to a compiler, 158 | "解释器"和"编译器"类似 159 | 160 | but happens as the programs runs instead of beforehand. 161 | 区别是"解释器"运行时转换\N而"编译器"提前转换 162 | 163 | Let's go to the thought bubble! 164 | 让我们进入思想泡泡! 165 | 166 | MITS was interested, 167 | MITS 表示感兴趣 168 | 169 | and agreed to meet Bill and Paul for a demonstration. 170 | 同意与 Bill 和 Paul 见个面,让他们演示一下 171 | 172 | Problem is, they hadn't written the interpreter yet. 173 | 问题是,他们还没写好解释器 174 | 175 | So, they hacked it together in just a few weeks 176 | 所以他们花了几个星期赶工 \N 而且还不是在 Altair 8800 上写的 177 | 178 | without even an Altair 8800 to develop on, 179 | 所以他们花了几个星期赶工 \N 而且还不是在 Altair 8800 上写的 180 | 181 | finishing the final piece of code on the plane. 182 | 最后在飞机上完成了代码 183 | 184 | The first time they knew their code worked was at MITS headquarters 185 | 他们在墨西哥 阿尔伯克基(城市) \N 的 MITS 总部做演示时,才知道代码可以成功运行 186 | 187 | in Albuquerque, New Mexico, for the demo. 188 | 他们在墨西哥 阿尔伯克基(城市) \N 的 MITS 总部做演示时,才知道代码可以成功运行 189 | 190 | Fortunately, it went well and MITS agreed to distribute their software. 191 | 幸运的是进展顺利 \N MITS 同意在计算机上搭载他们的软件 192 | 193 | Altair BASIC became the newly formed Microsoft's first product. 194 | Altair BASIC 成了微软的第一个产品 195 | 196 | Although computer hobbyists existed prior to 1975, 197 | 虽然1975年之前就有计算机爱好者 198 | 199 | the Altair 8800 really jump-started the movement. 200 | 但 Altair 8800 大量催生了更多计算机爱好者 201 | 202 | Enthusiast groups formed, sharing knowledge and software and passion about computing. 203 | 爱好者们组成各种小组 \N 分享知识,软件,以及对计算机的热爱 204 | 205 | Most legendary among these is the Homebrew Computer Club, 206 | 最具传奇色彩的小组是"家酿计算机俱乐部" 207 | 208 | which met for the first time in March 1975 209 | 第一次小组聚会在1975年3月 210 | 211 | to see a review unit of the Altair 8800, one of the first to ship to California. 212 | 看一台第一批运来加州的 Altair 8800 213 | 214 | At that first meeting was 24-year-old Steve Wozniak, who was so inspired by 215 | 第一次聚会上,24岁的 Steve Wozniak \N 被 Altair 8800 大大激励 216 | 217 | the Altair 8800 that he set out to design his own computer. 218 | 开始想设计自己的计算机 219 | 220 | In May 1976, he demonstrated his prototype to the Club 221 | 1976年5月,他向小组展示了原型机 222 | 223 | and shared the schematics with interested members. 224 | 并且把电路图分享给感兴趣的其他会员 225 | 226 | Unusual for the time, it was designed to connect to a TV and offered a text interface 227 | 他的设计不同寻常 \N 要连到电视显示,并提供文本界面 228 | 229 | a first for a low-cost computer. 230 | 在低成本计算机上还是第一次见 231 | 232 | Interest was high, and shortly after fellow club member and college friend Steve Jobs 233 | 同是俱乐部成员和大学朋友的 史蒂夫·乔布斯 234 | 235 | suggested that instead of just sharing the designs for free, 236 | 建议说与其免费分享设计,不如直接出售装好的主板 237 | 238 | they should just sell an assembled motherboard. 239 | 建议说与其免费分享设计,不如直接出售装好的主板 240 | 241 | However, you still had to add your own keyboard, power supply, and enclosure. 242 | 但用户依然需要自己加键盘,电源和机箱 243 | 244 | It went on sale in July 1976 with a price tag of $666.66. 245 | 1976年7月开始发售,价格$666.66美元 246 | 247 | It was called the Apple-I, and it was Apple Computer's first product. 248 | 它叫 Apple-I ,苹果计算机公司的第一个产品 249 | 250 | Thanks thought bubble! 251 | 谢了 思想泡泡 252 | 253 | Like the Altair 8800, the Apple-I was sold as a kit. 254 | 就像 Altair 8800 一样,Apple-I 也是作为套件出售 255 | 256 | It appealed to hobbyists, 257 | Apple-I 吸引了业余爱好者 \N 不介意机器买回来自己组装 258 | 259 | who didn't mind tinkering and soldering, 260 | Apple-I 吸引了业余爱好者 \N 不介意机器买回来自己组装 261 | 262 | but consumers and businesses weren't interested. 263 | 但个人消费者和公司对 Apple-I 不感兴趣 264 | 265 | This changed in 1977, 266 | 这在 1977 年发生变化 \N 市场上有了三款开箱即用的计算机 267 | 268 | with the release of three game-changing computers, that could be used right out of the box. 269 | 这在 1977 年发生变化 \N 市场上有了三款开箱即用的计算机 270 | 271 | First was the Apple II, 272 | 第一款是 Apple-II 273 | 274 | Apple's earliest product that sold as a complete system 275 | 苹果公司第一个提供全套设备的产品 276 | 277 | that was professionally designed and manufactured. 278 | 设计和制造工艺都是专业的 279 | 280 | It also offered rudimentary color graphics and sound output, 281 | 它还提供了简单彩色图形和声音输出 282 | 283 | amazing features for a low cost machine. 284 | 这些功能对低成本机器非常了不起 285 | 286 | The Apple II series of computers sold by the millions and quickly 287 | Apple-II 卖了上百万套 288 | 289 | propelled Apple to the forefront of the personal computing industry. 290 | 把苹果公司推到了个人计算机行业的前沿 291 | 292 | The second computer was the TRS-80 Model I, 293 | 第二款是"TRS-80 1型" 294 | 295 | made by the Tandy Corporation 296 | 由 Tandy 公司生产 297 | 298 | and sold by RadioShack hence the "TRS" 299 | 由 Radioshack 销售,所以叫 TRS 300 | 301 | Although less advanced than the Apple II, 302 | 虽然不如 Apple-II 先进 \N 但因为价格只有一半,所以卖得很火爆 303 | 304 | it was half the cost and sold like hot cakes. 305 | 虽然不如 Apple-II 先进 \N 但因为价格只有一半,所以卖得很火爆 306 | 307 | Finally, there was the Commodore PET 2001, 308 | 最后一款是 Commodore PET 2001 309 | 310 | with a unique all-in-one design 311 | 有一体化设计 312 | 313 | that combined computer, monitor, keyboard and tape drive into one device, 314 | 集成了计算机,显示器,键盘和磁带驱动器 315 | 316 | aimed to appeal to consumers. 317 | 目标是吸引普通消费者 318 | 319 | It started to blur the line between computer and appliance. 320 | 计算机和家用电器之间的界限开始变得模糊 321 | 322 | These three computers became known as the 1977 Trinity. 323 | 这3台计算机被称为1977年的"三位一体" 324 | 325 | They all came bundled with BASIC interpreters, 326 | 它们都自带了 BASIC 解释器 327 | 328 | allowing non-computer-wizards to create programs. 329 | 让不那么精通计算机的人也能用 BASIC 写程序 330 | 331 | The consumer software industry also took off, 332 | 针对消费者的软件行业 开始腾飞 333 | 334 | offering games and productivity tools for personal computers, 335 | 市场上出现了各种 \N 针对个人计算机的游戏和生产力工具 336 | 337 | like calculators and word processors. 338 | 比如计算器和文字处理器 339 | 340 | The killer app of the era was 1979's VisiCalc, 341 | 最火的是 1979 年的 VisiCalc 342 | 343 | the first spreadsheet program 344 | 第一个电子表格程序 345 | 346 | which was infinitely better than paper 347 | 比纸好无数倍 348 | 349 | and the forbearer of programs like Microsoft Excel and Google Sheets. 350 | 是微软 Excel 和 Google Sheets 的老祖先 351 | 352 | But perhaps the biggest legacy of these computers was their marketing, 353 | 但这些计算机带来的最大影响 \N 也许是他们的营销策略 354 | 355 | they were the first to be targeted at households, and not just businesses and hobbyists. 356 | 它们针对普通消费者 \N 而不是企业和爱好者 357 | 358 | And for the first time in a substantial way, 359 | 这是第一次大规模地 360 | 361 | computers started to appear in homes, and also small businesses and schools. 362 | 计算机出现在家庭,小公司,以及学校中 363 | 364 | This caught the attention of the biggest computer company on the planet, IBM, who had seen its 365 | 这引起了全球最大计算机公司 IBM 的注意 366 | 367 | share of the overall computer market shrink from 60% in 1970 to around 30% by 1980. 368 | 其市场份额从1970年的60% \N 在1980年降到了30%左右 369 | 370 | This was mainly because IBM had ignored the microcomputer market, 371 | 因为IBM忽略了增长的"微型计算机"市场 372 | 373 | which was growing at about 40% annually. 374 | 这个市场每年增长约40% 375 | 376 | As microcomputers evolved into personal computers, IBM knew it needed to get in on the action. 377 | 随着微型计算机演变成个人计算机 \N IBM 知道他们需要采取行动 378 | 379 | But to do this, it would have to radically rethink its computer strategy and design. 380 | 但要做到这一点 \N 公司要从根本上重新思考战略和设计 381 | 382 | In 1980, IBM's least-expensive computer, the 5120, cost roughly ten thousand dollars, 383 | 1980年 IBM 最便宜的计算机 \N "5120"的价格大概是一万美元 384 | 385 | which was never going to compete with the likes of the Apple II. 386 | 永远也没法和 Apple-II 这样的计算机竞争 387 | 388 | This meant starting from scratch. 389 | 意味着要从头开始 390 | 391 | A crack team of twelve engineers, later nicknamed the dirty dozen, 392 | 一个由十二名工程师组成的精干团队(后来叫"肮脏十二人") 393 | 394 | were sent off to offices in Boca Raton, Florida, 395 | 被派往佛罗里达州的 \N 博卡拉顿(Boca Raton)办公室 396 | 397 | to be left alone and put their talents to work. 398 | 让他们独立工作 399 | 400 | Shielded from IBM internal politics, they were able to design a machine as they desired. 401 | 不受 IBM 内部的政治斗争干扰 \N 他们想怎么设计怎么设计 402 | 403 | Instead of using IBM proprietary CPUs, they chose Intel chips. 404 | 没用 IBM 的 CPU,选了 Intel 的芯片 405 | 406 | Instead of using IBM's prefered operating system, CP/M, 407 | 也没用 IBM 的首选操作系统 CP/M 408 | 409 | they licenced Microsoft's Disk Operating System: DOS 410 | 而是用了微软的 DOS 411 | 412 | and so on, from the screen to the printer. 413 | 依此类推,从屏幕到打印机都这样自由选择 414 | 415 | For the first time, IBM divisions had to compete with outside firms 416 | IBM 第一次不得不与外部公司竞争 417 | 418 | to build hardware and software for the new computer. 419 | 来给新计算机做硬件和软件 420 | 421 | This radical break from the company tradition of in-house development kept costs low 422 | 这和 IBM 的传统做法不同:自己做硬件来节省成本 423 | 424 | and brought partner firms into the fold. 425 | 然后和其它公司合作 426 | 427 | After just a year of development, 428 | 经过短短一年 429 | 430 | the IBM Personal Computer, or IBM PC was released. 431 | IBM 个人计算机发布了,简称 IBM PC 432 | 433 | It was an immediate success, 434 | 产品立马取得了成功 435 | 436 | especially with businesses that had long trusted the IBM brand. 437 | 长期信任 IBM 品牌的企业买了很多 438 | 439 | But, most influential to its ultimate success was that the computer featured an open architecture, 440 | 但最有影响力的是 \N 它使用 "开放式架构" 441 | 442 | with good documentation and expansion slots, 443 | 有良好的文档和扩展槽 444 | 445 | allowing third parties to create new hardware and peripherals for the platform. 446 | 使得第三方可以做硬件/外设 447 | 448 | That included things like graphics cards, sounds cards, external hard drives, joysticks, 449 | 包括显卡,声卡,外置硬盘,游戏控制杆 \N 以及无数其它组件 450 | 451 | and countless other add-ons. 452 | 包括显卡,声卡,外置硬盘,游戏控制杆 \N 以及无数其它组件 453 | 454 | This spurred innovation, and also competition, resulting in a huge ecosystem of products. 455 | 这刺激了创新,激发了竞争,产生了巨大的生态系统 456 | 457 | This open architecture became known as "IBM Compatible". 458 | 这个开放架构叫 IBM Compatible"(IBM 兼容) 459 | 460 | If you bought an "IBM Compatible" computer, it meant you 461 | 意味着如果买了"IBM兼容"的计算机 462 | 463 | could use that huge ecosystem of software and hardware. 464 | 你可以用庞大生态系统中的其它软硬件 465 | 466 | Being an open architecture also meant that competitor companies could follow the standard 467 | 开放架构也意味着 竞争对手公司可以遵循这个标准 468 | 469 | and create their own IBM Compatible computers. 470 | 做出自己的"IBM 兼容"计算机 471 | 472 | Soon, Compaq and Dell were selling their own PC clones... 473 | 很快,康柏和戴尔也开始卖 PC 474 | 475 | And Microsoft was happy to license MS-DOS to them, 476 | 微软很乐意把 MS-DOS 授权给他们 477 | 478 | quickly making it the most popular PC operating system. 479 | 使 DOS 迅速成为最受欢迎的 PC 操作系统 480 | 481 | IBM alone sold two million PCs in the first three years, overtaking Apple. 482 | 仅在前三年 \N IBM就卖出了200万台 PC ,超过了苹果 483 | 484 | With a large user base, software and hardware developers concentrated 485 | 有了庞大用户群,软件和硬件开发人员 \N 把精力放在"IBM 兼容"平台,因为潜在用户更多 486 | 487 | their efforts on IBM Compatible platforms, there were just more users to sell to. 488 | 有了庞大用户群,软件和硬件开发人员 \N 把精力放在"IBM 兼容"平台,因为潜在用户更多 489 | 490 | Then, people wishing to buy a computer bought the one with the 491 | 同时,想买计算机的人 \N 也会看哪种计算机的软硬件选择更多 492 | 493 | most software and hardware available, and this effect snowballed. 494 | 就像雪球效应一样 495 | 496 | Companies producing non-IBM-compatible computers, often with superior specs, 497 | 而那些生产非"IBM兼容"计算机的公司 (一般性能更好) 498 | 499 | failed. 500 | 都失败了 501 | 502 | Only Apple kept significant market share without IBM compatibility. 503 | 只有苹果公司在没有"IBM兼容"的情况下 \N 保持了足够市场份额 504 | 505 | Apple ultimately chose to take the opposite approach a "closed architecture" proprietary 506 | 苹果公司最终选了相反的方式:"封闭架构" 507 | 508 | designs that typically prevent people from adding new hardware to their computers. 509 | 即自己设计一切,用户一般无法加新硬件到计算机中 510 | 511 | This meant that Apple made its own computers, with its own operating system, and often its 512 | 意味着苹果公司要做自己的计算机,自己的操作系统 513 | 514 | own peripherals, like displays, keyboards, and printers. 515 | 还有自己的外围设备,如显示器,键盘和打印机 516 | 517 | By controlling the full stack, from hardware to software, 518 | 通过控制整个范围,从硬件到软件 519 | 520 | Apple was able to control the user experience and improve reliability. 521 | 苹果能控制用户体验并提高可靠性 522 | 523 | These competing business strategies were the genesis of the "Mac" versus "PC" division 524 | 不同的商业策略是"Mac vs PC 谁更好"这种争论的起源 525 | 526 | that still exists today... which is a misnomer, because they're both personal computers! 527 | 这些争论如今还存在 \N 不过"Mac vs PC"用词不对,因为它们都是个人计算机! 528 | 529 | But whatever. 530 | 但是随便啦 531 | 532 | To survive the onslaught of low-cost PCs, 533 | 为了在低成本个人计算机的竞争冲击下生存下来 534 | 535 | Apple needed to up its game, 536 | 苹果需要提高自身水平 \N 提供比 PC 和 DOS 更好的用户体验 537 | 538 | and offer a user experience that PCs and DOS couldn't. 539 | 苹果需要提高自身水平 \N 提供比 PC 和 DOS 更好的用户体验 540 | 541 | Their answer was the Macintosh, released in 1984. 542 | 他们的答案是 Macintosh,于 1984 年发布 543 | 544 | This ground breaking, reasonably-low-cost, all-in-one computer 545 | 一台突破性 价格适中的一体式计算机 \N 用的不是命令行界面,而是图形界面 546 | 547 | booted not a command-line text-interface, but rather a graphical user interface, 548 | 一台突破性 价格适中的一体式计算机 \N 用的不是命令行界面,而是图形界面 549 | 550 | our topic for next week. See you then. 551 | 我们下周讨论图形界面. 到时见 552 | 553 | -------------------------------------------------------------------------------- /(字幕)全40集中英字幕文本/35. 计算机视觉-Computer Vision.ass.txt: -------------------------------------------------------------------------------- 1 | Hi, I'm Carrie Anne, and welcome to Crash Course Computer Science! 2 | (。・∀・)ノ゙嗨 我是Carrie Anne,欢迎收看计算机科学速成课 3 | 4 | Today, let's start by thinking about how important vision can be. 5 | 今天 我们来思考视觉的重要性 6 | 7 | Most people rely on it to prepare food, 8 | 大部分人靠视觉来做饭 9 | 10 | walk around obstacles, 11 | 越过障碍 12 | 13 | read street signs, 14 | 读路牌 15 | 16 | watch videos like this, 17 | 看视频 18 | 19 | and do hundreds of other tasks. 20 | 以及无数其它任务 21 | 22 | Vision is the highest bandwidth sense, 23 | 视觉是信息最多的感官 \N 比如周围的世界是怎样的,如何和世界交互 24 | 25 | and it provides a firehose of information about the state of the world and how to act on it. 26 | 视觉是信息最多的感官 \N 比如周围的世界是怎样的,如何和世界交互 27 | 28 | For this reason, computer scientists have been trying to give computers vision for half a century, 29 | 因此半个世纪来\N 计算机科学家一直在想办法让计算机有视觉 30 | 31 | birthing the sub-field of computer vision. 32 | 因此诞生了"计算机视觉"这个领域 33 | 34 | Its goal is to give computers the ability 35 | 目标是让计算机理解图像和视频 36 | 37 | to extract high-level understanding from digital images and videos. 38 | 目标是让计算机理解图像和视频 39 | 40 | As everyone with a digital camera or smartphone knows, 41 | 用过相机或手机的都知道 \N 可以拍出有惊人保真度和细节的照片 42 | 43 | computers are already really good at capturing photos with incredible fidelity and detail 44 | 用过相机或手机的都知道 \N 可以拍出有惊人保真度和细节的照片 45 | 46 | - much better than humans in fact. 47 | - 比人类强得多 48 | 49 | But as computer vision professor Fei-Fei Li recently said, 50 | 但正如计算机视觉教授 李飞飞 最近说的 51 | 52 | "Just like to hear is the not the same as to listen. 53 | "听到"不等于"听懂" 54 | 55 | To take pictures is not the same as to see." 56 | "看到"不等于"看懂" 57 | 58 | As a refresher, images on computers are most often stored as big grids of pixels. 59 | 复习一下,图像是像素网格 60 | 61 | Each pixel is defined by a color, stored as a combination of three additive primary colors: 62 | 每个像素的颜色 通过三种基色定义:红,绿,蓝 63 | 64 | red, green and blue. 65 | 每个像素的颜色 通过三种基色定义:红,绿,蓝 66 | 67 | By combining different intensities of these three colors, 68 | 通过组合三种颜色的强度 \N 可以得到任何颜色, 也叫 RGB 值 69 | 70 | we can represent any color. what's called a RGB value, 71 | 通过组合三种颜色的强度 \N 可以得到任何颜色, 也叫 RGB 值 72 | 73 | Perhaps the simplest computer vision algorithm 74 | 最简单的计算机视觉算法 75 | 76 | - and a good place to start - 77 | 最合适拿来入门的 78 | 79 | is to track a colored object, like a bright pink ball. 80 | 是跟踪一个颜色物体,比如一个粉色的球 81 | 82 | The first thing we need to do is record the ball's color. 83 | 首先,我们记下球的颜色,保存最中心像素的 RGB 值 84 | 85 | For that, we'll take the RGB value of the centermost pixel. 86 | 首先,我们记下球的颜色,保存最中心像素的 RGB 值 87 | 88 | With that value saved, we can give a computer program an image, 89 | 然后给程序喂入图像,让它找最接近这个颜色的像素 90 | 91 | and ask it to find the pixel with the closest color match. 92 | 然后给程序喂入图像,让它找最接近这个颜色的像素 93 | 94 | An algorithm like this might start in the upper right corner, 95 | 算法可以从左上角开始,逐个检查像素 96 | 97 | and check each pixel, one at time, 98 | 算法可以从左上角开始,逐个检查像素 99 | 100 | calculating the difference from our target color. 101 | 计算和目标颜色的差异 102 | 103 | Now, having looked at every pixel, 104 | 检查了每个像素后,最贴近的像素,很可能就是球 105 | 106 | the best match is very likely a pixel from our ball. 107 | 检查了每个像素后,最贴近的像素,很可能就是球 108 | 109 | We're not limited to running this algorithm on a single photo; 110 | 不只是这张图片 \N 我们可以在视频的每一帧图片跑这个算法 111 | 112 | we can do it for every frame in a video, 113 | 不只是这张图片 \N 我们可以在视频的每一帧图片跑这个算法 114 | 115 | allowing us to track the ball over time. 116 | 跟踪球的位置 117 | 118 | Of course, due to variations in lighting, shadows, and other effects, 119 | 当然,因为光线,阴影和其它影响 120 | 121 | the ball on the field is almost certainly not going to be the exact same RGB value as our target color, 122 | 球的颜色会有变化,不会和存的 RGB 值完全一样 123 | 124 | but merely the closest match. 125 | 但会很接近 126 | 127 | In more extreme cases, like at a game at night, 128 | 如果情况更极端一些 \N 比如比赛是在晚上,追踪效果可能会很差 129 | 130 | the tracking might be poor. 131 | 如果情况更极端一些 \N 比如比赛是在晚上,追踪效果可能会很差 132 | 133 | And if one of the team's jerseys used the same color as the ball, 134 | 如果球衣的颜色和球一样,算法就完全晕了 135 | 136 | our algorithm would get totally confused. 137 | 如果球衣的颜色和球一样,算法就完全晕了 138 | 139 | For these reasons, color marker tracking and similar algorithms are rarely used, 140 | 因此很少用这类颜色跟踪算法 141 | 142 | unless the environment can be tightly controlled. 143 | 除非环境可以严格控制 144 | 145 | This color tracking example was able to search pixel-by-pixel, 146 | 颜色跟踪算法是一个个像素搜索 \N 因为颜色是在一个像素里 147 | 148 | because colors are stored inside of single pixels. 149 | 颜色跟踪算法是一个个像素搜索 \N 因为颜色是在一个像素里 150 | 151 | But this approach doesn't work for features larger than a single pixel, 152 | 但这种方法 不适合占多个像素的特征 153 | 154 | like edges of objects, which are inherently made up of many pixels. 155 | 比如物体的边缘,是多个像素组成的. 156 | 157 | To identify these types of features in images, 158 | 为了识别这些特征,算法要一块块像素来处理 159 | 160 | computer vision algorithms have to consider small regions of pixels, 161 | 为了识别这些特征,算法要一块块像素来处理 162 | 163 | called patches. 164 | 每一块都叫"块" 165 | 166 | As an example, let's talk about an algorithm that finds vertical edges in a scene, 167 | 举个例子,找垂直边缘的算法 168 | 169 | let's say to help a drone navigate safely through a field of obstacles. 170 | 假设用来帮无人机躲避障碍 171 | 172 | To keep things simple, we're going to convert our image into grayscale, 173 | 为了简单,我们把图片转成灰度 \N 不过大部分算法可以处理颜色 174 | 175 | although most algorithms can handle color. 176 | 为了简单,我们把图片转成灰度 \N 不过大部分算法可以处理颜色 177 | 178 | Now let's zoom into one of these poles to see what an edge looks like up close. 179 | 放大其中一个杆子,看看边缘是怎样的 180 | 181 | We can easily see where the left edge of the pole starts, 182 | 可以很容易地看到 杆子的左边缘从哪里开始 183 | 184 | because there's a change in color that persists across many pixels vertically. 185 | 因为有垂直的颜色变化 186 | 187 | We can define this behavior more formally by creating a rule 188 | 我们可以弄个规则说 189 | 190 | that says the likelihood of a pixel being a vertical edge 191 | 某像素是垂直边缘的可能性 \N 取决于左右两边像素的颜色差异程度 192 | 193 | is the magnitude of the difference in color 194 | 某像素是垂直边缘的可能性 \N 取决于左右两边像素的颜色差异程度 195 | 196 | between some pixels to its left and some pixels to its right. 197 | 某像素是垂直边缘的可能性 \N 取决于左右两边像素的颜色差异程度 198 | 199 | The bigger the color difference between these two sets of pixels, 200 | 左右像素的区别越大,这个像素越可能是边缘 201 | 202 | the more likely the pixel is on an edge. 203 | 左右像素的区别越大,这个像素越可能是边缘 204 | 205 | If the color difference is small, it's probably not an edge at all. 206 | 如果色差很小,就不是边缘 207 | 208 | The mathematical notation for this operation looks like this 209 | 这个操作的数学符号 看起来像这样 210 | 211 | it's called a kernel or filter. 212 | 这叫"核"或"过滤器" 213 | 214 | It contains the values for a pixel-wise multiplication, 215 | 里面的数字用来做像素乘法 216 | 217 | the sum of which is saved into the center pixel. 218 | 总和 存到中心像素里 219 | 220 | Let's see how this works for our example pixel. 221 | 我们来看个实际例子 222 | 223 | I've gone ahead and labeled all of the pixels with their grayscale values. 224 | 我已经把所有像素转成了灰度值 225 | 226 | Now, we take our kernel, and center it over our pixel of interest. 227 | 现在把"核"的中心,对准感兴趣的像素 228 | 229 | This specifies what each pixel value underneath should be multiplied by. 230 | 这指定了每个像素要乘的值 231 | 232 | Then, we just add up all those numbers. 233 | 然后把所有数字加起来 234 | 235 | In this example, that gives us 147. 236 | 在这里,最后结果是 147 237 | 238 | That becomes our new pixel value. 239 | 成为新像素值 240 | 241 | This operation, of applying a kernel to a patch of pixels, 242 | 把 核 应用于像素块,这种操作叫"卷积" 243 | 244 | is call a convolution. 245 | 把 核 应用于像素块,这种操作叫"卷积" 246 | 247 | Now let's apply our kernel to another pixel. 248 | 现在我们把"核"应用到另一个像素 249 | 250 | In this case, the result is 1. Just 1. 251 | 结果是 1 252 | 253 | In other words, it's a very small color difference, and not an edge. 254 | 色差很小,不是边缘 255 | 256 | If we apply our kernel to every pixel in the photo, 257 | 如果把"核"用于照片中每个像素 258 | 259 | the result looks like this, 260 | 结果会像这样 261 | 262 | where the highest pixel values are where there are strong vertical edges. 263 | 垂直边缘的像素值很高 264 | 265 | Note that horizontal edges, like those platforms in the background, 266 | 注意,水平边缘(比如背景里的平台) 267 | 268 | are almost invisible. 269 | 几乎看不见 270 | 271 | If we wanted to highlight those features, 272 | 如果要突出那些特征 273 | 274 | we'd have to use a different kernel 275 | 要用不同的"核" 276 | 277 | - one that's sensitive to horizontal edges. 278 | 用对水平边缘敏感的"核" 279 | 280 | Both of these edge enhancing kernels are called Prewitt Operators, 281 | 这两个边缘增强的核叫"Prewitt 算子" 282 | 283 | named after their inventor. 284 | 以发明者命名 285 | 286 | These are just two examples of a huge variety of kernels, 287 | 这只是众多"核"的两个例子 288 | 289 | able to perform many different image transformations. 290 | "核"能做很多种图像转换 291 | 292 | For example, here's a kernel that sharpens images. 293 | 比如这个"核"能锐化图像 294 | 295 | And here's a kernel that blurs them. 296 | 这个"核"能模糊图像 297 | 298 | Kernels can also be used like little image cookie cutters that match only certain shapes. 299 | "核"也可以像饼干模具一样,匹配特定形状 300 | 301 | So, our edge kernels looked for image patches 302 | 之前做边缘检测的"核" 303 | 304 | with strong differences from right to left or up and down. 305 | 会检查左右或上下的差异 306 | 307 | But we could also make kernels that are good at finding lines, with edges on both sides. 308 | 但我们也可以做出 擅长找线段的"核" 309 | 310 | And even islands of pixels surrounded by contrasting colors. 311 | 或者包了一圈对比色的区域 312 | 313 | These types of kernels can begin to characterize simple shapes. 314 | 这类"核"可以描述简单的形状 315 | 316 | For example, on faces, the bridge of the nose tends to be brighter than the sides of the nose, 317 | 比如鼻梁往往比鼻子两侧更亮 318 | 319 | resulting in higher values for line-sensitive kernels. 320 | 所以线段敏感的"核"对这里的值更高 321 | 322 | Eyes are also distinctive 323 | 眼睛也很独特 324 | 325 | - a dark circle sounded by lighter pixels - 326 | - 一个黑色圆圈被外层更亮的一层像素包着 327 | 328 | a pattern other kernels are sensitive to. 329 | 有其它"核"对这种模式敏感 330 | 331 | When a computer scans through an image, 332 | 当计算机扫描图像时,最常见的是用一个窗口来扫 333 | 334 | most often by sliding around a search window, 335 | 当计算机扫描图像时,最常见的是用一个窗口来扫 336 | 337 | it can look for combinations of features indicative of a human face. 338 | 可以找出人脸的特征组合 339 | 340 | Although each kernel is a weak face detector by itself, 341 | 虽然每个"核"单独找出脸的能力很弱 \N 但组合在一起会相当准确 342 | 343 | combined, they can be quite accurate. 344 | 虽然每个"核"单独找出脸的能力很弱 \N 但组合在一起会相当准确 345 | 346 | It's unlikely that a bunch of face-like features will cluster together if they're not a face. 347 | 不是脸但又有一堆脸的特征在正确的位置,\N 这种情况不太可能 348 | 349 | This was the basis of an early and influential algorithm 350 | 这是一个早期很有影响力的算法的基础 351 | 352 | called Viola-Jones Face Detection. 353 | 叫 维奥拉·琼斯 人脸检测算法 354 | 355 | Today, the hot new algorithms on the block are Convolutional Neural Networks. 356 | 如今的热门算法是 "卷积神经网络" 357 | 358 | We talked about neural nets last episode, if you need a primer. 359 | 我们上集谈了神经网络,如果需要可以去看看 360 | 361 | In short, an artificial neuron 362 | 总之,神经网络的最基本单位,是神经元 363 | 364 | - which is the building block of a neural network - 365 | 总之,神经网络的最基本单位,是神经元 366 | 367 | takes a series of inputs, and multiplies each by a specified weight, 368 | 它有多个输入,然后会把每个输入 乘一个权重值 369 | 370 | and then sums those values all together. 371 | 然后求总和 372 | 373 | This should sound vaguely familiar, because it's a lot like a convolution. 374 | 听起来好像挺耳熟,因为它很像"卷积" 375 | 376 | In fact, if we pass a neuron 2D pixel data, rather than a one-dimensional list of inputs, 377 | 实际上,如果我们给神经元输入二维像素 378 | 379 | it's exactly like a convolution. 380 | 完全就像"卷积" 381 | 382 | The input weights are equivalent to kernel values, 383 | 输入权重等于"核"的值 384 | 385 | but unlike a predefined kernel, 386 | 但和预定义"核"不同 387 | 388 | neural networks can learn their own useful kernels 389 | 神经网络可以学习对自己有用的"核" 390 | 391 | that are able to recognize interesting features in images. 392 | 来识别图像中的特征 393 | 394 | Convolutional Neural Networks use banks of these neurons to process image data, 395 | "卷积神经网络"用一堆神经元处理图像数据 396 | 397 | each outputting a new image, essentially digested by different learned kernels. 398 | 每个都会输出一个新图像,\N 本质上是被不同的"核"处理了 399 | 400 | These outputs are then processed by subsequent layers of neurons, 401 | 输出会被后面一层神经元处理 402 | 403 | allowing for convolutions on convolutions on convolutions. 404 | 卷积卷积再卷积 405 | 406 | The very first convolutional layer might find things like edges, 407 | 第一层可能会发现"边缘"这样的特征 408 | 409 | as that's what a single convolution can recognize, as we've already discussed. 410 | 单次卷积可以识别出这样的东西,之前说过 411 | 412 | The next layer might have neurons that convolve on those edge features 413 | 下一层可以在这些基础上识别 414 | 415 | to recognize simple shapes, comprised of edges, like corners. 416 | 比如由"边缘"组成的角落 417 | 418 | A layer beyond that might convolve on those corner features, 419 | 然后下一层可以在"角落"上继续卷积 420 | 421 | and contain neurons that can recognize simple objects, 422 | 下一些可能有识别简单物体的神经元 423 | 424 | like mouths and eyebrows. 425 | 比如嘴和眉毛 426 | 427 | And this keeps going, building up in complexity, 428 | 然后不断重复,逐渐增加复杂度 429 | 430 | until there's a layer that does a convolution that puts it together: 431 | 直到某一层把所有特征放到一起: 432 | 433 | eyes, ears, mouth, nose, the whole nine yards, 434 | 眼睛,耳朵,嘴巴,鼻子 435 | 436 | and says "ah ha, it's a face!" 437 | 然后说:"啊哈,这是脸!" 438 | 439 | Convolutional neural networks aren't required to be many layers deep, 440 | "卷积神经网络"不是非要很多很多层 441 | 442 | but they usually are, in order to recognize complex objects and scenes. 443 | 但一般会有很多层,来识别复杂物体和场景 444 | 445 | That's why the technique is considered deep learning. 446 | 所以算是"深度学习" 447 | 448 | Both Viola-Jones and Convolutional Neural Networks can be applied to many image recognition problems, 449 | "维奥拉·琼斯"和"卷积神经网络"\N 不只是认人脸,还可以识别手写文字 450 | 451 | beyond faces, like recognizing handwritten text, 452 | "维奥拉·琼斯"和"卷积神经网络"\N 不只是认人脸,还可以识别手写文字 453 | 454 | spotting tumors in CT scans and monitoring traffic flow on roads. 455 | 在 CT 扫描中发现肿瘤,监测马路是否拥堵 456 | 457 | But we're going to stick with faces. 458 | 但我们这里接着用人脸举例 459 | 460 | Regardless of what algorithm was used, once we've isolated a face in a photo, 461 | 不管用什么算法,识别出脸之后 462 | 463 | we can apply more specialized computer vision algorithms to pinpoint facial landmarks, 464 | 可以用更专用的计算机视觉算法 \N 来定位面部标志 465 | 466 | like the tip of the nose and corners of the mouth. 467 | 比如鼻尖和嘴角 468 | 469 | This data can be used for determining things like if the eyes are open, 470 | 有了标志点,判断眼睛有没有张开就很容易了 471 | 472 | which is pretty easy once you have the landmarks 473 | 有了标志点,判断眼睛有没有张开就很容易了 474 | 475 | it's just the distance between points. 476 | 只是点之间的距离罢了 477 | 478 | We can also track the position of the eyebrows; 479 | 也可以跟踪眉毛的位置 480 | 481 | their relative position to the eyes can be an indicator of surprise, or delight. 482 | 眉毛相对眼睛的位置 可以代表惊喜或喜悦 483 | 484 | Smiles are also pretty straightforward to detect based on the shape of mouth landmarks. 485 | 根据嘴巴的标志点,检测出微笑也很简单 486 | 487 | All of this information can be interpreted by emotion recognition algorithms, 488 | 这些信息可以用"情感识别算法"来识别 489 | 490 | giving computers the ability to infer when you're happy, sad, frustrated, confused and so on. 491 | 让电脑知道你是开心,忧伤,沮丧,困惑等等 492 | 493 | In turn, that could allow computers to intelligently adapt their behavior... 494 | 然后计算机可以做出合适的行为. 495 | 496 | maybe offer tips when you're confused, 497 | 比如当你不明白时 给你提示 498 | 499 | and not ask to install updates when you're frustrated. 500 | 你心情不好时,就不弹更新提示了 501 | 502 | This is just one example of how vision can give computers the ability to be context sensitive, 503 | 这只是计算机通过视觉感知周围的一个例子 504 | 505 | that is, aware of their surroundings. 506 | 这只是计算机通过视觉感知周围的一个例子 507 | 508 | And not just the physical surroundings 509 | 不只是物理环境 - 比如是不是在上班,或是在火车上 510 | 511 | - like if you're at work or on a train - 512 | 不只是物理环境 - 比如是不是在上班,或是在火车上 513 | 514 | but also your social surroundings 515 | 还有社交环境 - 比如是朋友的生日派对,还是正式商务会议 516 | 517 | - like if you're in a formal business meeting versus a friend's birthday party. 518 | 还有社交环境 - 比如是朋友的生日派对,还是正式商务会议 519 | 520 | You behave differently in those surroundings, and so should computing devices, 521 | 你在不同环境会有不同行为,计算机也应如此 522 | 523 | if they're smart. 524 | 如果它们够聪明的话... 525 | 526 | Facial landmarks also capture the geometry of your face, 527 | 面部标记点 也可以捕捉脸的形状 528 | 529 | like the distance between your eyes and the height of your forehead. 530 | 比如两只眼睛之间的距离,以及前额有多高 531 | 532 | This is one form of biometric data, 533 | 做生物识别 534 | 535 | and it allows computers with cameras to recognize you. 536 | 让有摄像头的计算机能认出你 537 | 538 | Whether it's your smartphone automatically unlocking itself when it sees you, 539 | 不管是手机解锁 还是政府用摄像头跟踪人 540 | 541 | or governments tracking people using CCTV cameras, 542 | 不管是手机解锁 还是政府用摄像头跟踪人 543 | 544 | the applications of face recognition seem limitless. 545 | 人脸识别有无限应用场景 546 | 547 | There have also been recent breakthroughs in landmark tracking for hands and whole bodies, 548 | 另外 跟踪手臂和全身的标记点,最近也有一些突破 549 | 550 | giving computers the ability to interpret a user's body language, 551 | 让计算机理解用户的身体语言 552 | 553 | and what hand gestures they're frantically waving at their internet connected microwave. 554 | 比如用户给联网微波炉的手势 555 | 556 | As we've talked about many times in this series, 557 | 正如系列中常说的,抽象是构建复杂系统的关键 558 | 559 | abstraction is the key to building complex systems, 560 | 正如系列中常说的,抽象是构建复杂系统的关键 561 | 562 | and the same is true in computer vision. 563 | 计算机视觉也是一样 564 | 565 | At the hardware level, you have engineers building better and better cameras, 566 | 硬件层面,有工程师在造更好的摄像头 \N 让计算机有越来越好的视力 567 | 568 | giving computers improved sight with each passing year, 569 | 硬件层面,有工程师在造更好的摄像头 \N 让计算机有越来越好的视力 570 | 571 | which I can't say for myself. 572 | 我自己的视力却不能这样 573 | 574 | Using that camera data, 575 | 用来自摄像头的数据 可以用视觉算法找出脸和手 576 | 577 | you have computer vision algorithms crunching pixels to find things like faces and hands. 578 | 用来自摄像头的数据 可以用视觉算法找出脸和手 579 | 580 | And then, using output from those algorithms, 581 | 然后可以用其他算法接着处理,解释图片中的东西 582 | 583 | you have even more specialized algorithms for interpreting things 584 | 然后可以用其他算法接着处理,解释图片中的东西 585 | 586 | like user facial expression and hand gestures. 587 | 比如用户的表情和手势 588 | 589 | On top of that, there are people building novel interactive experiences, 590 | 有了这些,人们可以做出新的交互体验 591 | 592 | like smart TVs and intelligent tutoring systems, 593 | 比如智能电视和智能辅导系统 \N 会根据用户的手势和表情来回应 594 | 595 | that respond to hand gestures and emotion. 596 | 比如智能电视和智能辅导系统 \N 会根据用户的手势和表情来回应 597 | 598 | Each of these levels are active areas of research, 599 | 这里的每一层都是活跃的研究领域 600 | 601 | with breakthroughs happening every year. 602 | 每年都有突破,这只是冰山一角 603 | 604 | And that's just the tip of the iceberg. 605 | 每年都有突破,这只是冰山一角 606 | 607 | Today, computer vision is everywhere 608 | 如今 计算机视觉无处不在 609 | 610 | - whether it's barcodes being scanned at stores, 611 | - 商店里扫条形码 \N 等红灯的自动驾驶汽车 612 | 613 | self-driving cars waiting at red lights, 614 | - 商店里扫条形码 \N 等红灯的自动驾驶汽车 615 | 616 | or snapchat filters superimposing mustaches. 617 | 或是 Snapchat 里添加胡子的滤镜 618 | 619 | And, the most exciting thing is that computer scientists are really just getting started, 620 | 令人兴奋的是 一切才刚刚开始 621 | 622 | enabled by recent advances in computing, like super fast GPUs. 623 | 最近的技术发展,比如超快的GPU,\N 会开启越来越多可能性 624 | 625 | Computers with human-like ability to see is going to totally change how we interact with them. 626 | 视觉能力达到人类水平的计算机 \N 会彻底改变交互方式 627 | 628 | Of course, it'd also be nice if they could hear and speak, 629 | 当然,如果计算机能听懂我们然后回话,就更好了 630 | 631 | which we'll discuss next week. I'll see you then. 632 | 我们下周讨论 到时见 633 | 634 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | extract_from_ass_subtitle/node_modules 2 | extract_from_ass_subtitle/package-lock.json 3 | *.DS_Store 4 | -------------------------------------------------------------------------------- /1. screenshot.py: -------------------------------------------------------------------------------- 1 | # Crash Course Computer Science 2 | # 这里的程序只是为了截图视频开头 00:00:00 的时间表 3 | # 最后的输出结果:40张 jpg 图片 4 | 5 | import os 6 | 7 | files = [f for f in os.listdir('.') if os.path.isfile(f)] 8 | for f in files: 9 | # 文件名举例: 10 | # 1. 计算机早期历史-Early Computing_BiliBili.mp4 11 | # 23. 屏幕&2D 图形显示-Screens&2D Graphics_BiliBili.mp4 12 | filename = f 13 | filename_array = filename.split('.') 14 | number = filename_array[0] # 拿 1 15 | suffix = filename_array[-1] # 拿 mp4 16 | if suffix == 'mp4': 17 | cmd = "ffmpeg -ss 00:00:00 -i '{0}' -vframes 1 -q:v 2 {1}.jpg".format(filename, number) 18 | # 截图第一帧图片# 19 | # {0} 是 input 文件名 20 | # {1} 是输出的 jpg 名 21 | os.system(cmd) # 执行 -------------------------------------------------------------------------------- /README-about-subtitle.md: -------------------------------------------------------------------------------- 1 | ## 1. 关于公开字幕的一些说明 2 | 公开字幕是为了方便大家学习 3 | 因为字幕是可以搜索的,而视频是没法搜索的 4 | 这里只公开了 txt 格式 5 | 6 | * `(字幕)全40集中英字幕文本.txt` 这个 txt 文件 7 | * `(字幕)全40集中英字幕文本` 这个文件夹 8 | 9 | 都是用 Node.js 脚本从 .ass 字幕文件中提取出来的 10 | 11 | ## 2. 我们不会公开中英双语 .ass 字幕文件 12 | 13 | 因为大部分人都没做过字幕,我解释一下,翻译视频时,字幕是一个 .ass 文件 14 | 里面有字幕的样式,大小,颜色,时间轴(字幕的出现时间和消失时间)等等,我们用来做字幕的软件是 Aegisub 15 | 如果对字幕翻译有兴趣,可以看我做的教程: 16 | 17 | [教程] 0基础入门字幕翻译(下载->翻译->压制, 教整个流程) @糖醋陈皮 制作 18 | https://www.bilibili.com/video/av23046009 19 | 20 | 21 | 而视频的压制则是把 .mp4 视频视频和 .ass 字幕文件通过软件压制一下(我们是用小丸工具箱) 22 | 得到最后的成品(就是你在 B 站上看的那个) 23 | 24 | 因为国内目前环境不太好,我们之前遭遇过被中国电信的天翼把视频盗过去收费的事情 25 | (请看如下的微博链接) 26 | https://weibo.com/5237129097/Fy61opQ2B?from=page_1005055237129097_profile&wvr=6&mod=weibotime 27 | https://weibo.com/5237129097/Fy5UT1qaf?from=page_1005055237129097_profile&wvr=6&mod=weibotime&type=comment#_rnd1528465493854 28 | https://weibo.com/5237129097/FyfXvdVp8?from=page_1005055237129097_profile&wvr=6&mod=weibotime&type=comment#_rnd1528465495773 29 | 30 | 而且,别忘了国内有很多垃圾自媒体(你在 B 站 App 的首页随便扫两下估计能看到3-4个) 31 | 所以如果把 .ass 文件放出来,对抄袭者来说成本就很低了,只需要把源视频下载一下,把 ass 字幕批量压制一下, 32 | 然后改个名字就可以说是自己的了,扯皮都扯不清楚。 33 | 34 | 所以 ass 字幕文件是不会放出来的。 35 | 36 | ## 3. 总结 37 | 为了方便大家学习 + 防止抄袭者拿走然后低成本就变成自己的视频,所以放 txt 格式的文件 38 | 这里写这一堆只是为了给没做过字幕的同学一些小科普 + 解释为什么这么做 39 | 40 | ## 4. 最后 41 | 参与字幕翻译的所有翻译君都是出于热爱 Crash Course,花费了大量个人时间免费翻译 42 | 希望大家好好学习~~ 43 | 让自己变得更厉害,让这个世界变得更美好 ;) 44 | Don't forget to be Awesome! 45 | -------------------------------------------------------------------------------- /extract_from_ass_subtitle/2. extract_head.js: -------------------------------------------------------------------------------- 1 | const assParser = require('ass-parser'); 2 | const fs = require('fs'); 3 | const common = require('./common.js'); 4 | const path = require('path') 5 | 6 | /* 7 | 2018-6-8 周五 03:42 8 | 9 | 1. 是什么: 10 | 从 40 个 .ass 字幕文件中提取片头文字(共2行字幕,字幕样式名统一) 11 | 仅针对 [计算机科学速成课] Crash Course Computer Science 12 | 13 | 2. 使用方法: 14 | node 2.\ extract_head.js 15 | 16 | 3. 备注: 17 | 所有40集 ass 字幕文件,要和这个脚本放到同一个目录下 18 | 文件命名必须类似:1. 计算机早期历史-Early Computing.ass 19 | 1. 开头 20 | .ass 结尾 21 | 因为我们要按顺序读取 22 | 23 | 代码是半夜2点写的,目的是快速 hack 出来, 24 | 所以有些代码不是最“高效”的(比如循环很多次)就不要吐槽啦,管用就好 25 | */ 26 | 27 | var result = '' // 存临时结果 28 | var final_result = ''; // 存最终结果 29 | 30 | var folder = '/Users/remote_edit/Desktop/ass/' 31 | 32 | // 循环当前目录下的 ass 文件,按 1,2,3,4...一直到 40 这样的顺序 33 | function loop_current_folder_ass_file_in_order(){ 34 | fs.readdir(folder, (err, files) => { 35 | for (var i = 1; i <= 40; i++) { // 40*40(files)=1600 loop, I know.. I know.. not the most ‘efficient’ code, but it's good enough 36 | files.forEach(file => { // file == 1. 计算机早期历史-Early Computing.ass 37 | var number = file.split('.')[0] // number == 1 38 | var ext = path.extname(file); // ext == ass 39 | if (parseInt(number) === i && ext === '.ass'){ 40 | extract_main_point(file, number); 41 | } 42 | }); 43 | } 44 | }) 45 | } 46 | 47 | // 抽取重点出来,那两行字幕的样式在全部40集中都是统一的,叫'备注 - 主题'和'这一集的名字' 48 | function extract_main_point(file, number){ 49 | var text = fs.readFileSync(file,'utf8') 50 | data = assParser(text); 51 | body = data[3]['body']; 52 | 53 | for (var i = 0; i <= body.length-1; i++) { 54 | var element = body[i]; 55 | if (element.key == 'Dialogue') { 56 | if (element.value.Style == '这一集的名字'){ 57 | var text = element.value.Text; 58 | text = common.remove_curly_brace_keep_text(text); 59 | // console.log(text); 60 | result = result + '## ' + text + '\n'; 61 | } 62 | } 63 | } 64 | 65 | for (var i = 0; i <= body.length-1; i++) { 66 | var element = body[i]; 67 | if (element.key == 'Dialogue') { 68 | if (element.value.Style == '备注 - 主题'){ 69 | var text = element.value.Text; 70 | text = common.remove_curly_brace_keep_text(text); 71 | // console.log(text); 72 | result = result + text.replace(/\\N/g, '\n') + '\n\n\n'; 73 | } 74 | } 75 | } 76 | 77 | 78 | // 如果到了最后一集 79 | if (number == 40){ 80 | // 清理两边空格(trim),然后末尾加俩空格作为 markdown 换行 81 | var line_array = result.split('\n'); 82 | line_array.forEach(l => { 83 | final_result = final_result + l.trim() + ' ' + '\n'; 84 | }) 85 | // 处理 HTML entity 86 | final_result = common.convertHTML(final_result); 87 | // 写文件 88 | fs.writeFile("CS_40episode_point.md", final_result, function(err) { 89 | if(err) { 90 | return console.log(err); 91 | } 92 | console.log("The file was saved!"); 93 | }); 94 | } 95 | } 96 | 97 | // ============= 98 | 99 | loop_current_folder_ass_file_in_order(); 100 | -------------------------------------------------------------------------------- /extract_from_ass_subtitle/3. extract_ass_to_txt.js: -------------------------------------------------------------------------------- 1 | // ass to txt 2 | // 不写注释了,参考 2. extract_head.js 即可 3 | 4 | const assParser = require('ass-parser'); 5 | const fs = require('fs'); 6 | const common = require('./common.js'); 7 | const path = require('path') 8 | 9 | var result = '' 10 | var folder = '/Users/remote_edit/Desktop/ass/' // 40 集字幕文件的文件夹 11 | 12 | function loop_current_folder_ass_file_in_order(){ 13 | fs.readdir(folder, (err, files) => { 14 | for (var i = 1; i <= 40; i++) { // 40*40(files)=1600 loop, I know.. I know.. not the most ‘efficient’ code, but it's good enough 15 | files.forEach(file => { // file == 1. 计算机早期历史-Early Computing.ass 16 | var number = file.split('.')[0] // number == 1 17 | var ext = path.extname(file); // ext == ass 18 | if (parseInt(number) === i && ext === '.ass'){ 19 | extract_main_point(folder+file, number); 20 | } 21 | }); 22 | } 23 | }) 24 | } 25 | 26 | 27 | function extract_main_point(file, number){ 28 | var text = fs.readFileSync(file,'utf8') 29 | data = assParser(text); 30 | body = data[3]['body']; 31 | 32 | for (var i = 0; i <= body.length-1; i++) { 33 | var element = body[i]; 34 | if (element.key == 'Dialogue') { 35 | if (element.value.Style == 'en - 白色'){ 36 | var text = element.value.Text; 37 | text = common.remove_curly_brace_keep_text(text); 38 | result = result + text.trim() + '\n'; 39 | } 40 | if (element.value.Style == 'zh - 黄色'){ 41 | var text = element.value.Text; 42 | text = common.remove_curly_brace_keep_text(text); 43 | result = result + text.trim() + '\n\n'; 44 | } 45 | } 46 | } 47 | 48 | // 写文件 49 | // if (number == 40){ 50 | fs.writeFile(file+'.txt', result, function(err) { 51 | if(err) { 52 | return console.log(err); 53 | } 54 | console.log("The file was saved!"); 55 | }); 56 | // } 57 | 58 | result = ''; 59 | } 60 | 61 | // ============= 62 | 63 | loop_current_folder_ass_file_in_order(); 64 | -------------------------------------------------------------------------------- /extract_from_ass_subtitle/README.md: -------------------------------------------------------------------------------- 1 | ## 这里是一些 Node.js 代码 2 | 3 | ## 用途 4 | 抽取 ass 字幕文件的信息 5 | 6 | ## 说明 7 | 针对计算机速成课的 ass 字幕 8 | 不通用(不能随便把什么 .ass 都拿来当输入),就是快速 hack 了一下写了点 node.js 代码把事情搞定 9 | 10 | Input: 40个 .ass 后缀的字幕文件 11 | Output: 看下面的说明 12 | 13 | `2. extract_head.js` 负责抽取40集的片头文字总结,放到一个文件中(结果:1个 .md 文件) 14 | `3. extract_ass_to_txt.js` 把 ass 转成 txt (结果:40个 .txt 文件) 15 | -------------------------------------------------------------------------------- /extract_from_ass_subtitle/common.js: -------------------------------------------------------------------------------- 1 | // Remove all {} 2 | // Input: "{\c&HCC9933&}Subtitles by {\c\c&HFFFFFF &}MemoryOnSmells{\c} {\c&HCC9933&}Exclusive for http://UKsubtitles.ru{\c}" 3 | // Output: "Subtitles by MemoryOnSmells Exclusive for http://UKsubtitles.ru" 4 | function remove_curly_brace_keep_text(str) { 5 | return str.replace(/\s*\{.*?\}\s*/g, ' ').trim(); 6 | } 7 | 8 | function convertHTML(str) { 9 | var entityPairs = [ 10 | {character: '&', html: '&'}, 11 | {character: '<', html: '<'}, 12 | {character: '>', html: '>'}, 13 | {character: "'", html: '''}, 14 | {character: '"', html: '"'}, 15 | ]; 16 | 17 | entityPairs.forEach(function(pair){ 18 | var reg = new RegExp(pair.character, 'g'); 19 | str = str.replace(reg, pair.html); 20 | }); 21 | return str; 22 | } 23 | 24 | exports.remove_curly_brace_keep_text = remove_curly_brace_keep_text 25 | exports.convertHTML = convertHTML 26 | -------------------------------------------------------------------------------- /extract_from_ass_subtitle/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cc_nodejs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ass-parser": "^0.2.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /image/cs40.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1c7/Crash-Course-Computer-Science-Chinese/9db1a3e0c5ae81a915ed5c2c1977c03261cf71a0/image/cs40.jpg -------------------------------------------------------------------------------- /image/english.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1c7/Crash-Course-Computer-Science-Chinese/9db1a3e0c5ae81a915ed5c2c1977c03261cf71a0/image/english.png -------------------------------------------------------------------------------- /image/qr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1c7/Crash-Course-Computer-Science-Chinese/9db1a3e0c5ae81a915ed5c2c1977c03261cf71a0/image/qr.png --------------------------------------------------------------------------------