0:00:00.000,0:00:06.615 All right. So it's such a beautiful spring[br]day, here, on campus. I'm gonna record 0:00:06.615,0:00:13.230 outside, here at Stamford campus. Just[br]kind of sunny, park like. So where college 0:00:13.230,0:00:18.455 students just come [inaudible]. So the[br]topic for this section is: What is 0:00:18.455,0:00:23.330 software? What is code? How is it that[br]something runs on a computer. So when we 0:00:23.330,0:00:28.584 talked about hardware in the computer we[br]talked about the cpu, that's the brains of 0:00:28.584,0:00:33.712 the thing. So the cpu's what actually does[br]running, and the cpu implements what is 0:00:33.712,0:00:38.650 called? Machine code instructions'. And[br]machine code instructions are extremely 0:00:38.650,0:00:43.968 simple. One machine code instruction might[br]add two numbers, another instruction might 0:00:43.968,0:00:48.784 compare two numbers to see which one is[br]bigger. So when. For code that we've seen, 0:00:48.784,0:00:52.936 so something like pixel set red dot ten.[br]That's much more complicated, than an 0:00:52.936,0:00:57.077 individual machine code instruction. So[br]the way that's going to work, is that 0:00:57.241,0:01:01.525 pixel set red dot ten. Will ultimately[br]expand to a series of maybe five or ten of 0:01:01.525,0:01:05.134 these very simple machine code[br]instructions. Such that, when those ten 0:01:05.134,0:01:09.528 instructions are run, one after the other,[br]it sorta has the effect of setting the red 0:01:09.528,0:01:13.660 value of the pixel to ten. Alright, and[br]I'll get into more detail later on about, 0:01:13.817,0:01:19.388 how that, how that expansion happens. So,[br]I think a good first question for running 0:01:19.388,0:01:24.744 a program is, what is a program? So I'll[br]look at the, the right hand side of this 0:01:24.744,0:01:29.447 diagram. So a program, and I'll, I'll sort[br]of use this as my running example, 0:01:29.630,0:01:34.760 Firefox. So a program is made actually of[br]just an enormous sequence of these very 0:01:34.760,0:01:39.523 simple machine code instructions. And so,[br]when Firefox is, is running on your 0:01:39.523,0:01:44.104 computer, that means somewhere in RAM,[br]there's a block of these instructions. 0:01:44.287,0:01:48.928 Such that running them, has, you know,[br]does the things that Firefox does. So it 0:01:48.928,0:01:53.753 blinks the cursor, it takes url, draws,[br]gets web pages and all that sort of stuff. 0:01:53.936,0:01:58.476 So. The individual instructions are really[br]trivial, right. Just add two numbers. So 0:01:58.476,0:02:02.532 how does it get from that to like blinking[br]the cursor? And the best I can say is it's 0:02:02.532,0:02:06.015 maybe sort of like the relationship[br]between sand and sculpture. That each 0:02:06.015,0:02:09.975 instruction is like a grain of sand. Like[br]by itself it's meaningless and it kind of 0:02:09.975,0:02:13.983 looks like all the others. But if you put[br]them together in just the right way, you 0:02:13.983,0:02:17.466 can build this complicated overall[br]structure. And so that is the way that 0:02:17.466,0:02:21.685 Firefox is built out of these, this these[br]simple instructions. So the way it works 0:02:21.685,0:02:25.838 is the CPU, over here on the left, runs[br]what is called a fetch execute cycle. And 0:02:25.838,0:02:30.144 all that means is that, the CPU will start[br]off, let's say here, with Instruction one, 0:02:30.144,0:02:34.095 and it'll load that instruction and run[br]it. Or sometimes we'll say it executes 0:02:34.095,0:02:37.691 that instruction. So, it, it adds the two[br]numbers, or it does whatever the 0:02:37.691,0:02:41.642 instruction says. And when it, it's done[br]with Instruction one, it just goes down 0:02:41.642,0:02:45.843 the list. It goes to Instruction two, and[br]it does that one. Instructions for you, it 0:02:45.843,0:02:50.495 does that one, it's only just ru-, it just[br]runs through the sequence. So, when we say 0:02:50.495,0:02:55.146 that a CPU operates at two gigahertz, two[br]billion operations per second, it's these 0:02:55.146,0:03:00.109 little instructions that, that refers to.[br]So there are, there's obviously a, a big 0:03:00.109,0:03:05.203 variety of instructions. But I'm just[br]gonna point out a couple special types. So 0:03:05.203,0:03:10.483 one type of instruction has the effect of[br]changing the order that the instruction. 0:03:10.483,0:03:15.267 Normally the CPU just runs down the list[br]and does them in order. But let?s say 0:03:15.267,0:03:20.361 instruction four maybe says, oh jump back[br]and start executing again at instruction 0:03:20.361,0:03:24.304 one. And so think about what the cpu's[br]gonna do. So it's gonna do instructions... 0:03:24.304,0:03:27.856 One, two, three, four. And then when it[br]gets to four, it'll sorta jump back, and 0:03:27.856,0:03:31.740 do instructions one, two, three again. And[br]then one, two, three again, and again. You 0:03:31.740,0:03:35.008 can sorta see that's how loops are[br]implemented. Just arranging the 0:03:35.008,0:03:38.797 instructions so that they'res some piece[br]of code we wanna do 500,000 times. Well, 0:03:38.797,0:03:42.728 you can set up an instruction to just loop[br]back and so, do those instructions again 0:03:42.728,0:03:47.408 and again. Another sort of instruction.[br]Our [inaudible] idea is an instruction, 0:03:47.408,0:03:53.235 well, the same instruction two here, which[br]tests some condition and if the condition 0:03:53.235,0:03:58.847 is true maybe it skips ahead where[br]instruction five would be here. So. That's 0:03:58.847,0:04:02.316 how if statements are implemented. You[br]have an instruction that's gonna look at 0:04:02.316,0:04:05.654 some condition, and if it's, if the[br]condition is true, it's gonna sort of tell 0:04:05.654,0:04:08.991 the CPU to go over here. And if it's[br]false, it'll go over to some other place. 0:04:09.123,0:04:12.856 So by arranging the instructions just so,[br]you can get the effect of, something that 0:04:12.856,0:04:18.904 we, that, in our code, looks like an if[br]statement. [sound]. Alrighty. So Well so 0:04:18.904,0:04:24.053 how, how does a program get running and[br]how do we get to, how do we get to this 0:04:24.053,0:04:28.828 thing. So I would imagine you've got,[br]Firefox on your flash drive, you know, the 0:04:28.828,0:04:33.435 file. So on your flash drive, or on your[br]hard drive. So here, I've stored, long 0:04:33.435,0:04:37.814 term, you know, persistent storage down[br]here. And here's RAM and here's the CPU. 0:04:37.814,0:04:42.184 So when you've got Firefox on your hard[br]drive. It's, it's a file, basically, and 0:04:42.184,0:04:46.429 here I, it's called Firefox exe, that's[br]just a windows convention, for, how to 0:04:46.429,0:04:50.674 name a file, which is a program, but it,[br]it helps keep things clearer so I'll 0:04:50.674,0:04:55.485 follow that. So firefox.exe, that file, it[br]has a lot of bytes in it, and for the most 0:04:55.485,0:04:59.334 part, those bytes, are just the[br]instructions, that make up the program, 0:04:59.334,0:05:03.717 plus some icons. [inaudible] and other[br]stuff. So what I want to think about is 0:05:03.717,0:05:08.163 well, what happens when you double click[br]Firefox.exc? How does it start running? 0:05:08.163,0:05:12.951 And basically what happens is there's two[br]steps. The first thing that happens is the 0:05:12.951,0:05:17.512 bytes for these instructions, at least[br]enough of the instructions to get started, 0:05:17.512,0:05:22.186 get copied up to RAM. So it just copies,[br]puts the instructions in RAM where the CPU 0:05:22.186,0:05:26.119 can get at them. And then step two, just[br]tell the CPU, 'Okay, well, here's 0:05:26.119,0:05:30.622 instruction one. Start executing here.'[br]And so then the CPU just starts ripping 0:05:30.622,0:05:35.257 down the series of instructions and now[br]it's running Firefox. So I think this 0:05:35.257,0:05:40.191 leads to the question of like, well who do[br]this right? How did, how did, who handles 0:05:40.191,0:05:45.336 the double click? Who gets Firefox[br]running? And so that's what an operating 0:05:45.336,0:05:50.731 system is. So the operating system is a[br]set of kind of supervisory and 0:05:50.731,0:05:56.718 administrative programs that sort of you[br]know, organize, organize the whole system. 0:05:56.940,0:06:01.274 So, in particular, the operating system[br]manages multiple programs, and starting 0:06:01.274,0:06:06.059 and, ending programs. So a modern computer[br]can, run multiple programs at the same 0:06:06.059,0:06:10.732 time and so the operating system sorta[br]keeps things organized. So it, it does the 0:06:10.732,0:06:15.066 initial startup of a program, giving it[br]some RAM to use, and maybe giving it a 0:06:15.066,0:06:19.126 window to draw in. Also, operating systems[br]try to keep programs isolated from each 0:06:19.126,0:06:22.864 other. So each program has its RAM, but it[br]can't necessarily just go mess with the 0:06:22.864,0:06:26.602 RAM of some other program. So that helps[br]if a program has bugs. Hopefully, it'll 0:06:26.602,0:06:30.479 just mess up that program, but not mess up[br]some other program that you're running. Or 0:06:30.479,0:06:34.310 maliciously, if I program was trying to do[br]something bad to some other program that, 0:06:34.448,0:06:38.080 the operating system tries to keep them[br]separate. So, the operating system is 0:06:38.080,0:06:42.099 really the first thing that runs when your[br]computer starts up. So that's what's gonna 0:06:42.099,0:06:45.754 put up those first windows, and maybe show[br]you what's on the hard drive. And then, 0:06:45.754,0:06:49.454 from thereafter, you can double click, or[br]do other things, and the operating system 0:06:49.454,0:06:52.742 will start those programs for you. So[br]that's really the, the thing you're 0:06:52.742,0:06:56.292 seeing, when you start up your laptop.[br]Digital camera, it, you don't think of it 0:06:56.292,0:06:59.297 as a computer, necessarily, but it's[br]really the same. When the digital camera 0:06:59.297,0:07:02.582 first starts up, there's probably a couple[br]programs. There's maybe the program that 0:07:02.582,0:07:05.707 takes pictures, and the program that lets[br]you look through the pictures you've 0:07:05.707,0:07:08.792 already taken. And so, when the camera[br]first starts out, there's a little bit of 0:07:08.792,0:07:11.917 administrative, sorta get, get the, the[br]programs running to sort of show you the 0:07:11.917,0:07:17.467 interface, and then it just lets you go.[br]Am. So just as a, a last thing, there's 0:07:17.467,0:07:21.344 sort of the, the, these, this terminology[br]boot and reboot, I always thought that was 0:07:21.344,0:07:25.077 kinda funny. Like, where does that come[br]from? And I-, it refers to the sort of 0:07:25.077,0:07:28.906 chicken egg problem of, like, well, when[br]the computer first turns on, when it first 0:07:28.906,0:07:31.985 starts up, how does it. How does it get[br]itself organized? How does it start 0:07:31.985,0:07:36.058 running a program? And this terminology[br]actually comes from sort of an old joke, 0:07:36.058,0:07:40.150 about if you want to get over a fence,[br]what you could just do is reach down to 0:07:40.150,0:07:44.138 your bootstraps, and pull up, and just[br]like lift yourself over the fence that 0:07:44.138,0:07:48.336 way. And it's obviously kind of absurd.[br]But it, it has the same quality of like, 0:07:48.336,0:07:52.271 well wait, what program does the computer[br]run to enable the computer to run 0:07:52.271,0:07:57.257 programs. So it sort of a chicken and egg[br]problem. So the way it works is that the 0:07:57.257,0:08:02.416 hardware, the, the CPU, has, when it[br]recognizes that it has, it was turned off, 0:08:02.416,0:08:07.898 and that it has, it has just started up.[br]And so there's a special tiny program that 0:08:07.898,0:08:13.203 is hardwired into the computer to run, at[br]that moment. And that program can maybe 0:08:13.203,0:08:17.551 check, check what's hardware area, you[br]know, do some basic early start-up stuff. 0:08:17.551,0:08:22.237 And then pretty much it looks around for a[br]hard disk or a flash drive or something 0:08:22.237,0:08:26.584 that contains an operating system on it.[br]And then it can start that operating 0:08:26.584,0:08:31.157 system and then, then the computer can[br]start up in that way. So that's why it's 0:08:31.157,0:08:35.900 called boot up. It refers to this old boot[br]strap idea. And then that's what rebooting 0:08:35.900,0:08:40.473 is. So reboot means just to sort of, we[br]want to get a clean slate so it's going to 0:08:40.473,0:08:44.320 shut down and start up fresh. All right.[br]So now you know.