WEBVTT 1 00:00:02.250 --> 00:00:08.940 William Cheng: Welcome to lectures seven so warm up to his do a next to 2 00:00:11.040 --> 00:00:18.870 William Cheng: Sorry, we are warm up Tuesday next Tuesday. If you have co from previous semester, don't look at them. Don't copy that best to get rid of it. 3 00:00:19.380 --> 00:00:24.660 William Cheng: Grading guidelines, they always will grade and we can all agree on 32 bill bunch of 16.04 4 00:00:25.200 --> 00:00:33.450 William Cheng: If we make a submission read and understand the ticket in the email and also the web page and make sure you follow the verify your submission procedure. 5 00:00:34.200 --> 00:00:41.220 William Cheng: Some people didn't do that in warm up one and you end up losing a bunch of points. So again, you know, when we grade. We have to follow the grading guidelines. 6 00:00:41.550 --> 00:00:45.900 William Cheng: If you didn't, verify your submission, you end up losing a lot of points. There's nothing I can do. 7 00:00:46.260 --> 00:00:56.370 William Cheng: Okay, so. So again, I strongly recommend you to follow that procedure, again, verify your submission, meaning that you need to follow all the steps and go through the grading guidelines, step by step. 8 00:00:56.880 --> 00:01:02.940 William Cheng: Okay, so if you make some obvious mistake that's the time to fix it and make a recent mission now. All right. 9 00:01:05.400 --> 00:01:12.810 William Cheng: The tentative timeline for warm up to is that, you know, so your simulation should work by the end of, you know, today. 10 00:01:13.290 --> 00:01:23.250 William Cheng: You know, you need to produce a consistent chase output. Make sure you only use one new tax law and conditioning variable. Make sure the you know the servers are busy when they're supposed to. 11 00:01:24.210 --> 00:01:28.560 William Cheng: So you should also you segue to catch control in so after you're done with that part. 12 00:01:28.980 --> 00:01:34.920 William Cheng: And then when Sagan returns you should lock to be texts that global variable cancer, both the rival thread. 13 00:01:35.280 --> 00:01:40.890 William Cheng: And then broadcast the condition variable unlocking new tags and then self terminate. As I mentioned that before. 14 00:01:41.400 --> 00:01:52.620 William Cheng: You need to run analyze chase and make sure everything matches. So again, some people asked me about precision. I mean, you can can see that the Analyze trades give you a lot of digits, you know, so it's too many digits. 15 00:01:53.580 --> 00:01:59.910 William Cheng: You know, so, so, so therefore you know your print out needs to satisfy the requirements inside the 16 00:02:00.360 --> 00:02:08.430 William Cheng: Requirement inside the spec. Okay, so your output shouldn't really match the printer of analyze chase okay so electrics give you, you know, more digits. 17 00:02:08.970 --> 00:02:20.280 William Cheng: Of rivals there myself terminate or whatever they're supposed to terminate. So again, you need to figure out when they're they're supposed to terminate. If you read a great guy. And it was a they hang around too long. While you're gonna end up losing points. Yeah. 18 00:02:21.570 --> 00:02:28.920 William Cheng: The server third must finish transmitting the current package. That's the deal. You know when you press Control. See, you're not allowed to kill the server threat. Yeah. 19 00:02:29.490 --> 00:02:46.080 William Cheng: And also, if you have committed to Colonel team, you know, send me the required information by following on the on the project web page. If you look at the the colonel team section, it will tell you what is the bird, the procedure for you to send your team information to me. Yeah. 20 00:02:48.690 --> 00:02:59.340 William Cheng: All right. And you know this Sunday. You know, I will send you your kernel source code. So it will be a couple of days before the end of warm up to. So in case that you finished. 21 00:02:59.640 --> 00:03:06.960 William Cheng: So in case you finish warm up too early, you can start doing your kernel assignment that the kernel source code, you know, I'm going to send you 22 00:03:09.780 --> 00:03:18.540 William Cheng: Know, send it, send it to you instead of email. So inside the email. There's a link, you need to click on the link to download your individually individualized kernel source code. 23 00:03:19.050 --> 00:03:24.570 William Cheng: Okay, so everybody's cornhusker source code will be a little bit different, you know, but they're pretty much the same thing. 24 00:03:25.260 --> 00:03:27.900 William Cheng: But, but there's a signature. You know that's different for everyone. 25 00:03:28.560 --> 00:03:37.410 William Cheng: So, so we're going to refer to this source code as the pristine kernel source. Okay, so this is the one that you start with you need to modify code in there as well. Again, you need to read the spec, you know, 26 00:03:37.740 --> 00:03:40.470 William Cheng: Read, read all the instructions of how to do that again. 27 00:03:41.130 --> 00:03:49.200 William Cheng: Your team must start with one of the teammates that one of the team members pristine kernel source code. You get to choose which teammate, you want to start with. It can be any one of them. 28 00:03:49.740 --> 00:03:56.190 William Cheng: Okay, so once you do that you should destroy all the other the other teammates CO and only start with one version. Yeah. 29 00:03:56.700 --> 00:04:03.000 William Cheng: You should also save a copy of the procedures or local source code because later on he will you try to verify your kernel submission 30 00:04:03.600 --> 00:04:06.780 William Cheng: The assumption over here is that you're going to start with a pristine kernel source. 31 00:04:07.200 --> 00:04:18.090 William Cheng: OK. So again, everyone you know so away. You know, if you if you have a team of four throwaway three a team members co star with one person and then make sure everybody's in sync that 32 00:04:18.780 --> 00:04:23.880 William Cheng: Alright, so, so, so, so, and also, you know, from the pristine kernel source. 33 00:04:24.480 --> 00:04:35.520 William Cheng: You got to be very careful if you want to delete something from the precinct controversial because some people has this weird habit of whenever they get to get some source code. They want to change everything to be the way they want things 34 00:04:36.060 --> 00:04:45.330 William Cheng: Okay, so, so it's fine point. What, what if we want to do that. But again, you got to read what you're deleting if if the comment box. So if you delete this line. If you delete. 35 00:04:45.930 --> 00:04:55.830 William Cheng: This comment block, you're going to lose 20 points. Okay. Guess what happened Ii Ii Ii Ii if you delete a comment, blah. Well, we have to deduct 20 points. 36 00:04:56.820 --> 00:05:06.750 William Cheng: Okay, so be very careful what you delete in a pristine kernel source. Okay. The best thing is to do is leave it on touch but if you have to have the habit of changing everything. Watch out for your changing 37 00:05:07.080 --> 00:05:12.330 William Cheng: Okay, some people common law even say that you are not allowed to even change it if you change, you're going to lose 20 points. 38 00:05:13.200 --> 00:05:21.600 William Cheng: Okay, so, so, so really what you're deleting there's hope. I make this very, very clear because some people always says, oh, you know, I accidentally delete this. 39 00:05:21.990 --> 00:05:29.100 William Cheng: If you delete them. You know what you said you know what, when you verify your bill your submission, you need to go through every, you know, every one of these things that you have changed. 40 00:05:29.310 --> 00:05:42.030 William Cheng: And make sure you didn't delete anything that you're not supposed to delete. Okay, it's your responsibility. Okay, so, so, so I made it very, very clear. And if you didn't accidentally delete something. There's nothing I can do. Yeah. 41 00:05:43.770 --> 00:05:54.570 William Cheng: All right. Alright, you know the the link contains user ID and password. So maybe also, you shouldn't really be that email because you know some people accidentally wipe out there a pristine kernel source go you want to download again. 42 00:05:54.990 --> 00:05:57.840 William Cheng: You can use the user ID and password and the link right so don't be that email. 43 00:05:58.740 --> 00:06:07.260 William Cheng: Okay, so once you get your current source code, you are not expected to be able to do Colonel one yet because we haven't catch up with the lecture material. So, 44 00:06:07.800 --> 00:06:16.410 William Cheng: So, so, you know, you know, this Friday, I will give you an introduction to the colonel assignment during discussion section by Thursday next week. So a week. 45 00:06:17.280 --> 00:06:22.110 William Cheng: You know, a week from this lecture, you will have everything that you need to finish Colonel one 46 00:06:22.590 --> 00:06:30.240 William Cheng: Okay. So, current one is going to start on officially on a Tuesday. So by Thursday of that week you will have everything that you need to know in order for you to finish Colonel one 47 00:06:30.660 --> 00:06:35.280 William Cheng: Okay, I'm still so there's a lot of a lot of code to be written, but at least I'm done with the lecture part 48 00:06:35.610 --> 00:06:41.280 William Cheng: Yeah. Alright. So if you're done with a little warm up to over here, free, feel free to start, but since you don't know what to do yet. 49 00:06:41.730 --> 00:06:48.720 William Cheng: What should you be doing right you should read the spec read something called a weenie documentation. So our current. Oh, Sam is no as to why Unix operating system. 50 00:06:49.080 --> 00:06:52.590 William Cheng: Okay, it's a Unix, Linux, like operating system that you have to implement 51 00:06:53.040 --> 00:06:59.730 William Cheng: Okay, so what happened is that the Brown University, they implement disappearances them and then they punch a bunch of holes into it by deleting a bunch of code. 52 00:07:00.330 --> 00:07:10.290 William Cheng: Okay. So your job is to put all those co back. Okay, so that's your programming assignment. Alright, so, so, so, so there's an introduction introductory document. It's a PDF file. 53 00:07:10.860 --> 00:07:16.770 William Cheng: It's inside the colonel spec. So you should read it. It's a really long document, you need to read what you need. 54 00:07:17.070 --> 00:07:21.300 William Cheng: You don't have to read. Everything's okay. You should we start reading it, you'll find out. Oh, I don't need that only that 55 00:07:21.660 --> 00:07:28.320 William Cheng: So in the end, you don't have to read everything now. All right. And you also need to follow the instruction in the spec sort of try things out. 56 00:07:28.710 --> 00:07:34.680 William Cheng: Make sure everything looks like what's in the spec. As soon as you find out there's something wrong, let me know right away. Yeah. 57 00:07:35.190 --> 00:07:45.360 William Cheng: And also, you know, some people doesn't, you know, install the wrong version of Ubuntu 16.4. So again, I want you already installed the wrong. We want to see two point O for your kernel assignment will not run on that. 58 00:07:45.840 --> 00:07:51.150 William Cheng: Okay, so if you are doing something wrong. Well, then guess what you have to reinstall 116 point four now. 59 00:07:51.600 --> 00:08:00.750 William Cheng: Right. You also should start debugging other the colonel with GDP and make sure everything looks like what it says in the spec. Okay, so it's the spec will give you some example. So again, follow the example there. 60 00:08:01.500 --> 00:08:10.680 William Cheng: So so so before you know what to do. You need to get familiar with the kernel source go by looking around and stuff like that and you will realize that the kernel source code is very, very huge 61 00:08:11.310 --> 00:08:20.400 William Cheng: Okay. Alright, so, so, so, so, so please understand that there's no time for you to understand everything right. Some people really try to understand everything. It's impossible. Just give up right now. 62 00:08:20.910 --> 00:08:27.390 William Cheng: Okay, so. So what you need to do you need to learn how to do your part by assuming that everything else work perfectly. 63 00:08:27.840 --> 00:08:35.850 William Cheng: Okay. So for example, you don't know exactly how prenup work but you trust that it works, but you also you don't know how read and write, or love those kind of words but you sort of trust that the ordinances and 64 00:08:36.300 --> 00:08:42.810 William Cheng: Make sure that they work. Right. So you're going to do the same thing over here in the kernel Simon You assume everything else that you don't understand, they are all perfect 65 00:08:43.530 --> 00:08:46.920 William Cheng: Okay, and then you need to sort of figure out what do you have to do. And then you only do that part. 66 00:08:47.190 --> 00:08:53.220 William Cheng: Yeah, or so feel free to ask me a question about Colonel one assuming that you have read the spec and the meanings documentation. 67 00:08:53.490 --> 00:09:01.980 William Cheng: Okay. Another thing that's very important to do read is that is that the Colonel, why has the colonel one FAQ. You should also read the corner. Why FAQ, even though it wouldn't make too much sense. 68 00:09:02.220 --> 00:09:06.540 William Cheng: But you should glance through it because later on. You might need all the material there now. 69 00:09:07.290 --> 00:09:13.230 William Cheng: Right. And keep in mind that started with a kernel assignment neither I nor the TA will tell you what co to right 70 00:09:13.620 --> 00:09:21.210 William Cheng: Okay, if you see a data structure if you say, Why should I initialize this data structure to be you're asking us to tell you what code right and then we won't be able to tell you that 71 00:09:21.630 --> 00:09:27.810 William Cheng: OK. So again, if you want to, you know, sort of one line of code or something like that, feel free to ask that in the class Google group. 72 00:09:28.080 --> 00:09:35.580 William Cheng: Guys, or maybe your classmate. Okay, try to answer you, but please make sure that you don't share code or pseudo code inside in class will go through 73 00:09:35.940 --> 00:09:46.230 William Cheng: Okay, if you want to explain something explaining something very, very high level terms don't give away code. Okay. Because giving away code or pseudo code that will be considered cheating, right. 74 00:09:47.550 --> 00:09:56.130 William Cheng: Okay, so we're gonna go back to channel to chapter three. So last time we're here. We talked about when I talk about these, are these assembly code. 75 00:09:56.640 --> 00:10:04.290 William Cheng: So we are you know the function over here is may right and we have a stack waiting for me. So right now, at this point where at the beginning of May over here. 76 00:10:04.680 --> 00:10:07.110 William Cheng: So in this case, you know, the startup functioning, just call me. 77 00:10:07.800 --> 00:10:14.310 William Cheng: OK. So, again, in the last lecture was sort of talk about, you know, the you know the shuttle function will be building the the bottom two 78 00:10:14.730 --> 00:10:24.900 William Cheng: Things instead of stack rank. Right. The bottom two is the function argument and then followed by a IP. So therefore, these things are inside the stack room already. And now we need to finish building up the stack. Right. 79 00:10:25.200 --> 00:10:33.240 William Cheng: Okay, why do we need to finish booting up the stack rank, right, because we are not allowed to execute any C code unless we have a solid stack rank. 80 00:10:34.020 --> 00:10:38.760 William Cheng: OK. So again, we need to finish building out a staggering over here. So there are three things to build right so we're going to see how that's done. 81 00:10:39.000 --> 00:10:47.520 William Cheng: And then somewhere in the middle of the code over here. We're going to call some function. So we call the stuff I'm sure we're going to build the bottom two things that, aside from the the sacrifice for the cell function. 82 00:10:47.760 --> 00:10:53.370 William Cheng: Right. And then we're going to transfer control into the self absorbed by by by making a function call. 83 00:10:54.000 --> 00:11:02.430 William Cheng: Okay, and then we're going to sort of switch and look at a different set of assembly code to see how the cell phones, your worth. And eventually, when the sub return is going to return. Right. You know, to the next instruction. 84 00:11:02.610 --> 00:11:07.800 William Cheng: And they're going to continue to execute the code over here eventually when we're done, we need to return back to the startup function. 85 00:11:08.430 --> 00:11:12.960 William Cheng: Okay, so now we're going to focus in the main function we're going to see how you finish early how to stack rank. 86 00:11:13.200 --> 00:11:19.290 William Cheng: And then will you call the cell function. How do you build the beginning part of the stack right and then transfer control to the cell function. 87 00:11:19.500 --> 00:11:24.840 William Cheng: And then when the cell phones in return. We're going to tear down the stack rank that we have built over here. And then we're going to go back 88 00:11:25.260 --> 00:11:34.530 William Cheng: To to to run sequel. And when we are, you know, at the end of this function over here. When we return, we need to tear down all the stuff that were built. WE NEED TO RETURN RETURN RETURN TO THE starter function. 89 00:11:35.340 --> 00:11:42.720 William Cheng: Yeah. So again, the message over here is that we need to see when you make function call. How do you save the contacts and then build the context for next function. 90 00:11:42.930 --> 00:11:49.800 William Cheng: And then we're going to transfer control to the next function and then when the next function finish. We're going to, we're going to restore the contact and go back to what we were doing before. 91 00:11:50.550 --> 00:11:53.970 William Cheng: Yeah. Alright, so let's get going over here. 92 00:11:54.390 --> 00:12:07.080 William Cheng: So, so at this point we're at the beginning of the main function and then again the red arrow over here is the IP right so on. So here's our CPU over here CPUs right here inside the CPU tip register point to the red arrow. 93 00:12:08.070 --> 00:12:16.470 William Cheng: Okay, so instead of CPU. There are other register others ESP pointer. So at this point, at the entry point of may ESP is pointing right here. And we know that right people. 94 00:12:16.860 --> 00:12:21.510 William Cheng: Below ESP over here. That's the return address and below the return address over here is function argument. 95 00:12:21.780 --> 00:12:30.630 William Cheng: In our example over here, there is no function argument. So therefore, again, there's only IP there. What about EDP registrations that CPU right he BP pointed the next stack frame. 96 00:12:30.900 --> 00:12:41.550 William Cheng: This. So this is our main. This is the second for our main function, the stack rank. Below is the startup function right here is a stagnant with a star for to start a function. So this one point into the middle of that stack frame. 97 00:12:42.660 --> 00:12:53.220 William Cheng: Okay, so. So again, you know, all the staff room are linked together, you know, using it in a single link lives using the next pointer right and the head of the link list is stored inside the BP register inside the CPU. 98 00:12:54.000 --> 00:13:03.780 William Cheng: That as we mentioned before in what I'd give you sort of a quick review of computer hardware. I mentioned that for the x86 CPU. The three most important registers are 99 00:13:03.990 --> 00:13:10.950 William Cheng: The IP ESPN. A BP Raza we're going to switch it we're going to start looking at these register over and over again. All the way until chapter seven. 100 00:13:12.690 --> 00:13:23.130 William Cheng: Alright, so we're right here, over here, we're going to sort of execute the beginning part of main over here. So in the beginning part of name before we execute any sequel, we need to finish setting up the stack. Right. 101 00:13:24.120 --> 00:13:31.380 William Cheng: Now, so, so, so he has plenty right here. We need to you know put three things there instead of stack rank. So we're going to see how this is done. Okay. 102 00:13:31.560 --> 00:13:36.930 William Cheng: All right, the first instruction over here is called push LBP right so I'm going to explain some of the machine instructions. They'll be here. 103 00:13:37.410 --> 00:13:43.950 William Cheng: Push out MVP. So what happened is that L over here in for Intel L means is a four by number 104 00:13:44.700 --> 00:13:50.430 William Cheng: Okay, so there's something called as as a short short is to buy number and then there's a before by that's a single bite. 105 00:13:50.700 --> 00:14:00.180 William Cheng: Whenever you see an arrow. That means a four by number. So in this case, we're going to push the content of the MVP register by push it onto the stack. So how do you push something onto the stack. 106 00:14:00.840 --> 00:14:12.660 William Cheng: There's a way to do it is that ESPN one right here, ESPN point to the top of the stack over here. So what I need to do is I need to decorate USP by for why buy for what because over here I'm pushing for bites onto the stack. 107 00:14:13.260 --> 00:14:22.320 William Cheng: Okay, so there was a detriment ESP by four, two point right here. And then we're going to take the content of the MVP over here. I'm going to store it at the pointer that's pointed to buy ESP 108 00:14:23.580 --> 00:14:32.340 William Cheng: Okay. So, therefore I'm going to take the content of PvP over here. I'm going to copyright here. So when I finished doing that the content over here will be exactly what's inside the EDP register. 109 00:14:33.270 --> 00:14:46.800 William Cheng: Okay, so since he BP is appointed when you copy point or whatever, when you copy pointer. They point to the same place. Okay, so when you finish doing that this EP over here. Get the picture that we chose that will draw the pointer that point to exactly the same place as CBP. 110 00:14:48.000 --> 00:15:00.990 William Cheng: Right. Alright, so I'm going to go to the next picture over here. We'll finish doing that. Right. So inside. Over here we store the continent up, even though I write a BP. We know ed piece a pointer. So this point of what is the point to a point to the same place as a VP register. 111 00:15:02.790 --> 00:15:10.380 William Cheng: Alright, so that's what that's what the first machine instruction do right you pre detriment ESP by four and you copy the content of EDP register instead of CPU. 112 00:15:10.620 --> 00:15:15.300 William Cheng: Into where where the stack pointer is pointing right that's called pushing something onto the stack. Okay. 113 00:15:15.900 --> 00:15:18.840 William Cheng: Alright, the next instruction of yours has moved out ESP to a VP. 114 00:15:19.050 --> 00:15:24.870 William Cheng: For Intel whenever you have a moving structure is a copy instruction from the first argument to the second argument. 115 00:15:24.990 --> 00:15:36.660 William Cheng: So I'm going to copy data from here to here, right. The first argument over here is the ESP register and the second argument here is the EDP register. So we're going to copy the content of the ESP register into the EDP register. 116 00:15:37.050 --> 00:15:46.170 William Cheng: There so ESP register over here. Now it's a pointer that point over here if we copy into a BP over here again when you copy pointer that end up pointing at the same place. 117 00:15:46.410 --> 00:15:53.430 William Cheng: Right, so if you copy the content of ESP over into a BP BP no longer points. Your EP or point exactly where ESP is pointing 118 00:15:54.480 --> 00:15:57.090 William Cheng: Okay, so therefore, when you're done over here, they will look like this. 119 00:15:57.390 --> 00:16:10.320 William Cheng: Okay so EDP and ESP is going to point to the same place. And what is this CBP point right so the place they're pointing to contain a pointer to point to where you BVP used to point to right. So you're used to point right here. So, do a part right here. 120 00:16:11.850 --> 00:16:18.840 William Cheng: Okay, so now I'm going to claim that at this point all the staff frames are linked together. Okay, and the head of this 121 00:16:20.370 --> 00:16:28.140 William Cheng: And have that linked lists. So remember all the sacrum are linked together right in the head of the link inside the VP register. So now we have achieved that. 122 00:16:28.590 --> 00:16:33.390 William Cheng: Okay, so here is the stockroom for me. Right. We're in the main function over here. Here's a staggering for me. Right. 123 00:16:33.540 --> 00:16:42.390 William Cheng: And below it is a stagnant water for the startup function where the startup functions is right here. So now inside CPU yes ep, ep point or point to the top stack rank. 124 00:16:42.570 --> 00:16:50.040 William Cheng: And the top saccharine. The next point over here, points to the next one. And what it where it's pointing to. There's gonna be another next point, or they'll point to the next one. The next one. 125 00:16:51.150 --> 00:17:00.000 William Cheng: Okay, so after you execute these two machines instruction. Okay, all the staff Remote link together and the head of the link this points, you know, put points to the top stack right 126 00:17:01.590 --> 00:17:09.600 William Cheng: Alright, so you can see that the you know the machine instructions actually is pretty powerful. Okay, we can actually increase the length of the link list using to machine structures now. 127 00:17:10.410 --> 00:17:19.260 William Cheng: Alright, the next instruction over here. It's got pushed out. Yes. So again, L over here means four bytes ASI it's another registering inside the CPU. What does he do 128 00:17:19.620 --> 00:17:23.580 William Cheng: Don't worry about it. Okay. Just another registry. Is that a CPU, because what happened is that 129 00:17:23.760 --> 00:17:32.700 William Cheng: The main function over here. We're going to mess up the register e si N e di whatever they do it doesn't really matter what we need to do is that we need to say these register, we need to save the contracts. 130 00:17:32.880 --> 00:17:39.870 William Cheng: And they don't before we returned back to the main function, we need to restore all these register value. So this way, our startup function will not be surprised. 131 00:17:40.260 --> 00:17:49.380 William Cheng: Okay. So inside the CPU. There's an ESRI register, whatever the value. It is what you're going to save it into register. So again, how do we say that on to the staff. Well, in this case, we're going to 132 00:17:50.070 --> 00:18:01.170 William Cheng: Use the push L machine, judging by detriment ESP by four right and then copy this value over here into the stag over here, right. So that's push out yes I so we're done, they will look like this. 133 00:18:01.890 --> 00:18:09.750 William Cheng: Okay. So down here in the stock room over here. It's going to be the value of yes I registered, right, so what's inside. Yes. Is it a pointer. Is it a value. I have no idea. 134 00:18:09.960 --> 00:18:18.030 William Cheng: Okay, it doesn't matter. Right. We say the context. Then the next instruction over here, we also need to say VDI so again inside of CPU. There's an EDM register. 135 00:18:18.210 --> 00:18:26.250 William Cheng: We're going to again, push it onto the stack by Decker many BP by ESP by four and then copy this value right here. So we're finished doing that it will look like this. 136 00:18:26.580 --> 00:18:34.620 William Cheng: Guy. So in this case, the same register over here. How big is that area. The area is going to be a bites. Right. The first one over here is going to be e di and then followed by yes I 137 00:18:35.130 --> 00:18:40.170 William Cheng: Guess so. These are the same register value. Later on, we need to make sure we restore them that. All right. 138 00:18:40.710 --> 00:18:47.070 William Cheng: Then we need to create a space for a local variable. So in this example, how many bytes. Do we need we have for integers. Right. So over here. 139 00:18:47.580 --> 00:18:58.170 William Cheng: Is for buyers. As for buyers over here. So all we're going to do is that we're going to create space for these local variable. Remember local variables are not initialize unless you write code to initialize them explicitly 140 00:18:58.650 --> 00:19:11.430 William Cheng: Yeah. So in this case, the code over here says sub l so so Bell, meaning it's subtracting instruction. So subtract. So again, it's four bytes long so we hear the next argument is going to be a four by law number which is the which is a numerical constant eight 141 00:19:11.880 --> 00:19:18.060 William Cheng: There. So over here, there's $1 eight over here. So, Intel assembly language dollar me that's a numerical constant 142 00:19:18.450 --> 00:19:24.000 William Cheng: Okay, so this case, it's a value. So what we're going to do we're going to subtract from the ESP register by eight. 143 00:19:24.600 --> 00:19:32.640 William Cheng: Okay so so swiping, will you, will you subtract a pointer right so this point over here. He was pointing right here when you subtract a from a is going to point eight bytes above 144 00:19:33.450 --> 00:19:39.450 William Cheng: Okay, so therefore the local variable here is eight bytes long because we need to store these two variable. So when you finish doing that it will look like this. 145 00:19:40.200 --> 00:19:55.860 William Cheng: That. So again, in this example, local grab over here is eight bytes long say register over here is eight bytes long ABP over here is four bytes long. Yeah, right. And then we get to the data. What is data that over here. So the idea here is that now we are ready to execute sequel. 146 00:19:57.000 --> 00:20:01.590 William Cheng: Okay, so please understand that whenever you try to execute sequel. You got to make sure that 147 00:20:01.800 --> 00:20:09.720 William Cheng: ESP is pointing at the top of the stack frame because you gotta, you gotta get ready to make another function call. So therefore ESP has to point at the top of the stack. Right. 148 00:20:09.990 --> 00:20:18.240 William Cheng: Okay, and also all the sacrament linked together in, you know, in a single link lists and the head of the link. This is inside a BP register. 149 00:20:18.630 --> 00:20:27.240 William Cheng: Okay. So right now, are we ready to execute sicko what the answer is yes because ESP over here, point to the top of the stack or any type of Sephora has all five things in there already. 150 00:20:27.420 --> 00:20:39.120 William Cheng: An EDP point to the tops diagram, the top secret over here is the next point or link all the stock rooms together. So if there are many, many stock went below. They all linked together in a single linguists. Okay, so now we're ready to execute the code. 151 00:20:39.660 --> 00:20:50.400 William Cheng: That. So in the beginning, one yeah yeah IP over here is pointing here, we are not allowed to execute sequel, because we haven't finished building THE STAFF ROOM YET SO ALL THIS COLOR beginning over here, we are setting up the stack rank. 152 00:20:51.060 --> 00:20:55.110 William Cheng: Okay, once we finish setting up the stack for him. Now we're ready to excuse ego. Yeah. 153 00:20:56.010 --> 00:21:03.840 William Cheng: All right over here. It says, Now we're ready to actually seek out in the data that we're sort of data right here is sequel that. So, by the way, when you are 154 00:21:03.990 --> 00:21:10.560 William Cheng: Running GDP and you say, I want to set a breakpoint in May. Right. How do you do that right. You say, Be main over here. I want to set a breakpoint in May. 155 00:21:10.830 --> 00:21:17.460 William Cheng: Okay so me I in over here. And then when you write your code when it gets the main over here. Your AI P is actually pointing right here. 156 00:21:18.300 --> 00:21:31.080 William Cheng: Okay, so again. Well, you set a breakpoint in May, when you get a breakpoint. There you are not right at the beginning of the main function you actually you finished setting up the stack frame and now you're ready to excuse eco so the actual breakpoints actually right after this L instruction. 157 00:21:32.700 --> 00:21:36.660 William Cheng: Alright, so that's where your break minus okay they are you ready because GDP. 158 00:21:38.250 --> 00:21:40.170 William Cheng: GDP is main function is to debug sequel. 159 00:21:41.340 --> 00:21:44.700 William Cheng: Alright, so that's why you break points right here. Okay. All right. 160 00:21:45.870 --> 00:21:52.980 William Cheng: Let's so so we're gonna skip the data. Right. So what do we know about that at all. We know that data is going to mess up your site is going to mess up VDI 161 00:21:53.310 --> 00:21:56.250 William Cheng: Other things would only know what it does, right, it doesn't really matter. They're 162 00:21:56.670 --> 00:22:02.670 William Cheng: All right. So at some point over here. We'll finish all the data. And now we're going to recommend it to execute a function to make the several teen call 163 00:22:03.150 --> 00:22:11.610 William Cheng: OK. So again, right now we ESPN pointing to the top of the stack at a BP OIL, you know, the all the saffron are linked together and that EDP point to the first name. 164 00:22:11.910 --> 00:22:23.700 William Cheng: Then, so now what we need to do is do we need to build the, you know, we need to start building the stack frame for the subtlety and then we need to transfer control into the celebrity and now the supporting can finished building the rest of the sack way. 165 00:22:24.570 --> 00:22:38.010 William Cheng: Okay. So in this case, two things. Either gone to the stack rain. Right. One is the function argument. So in this case, we need to push to function arguments onto the stack. Okay, the way this is done in see is that we need to push the function argument into the stack from right to left. 166 00:22:39.060 --> 00:22:44.760 William Cheng: Okay, so again, long time ago, the compiler people the operating system people to hire people that got together, they decided this is the way to go. 167 00:22:45.210 --> 00:22:55.140 William Cheng: Okay, so therefore, in this case, we're gonna we're going to push one onto the stack first, followed by pushing the value of the content of the variable A onto the stack. 168 00:22:56.010 --> 00:22:57.450 William Cheng: Right. Alright. 169 00:22:57.900 --> 00:23:05.310 William Cheng: So, so, so, so remember our sub routine is taking the x to the white power. So over here actually be here and why should be here right because we need, we need to 170 00:23:05.460 --> 00:23:10.890 William Cheng: Go from right to left. So the first thing that go on to the side is going to be why and then followed by the value of x. Okay. 171 00:23:11.250 --> 00:23:17.190 William Cheng: Alright, so let's see how this is done right the first instruction over here is push L dollar while we're going to put the push the numerical constant 172 00:23:17.400 --> 00:23:27.840 William Cheng: Which is a four by law number and we're going to push it onto the stack. Right. How do you do that you decorate yes P by four and then you put the value one right here. So when you're done, it will look like this. 173 00:23:28.410 --> 00:23:33.540 William Cheng: OK. So again, over here, again, you should think about it over here is why is going to be equal to one. 174 00:23:34.710 --> 00:23:44.880 William Cheng: Okay, so we push the value of y onto the stack. So now you know for the function is x to the white power. So, y equals one. OK. The next thing that we need to do is that Alex nice to one x nice to have the value of a 175 00:23:45.780 --> 00:23:55.500 William Cheng: Okay, so in this case was a right. So, so a over here is a local variable be here. As it turns out, the way that we lay out the local variable is at the top. Over here is at the bottom. 176 00:23:55.800 --> 00:24:07.200 William Cheng: Okay, how do we know that what because we read the code over here, then we know we sort of figure out, right, the compiler get to decide what is the order of these variables as long as the compiler treat them consistently. It doesn't really matter what the order is 177 00:24:08.310 --> 00:24:14.160 William Cheng: OK. So in this example, I in a while. It just happened that they sort of follow the layout of the C code. 178 00:24:14.580 --> 00:24:22.050 William Cheng: OK, so the sequels, his eyes on top as on bottom. So in this example over here just happened. This way the compiler decided to put it on top and they at the bottom. 179 00:24:22.320 --> 00:24:29.190 William Cheng: Yeah. So how do you copy the content of a over here which is inside the stack over here into another location inside the stack. 180 00:24:30.120 --> 00:24:41.070 William Cheng: Okay. So, okay, well, so. So again what we do, or we tried to we tried to copy the, you know, one of the local variable from the current stack frame into the stack frame of the next function that we're trying to call 181 00:24:42.630 --> 00:24:53.220 William Cheng: Okay, so again this is inside your virtual address space, right, so therefore they correspond to some physical memory and what we tried to do is to copy data from one location in physical memory to another location physical memory. 182 00:24:53.790 --> 00:25:01.920 William Cheng: Okay, how do you do that. Well, there's no way to tell the physical memory to say hey you know copy some data from one address to another dress. There's no way that can be done. 183 00:25:02.430 --> 00:25:10.590 William Cheng: Okay, so therefore we need to do is that we need to go to this memory location over here, read the content of a put it inside one of the temporary registry inside the CPU. 184 00:25:10.770 --> 00:25:14.970 William Cheng: And then we're going to perform and not the right operation onto the bus to write it into this memory location. 185 00:25:15.840 --> 00:25:26.010 William Cheng: Okay, so that's exactly what we did with x equals y z, right, we need to rewind RI Z Adam together and then put into x. So, again, in this case, we need to copy from one place to the other. It has to go through the CPU. 186 00:25:26.820 --> 00:25:31.260 William Cheng: There. So, therefore, what we're going to do over here is that we're going to read the value of A. So what's the word say 187 00:25:31.980 --> 00:25:46.500 William Cheng: Okay. So, remember this is a bias law does a bias long, so A is actually sitting at a BP minus 12 right MVP is one right here. There's a bias over here. There's four bytes over here for a. So, if you take where he BP is pointing to. And you do pointer. 188 00:25:46.860 --> 00:25:52.080 William Cheng: You know manipulation over here pointer math you subtract EP by 12 you're going to get the address of a 189 00:25:53.070 --> 00:26:05.910 William Cheng: Okay, so they have over here at the instruction says mu L E BP minus two x over here. There's a funny expression over here. We're going to take the EDP register. We're going to put it in parentheses. That means that we're going to reference the BP and then 190 00:26:07.050 --> 00:26:15.660 William Cheng: So sorry. We're going to take the content of GDP over here. And then we're going to subtract 12 from it and we're going to use that as an address to read data from 191 00:26:17.130 --> 00:26:22.470 William Cheng: Okay, so we're here again EDP over here is the base point here, we're going to subtract 12 from we're going to get a memory location. 192 00:26:22.680 --> 00:26:31.890 William Cheng: And then we're going to take that memory location and start a bus operation to access memory. And then we're going to read data from the bus and then we're going to put it inside the extra just sure 193 00:26:32.460 --> 00:26:36.300 William Cheng: Okay, so remember yeah charges is the general purpose of registered with a mess it up when we want 194 00:26:36.930 --> 00:26:40.410 William Cheng: Okay, so now we're going to use them to store a temporary a temporary variable. 195 00:26:40.800 --> 00:26:45.420 William Cheng: Okay, so therefore you can move all over here to say we're going to go to HTTP minus 12 196 00:26:45.660 --> 00:26:55.440 William Cheng: Use that as an address, start a bus operation and RE. RE for memory and then put the data over here into yay x. So when we finished doing that. Yeah, x is going to contain the value of a right 197 00:26:56.280 --> 00:27:05.280 William Cheng: Okay. So, therefore I'm going to you know to our parentheses over here around there to say A is a memory location right is the memory location right here. We're going to take, go to a get the content and put into yes 198 00:27:05.670 --> 00:27:20.190 William Cheng: Yeah, the next instruction over years they push LDS we're going to decorate ESP by four and then take the content of years and then push it onto the stack, guys. So we're finished doing that x will get the value x will get the content of a 199 00:27:21.750 --> 00:27:28.530 William Cheng: Okay, so, because right now. Yeah, excellent account database. So next time when we go to execute push out. Yes. Then we're going to end up putting a right here. 200 00:27:29.550 --> 00:27:37.590 William Cheng: Okay, so these two machines structure and basically go across the bus, read the value of a and then put into a year to register and then start another bus operation. 201 00:27:37.890 --> 00:27:44.220 William Cheng: Take the value that story is that a year to register and then right into memory using you know by by by pushing it onto the stack. 202 00:27:45.420 --> 00:27:54.270 William Cheng: Okay, so when we finished doing that it will look like this. Right. So again, the function arguments over here, x is going to be equal to the content of a over here. And why is going to be equal to one. 203 00:27:55.350 --> 00:28:02.370 William Cheng: Okay. And again, you know, we're executing this code right here. Alright, so once we finish setting up the function argument over here again the, you know, 204 00:28:02.820 --> 00:28:09.000 William Cheng: These three instructions will be here, push all the function arguments onto the stack. So the ARG over here is exactly a bike long 205 00:28:09.420 --> 00:28:17.850 William Cheng: Okay, we take three machine instruction set it up if there are more arguments, it will take longer. Okay. And then what we need to do is that we need to execute the call machining structure. 206 00:28:18.540 --> 00:28:30.060 William Cheng: Okay, so remember inside the CPU. Now he is. He is I CPU points. Exactly right here right so now you got to execute a call machine structure. So, so what we hear is just an address inside the tech segment. 207 00:28:30.840 --> 00:28:41.640 William Cheng: Nice. Remember function point or function is just an address right so somebody is going to be addressing is that a text when so will you make the call we execute a call machines, it will happen, right. We need to save the return address. 208 00:28:41.940 --> 00:28:47.010 William Cheng: Where we need to say the return address on to the staff. Okay, so what's going to be the return address. 209 00:28:47.610 --> 00:28:55.830 William Cheng: Okay. As it turns out, when you're finished executing this instruction, then in that case inside the CPU. He IP has already the incremental to point to the next instruction. 210 00:28:56.160 --> 00:29:06.570 William Cheng: Okay. So, so what happened is that, you know, when you're executing this code. This code is sitting inside of tech stack, man. So, so what the CPU will do is I mentioned before this call instruction fetch right you go across the bus. 211 00:29:06.840 --> 00:29:11.040 William Cheng: Take all these instructions over here, fetch it inside the CPU and then you execute it. 212 00:29:11.760 --> 00:29:16.080 William Cheng: Okay, so when you're executing this instruction yeah i p has already changed to the next value. 213 00:29:16.410 --> 00:29:24.450 William Cheng: Right so yeah POV. Watch your point to the next. And so it will be sitting right here. So this instruction, when you start to execute it was a VIP by pushing the IP onto the stack. 214 00:29:24.960 --> 00:29:33.270 William Cheng: Okay, yeah. How do you do that right. I'm going to detriment ESP by for over here and I'm going to say the content over here into the stack over here. I'm going to enable the IP. 215 00:29:33.420 --> 00:29:40.410 William Cheng: And when you copy pointer. They point to the same place. So when you finish doing that. Yeah. What is the point to is going to point to the next machine structure. 216 00:29:41.160 --> 00:29:48.690 William Cheng: Okay, so, so again, why does the point to the next instruction because the next instruction is going to be the return address right when you finish calling. So where should you return to 217 00:29:48.900 --> 00:29:56.400 William Cheng: What you shouldn't return to where you were before, because you can end up calling sub again. So, therefore, you have to return to the next instruction. After you finish callings up 218 00:29:57.360 --> 00:30:03.420 William Cheng: Now, alright, so I'll be here. So when you finish doing that you can set up the IP over here. And then we need to transfer control to to stop so 219 00:30:03.540 --> 00:30:15.840 William Cheng: So the call machine instructions will copy the content of VIP over here by pushing it onto the stack. It will also change the IP to point to a different place. So, so, this place is going to be inside of tech side man for the cell function. 220 00:30:16.890 --> 00:30:22.680 William Cheng: OK. So again, this is kind of like an atomic operation. And let me clean this up again. Okay. Over here when you call us up, we're going to 221 00:30:22.950 --> 00:30:35.220 William Cheng: Si, si P over here. He IP is going to point right here right over here. We're going to save this value onto the stack and then change tip to point to the the tech stack, man. And now we're executing code inside the cell function. 222 00:30:36.390 --> 00:30:43.050 William Cheng: Okay. So eventually the cell function is going to return when it returns going to return to the next instruction over here, right. So what should the stock room look like right now. 223 00:30:44.430 --> 00:30:52.740 William Cheng: Okay, so, so we're gonna look at the South Pole. A little later. Right. But now we're gonna sort of pretend that we finished calling stuff we're going to return. So again, since we save the context and we restore the context. 224 00:30:52.920 --> 00:31:04.050 William Cheng: When we come back here. Everything is going to stay exactly the same. Okay. The only thing that can be different is going to be the year to register, because the yeah yeah X register contain the return value from a function 225 00:31:05.430 --> 00:31:12.750 William Cheng: Okay, so therefore, when we return, right, if we draw the picture. What does it look like it should look exactly the same as before. I say, if you look at the previous slide over here to the next slide over here. 226 00:31:13.020 --> 00:31:19.620 William Cheng: The picture on the right over here, stay exactly the same, right, the top of the other tip over here. Get pop because after will return ESP point right here. 227 00:31:19.800 --> 00:31:26.550 William Cheng: And then the stack will be here. So over here is still y is equal to or whatever the values because inside our cell function, we may change y 228 00:31:26.760 --> 00:31:34.620 William Cheng: And x over here also you know whatever the cell phone can do them. They might change it there. But the stock went below here. It's going to stay exactly the way it was before. 229 00:31:36.510 --> 00:31:42.150 William Cheng: Okay, so now what happens, you will return for this function. Are we ready to execute sequel. 230 00:31:43.860 --> 00:31:54.780 William Cheng: OK, so again ESP is pointing at the wrong place. So therefore, we are not ready to execute sequel, we can only access to sequel away ESP is pointing at the top of our stack. Right, right. Whereas the top of our stack rank. 231 00:31:55.140 --> 00:32:02.250 William Cheng: Start problem solving is actually right here so ESPN 12 years in the wrong place, or what we need to do is that we need to get rid of all the function argument. 232 00:32:02.550 --> 00:32:07.050 William Cheng: From the sacrum and this is the sacrum for the cell function right so so right out of the top over here. 233 00:32:07.260 --> 00:32:14.070 William Cheng: Is still the spectrum for the cell function. So we need to get rid of all that. So the code that we actually over here, since this is a price law. 234 00:32:14.340 --> 00:32:19.050 William Cheng: Right, the function arguments over here's one and x is a proposal over here. So we're going to add a TSP. 235 00:32:19.920 --> 00:32:31.800 William Cheng: Guys. Okay, so okay L dollar a numeric constant A four years P over here. So USP is going to get increments by a so we finished doing that while then you know the art of the argument has been popped off the stack. 236 00:32:32.820 --> 00:32:39.930 William Cheng: So it's going to look like this over here. Okay, so, so, by the way. Will you do this right, all these things inside the stack frame for the cell function. They're still there. 237 00:32:40.590 --> 00:32:45.180 William Cheng: Right, except that we're going to assume that their garbage because it will make another function called they going to be completely wiped out. 238 00:32:46.200 --> 00:32:46.470 William Cheng: Yeah. 239 00:32:47.310 --> 00:32:56.340 William Cheng: Alright, so the next thing that we do over here is that ESP now point to the top of the stack rank for this function and then all the staff room are linked together and I had to learn English has pointed to buy a BP. 240 00:32:56.490 --> 00:33:06.450 William Cheng: So now we're ready to execute sequel. So what does the CFO wants to do by the secret sauce here, over here, it says, say the return value of sub into memory location, known as I 241 00:33:07.050 --> 00:33:15.150 William Cheng: OK, so the return value of service inside the X register, whereas I i is right here. So in this case, where is it right i is EDP minus 16 242 00:33:15.900 --> 00:33:25.620 William Cheng: That's all we hear the instruction says move le x, because x contain the return value. We're going to copy them into the memory location EDP minus 16. So again, this case. 243 00:33:25.830 --> 00:33:32.670 William Cheng: We're going to start a bus operation. We're going to take yaks put it onto the bus. I'm going to write it into a BP minus 16 which is right here. 244 00:33:33.030 --> 00:33:38.940 William Cheng: So again, eight bytes here eight bytes here. Right, so therefore he BP minus 16 that's the memory location for I 245 00:33:39.900 --> 00:33:47.820 William Cheng: OK. So again, this code over here is compiler generated compiler know exactly where he is. He's know exactly where i is so therefore you will generate that code. Okay. 246 00:33:48.720 --> 00:34:00.330 William Cheng: All right, so when this one is done. Now I have the return value of sub and then we're going to have all you know more data over here. And eventually, when this function is going to return. We need to get rid of all these three things inside the stack frame. 247 00:34:01.440 --> 00:34:04.740 William Cheng: You know, so then we need to return to exactly where we were before. 248 00:34:05.850 --> 00:34:15.630 William Cheng: Okay, so therefore, now we need to tear down the stack from over here. So let's see what we go, what we're gonna do. Okay. Alright. So we can ask the question, what happened when you make another sub routine call right here. 249 00:34:16.770 --> 00:34:27.870 William Cheng: Then, so. So let's say that before. So over here we, you know, I guess we we just finished copying this value into it over here. Right. And then we're gonna at the beginning of data right here. What if we make another function call. 250 00:34:29.010 --> 00:34:37.500 William Cheng: Okay, then this call is going to execute something very similar to this. Go right because you know the function that we call it may have one argument three argument may have five arguments may have no arguments. 251 00:34:37.710 --> 00:34:46.800 William Cheng: Right. So I believe that what we call another function we're going to build another stack frame and this way. We're going to wipe out all the values in the previous document that we built for the stack for for the cell function. 252 00:34:48.240 --> 00:34:57.000 William Cheng: Okay, so that's why you have to assume that all these you know all the function argument and local variable for the cell function. They all turned into garbage. 253 00:34:57.270 --> 00:35:02.370 William Cheng: Okay, they don't turn into garbage by themselves. But as soon as you make another function call, they, they're going to get wiped out. 254 00:35:03.120 --> 00:35:12.330 William Cheng: Okay, so that's why you can have a pointer pointing to the previous diagram because the, the, the, the local variable their automatic variable. They are allocated when the function 255 00:35:12.900 --> 00:35:20.070 William Cheng: When the function is called as soon as you return on the function, you must understand that they do what you think about is that they turn into garbage. 256 00:35:20.910 --> 00:35:23.760 William Cheng: All right, because if you make another function call, you're going to wipe them all out. 257 00:35:24.660 --> 00:35:32.460 William Cheng: Alright guys, so again this is very, very important to understand what happened right here. Yeah. Alright. So then we're going to go execute the data that over here at the end over here, we're gonna 258 00:35:33.120 --> 00:35:41.250 William Cheng: We're gonna return over here. So what we need to do is that we need to get rid of, you know, all these things inside of stack rank. So first we need to get rid of local variable. Why do we need to go. 259 00:35:41.940 --> 00:35:53.250 William Cheng: Get rid of a local level, because we don't need them anymore. Okay. So in this case, what happened is that they're eight bytes over here. So what we can do is, I will can increment USP by ate rice over here at our dollar a DSP. 260 00:35:54.000 --> 00:36:03.810 William Cheng: So we're going to increment ESP by eight right so that's what we do over here and then it's gone. The next thing over here is move zero to x, what this one is actually this instruction right here. 261 00:36:05.070 --> 00:36:12.960 William Cheng: Okay, so. So in this case, we're going to return for this function, right, we need to set yeah X over here to contain the value zero Y zero because obvious as return zero. 262 00:36:13.800 --> 00:36:21.000 William Cheng: Okay, so therefore we're going to move out zero. Yeah. Right. So there's over here instead of CPU. There's a year X register, we need to put a zero in there. 263 00:36:21.570 --> 00:36:27.990 William Cheng: So this will be done right here. So from this point on, we should not touch the x, right, because if we touch yes what they were going to wipe out the return value. 264 00:36:29.100 --> 00:36:36.870 William Cheng: Okay, so, so I guess that's why the code is sort of down right here. Next thing to say register, we, I don't know if you remember right, the top of here is going to be e di and then the 265 00:36:37.290 --> 00:36:44.280 William Cheng: Next one over here is going to be yes I we're going to put them back into the CPU. So this world, when we returned to the startup function in the cell function will not be surprised. 266 00:36:44.490 --> 00:36:55.350 William Cheng: Okay, because right now inside CPU, yes I do get completely messed up. We don't know what value they are. We don't care what we need to do is that we need to restore the previous value. So we don't surprise, it's not a function 267 00:36:55.650 --> 00:37:06.600 William Cheng: Yeah. So in this case, what we need to do is that we need to put this back into the EDM register. So Ed, I register over here. Now, there's some garbage. We need to put this value in there. So this machine instruction is known as the pop instruction. 268 00:37:07.110 --> 00:37:15.330 William Cheng: Okay. The problem surgeon over here is that we're going to start from where ESP is pointing to. So this guy's is pop out. We're going to take for buys from where it ESP is pointing to. 269 00:37:15.540 --> 00:37:24.960 William Cheng: Take these four bytes and then put them into the ED I registered over here. There. And when we finish doing that we're going to increment USP by four, because again, because this is out. 270 00:37:25.860 --> 00:37:33.060 William Cheng: Okay, so therefore we're going to copy this one into the yes even register and then we're going to increment ESP by four. So we finished doing that it will be like this. 271 00:37:33.900 --> 00:37:47.490 William Cheng: Yeah, and the next part over here is going to be SI and again inside CP where but yes I register. We don't know what kind of value. It is so what we're going to do is I want to say pop L. Yes, I take this value copy them into yes i and then increment USP by four. 272 00:37:47.940 --> 00:37:54.270 William Cheng: So now they will look like this. Okay, so this should look familiar to you, right. So now we're going to sort on our way back to what it was before. 273 00:37:54.540 --> 00:38:01.380 William Cheng: The next instruction over here says move out. He BP to ESP right so now there's only one thing left onto the stack over here is the VP. 274 00:38:01.920 --> 00:38:10.080 William Cheng: So in this case, it's kind of a weird instruction. It says mobile end up DSP. We're going to copy the content of the BP registered into the SP register. 275 00:38:10.470 --> 00:38:21.000 William Cheng: There. So here is a big one over here. We're going to copy them into ESP when you copy the pointer. They point to the same place while they're already pointed the same place. So in this case, this instruction does nothing. 276 00:38:21.300 --> 00:38:28.860 William Cheng: Okay, so some people call this instruction a no op instruction and all op. It's a no operation Shashi it doesn't do anything. 277 00:38:29.700 --> 00:38:33.180 William Cheng: Okay, so why would you, why would the compiler generate an instructor that doesn't do anything. 278 00:38:33.600 --> 00:38:39.120 William Cheng: Well, as it turns out it doesn't do anything in this case and later on I'm going to see another example. If I should do something. 279 00:38:39.630 --> 00:38:47.520 William Cheng: Okay, so just so happen over here, this insurance doesn't do any there alright so this case would copy one point to another one. They want radio, same place. It doesn't do anything. 280 00:38:47.910 --> 00:38:52.710 William Cheng: The next instruction over here is pop LBP we already know what happens, we need to pop out right we 281 00:38:53.040 --> 00:39:00.840 William Cheng: We take this value over here. We're going to put it inside a VP register. So again, this value over here, point to the next X Ray Rice over here. Here is the main stack rank. 282 00:39:01.080 --> 00:39:04.710 William Cheng: And the negative the stock went below over here is a staggering for the startup routine. 283 00:39:05.220 --> 00:39:17.850 William Cheng: Now, so again I'm gonna probably all up, what up copy this value into the MVP register. So when you copy a pointer. They point to the same place. So when you finish doing that EDP no longer points to EDP got appointed upon right here. 284 00:39:18.990 --> 00:39:28.620 William Cheng: Okay. And then since since the pop out. We also going to decorate ESP by four. So when you finish doing that it will look like this ESP will point right here ep, ep will actually point to 285 00:39:29.010 --> 00:39:33.330 William Cheng: Here's the main stack. Right. And here's the starter routines attack when you open right here. 286 00:39:34.290 --> 00:39:39.630 William Cheng: Okay, I don't know if you remember that at the beginning of the main function in look exactly like this. 287 00:39:40.500 --> 00:39:47.760 William Cheng: Okay, so therefore, again we save the context and we restore the context. The last instruction over here. We're going to execute is going to return to the startup function. 288 00:39:48.000 --> 00:39:54.540 William Cheng: Okay. So, this function is called return. What does return to right. So at this point, esp is pointing right here right below is the return address. 289 00:39:55.140 --> 00:39:59.250 William Cheng: OK, so the return machine structure is the opposite of the call machine structure. 290 00:39:59.730 --> 00:40:08.280 William Cheng: Okay. So this guy is what it will do is that it will take where ESP is pointing to, and then perform a pop Ellen instruction and and copy that into the VIP register. 291 00:40:08.940 --> 00:40:15.900 William Cheng: Okay. And then after that, they will increment ESP before so yeah he IP over here right now. What is the point to the IP over here right now point right here. 292 00:40:16.170 --> 00:40:22.530 William Cheng: Okay, and what about this memory location of your VIP VIP point to somewhere inside the startup function. And we don't have the code for that. 293 00:40:23.010 --> 00:40:31.800 William Cheng: Okay, so yeah, P over here again point to somebody, instead of tech segment. So when you put this guy back into the IP register over here. What then the IP is going to start executing 294 00:40:32.070 --> 00:40:42.060 William Cheng: Inside the the shuttle function. Okay. And also we're going to increment ESP by four. So again, we're gonna start basically actually the same co as when you return from Sub and we're going to tear down the stack. Right. 295 00:40:44.010 --> 00:40:48.180 William Cheng: Alright, so that's the CO, you know, for, you know, for, for, you know, for, for the main function. 296 00:40:48.990 --> 00:40:59.520 William Cheng: Right. The next thing we're going to look at is this a function. Right. So again, the sub function. What is the start right when it starts is that, you know, the, you know, in inside the main function will call the sub routine. So, so this 297 00:41:00.060 --> 00:41:09.750 William Cheng: So in this case, the IP over here, it points to the return address inside the main function. Right. So remember, where would I was right. The return function for the main the main functions actually right here. 298 00:41:10.770 --> 00:41:18.660 William Cheng: Okay, so therefore, at the beginning of sub function over here. He IP this one already. Point two. So again, we need to go to the previous slide over here, right here. 299 00:41:18.900 --> 00:41:32.160 William Cheng: Now and then also inside the function arguments over here, x equals to, you know, a inside main function and y equals two, one. Right. Okay. So already see how they set a set up over here. And now we're going to start executing code insights up 300 00:41:33.780 --> 00:41:37.350 William Cheng: Alright, so again the cell phones, you know, over here. Race to the X to the white power over here. 301 00:41:37.800 --> 00:41:45.990 William Cheng: So again, before you actually see how you're going to finish setting up your stack for me going to make sure that your as your stack from a link together and then a VP point to the top Saturday. 302 00:41:46.350 --> 00:41:54.540 William Cheng: Night. Alright, so let's take a look at how this is done. So now ESP is pointing over here HTTPS running over here so you can see that we sort of have exactly the same thing as the main function. 303 00:41:55.590 --> 00:42:01.140 William Cheng: Okay, so every see function that you're going to right at the beginning of that function. They all look exactly the same. 304 00:42:01.980 --> 00:42:07.950 William Cheng: Okay. So, therefore, what it's going to be the first thing that you do, again, you do exactly the same thing. So the first two machine instructions over here. 305 00:42:08.160 --> 00:42:14.730 William Cheng: They are some assembler what they will do is that instead of, you know, putting these two instructions over here, they will actually print something called enter 306 00:42:15.570 --> 00:42:19.290 William Cheng: Okay so enter. Is that the answer is that you're gonna perform these two operations over here. 307 00:42:19.710 --> 00:42:22.170 William Cheng: Okay, so again, let's let's review what it does. Over here. 308 00:42:22.350 --> 00:42:31.230 William Cheng: We're going to perform push out a BP. Right, we're going to take the content of EDP over here. So right now, where does he BP point. Right. So here is the stack rank for me right mean over here. 309 00:42:31.410 --> 00:42:39.360 William Cheng: And then below it over here is going to be the startup function right startup function over here. So EDP pointed this timeframe. And then there's a linguist. The point to the next document. 310 00:42:39.540 --> 00:42:46.230 William Cheng: If there's more than this over here. Again, all the staff from a link together EDP point the top stack rank and now the top slack room is going to be the sub routine. 311 00:42:46.470 --> 00:42:53.850 William Cheng: Right, so therefore we need to adjust them to point to the new stack right there. So we're going to do push out EDP copy this value on to the stack point 312 00:42:54.630 --> 00:43:03.750 William Cheng: So we're going to pre detriment USP by four and then copy BP right here. So when we finished doing that EDP the other the memory location over here, point to the same places CBP. 313 00:43:04.140 --> 00:43:13.290 William Cheng: There. So again, this is part of enter over here EDP over here is going to point to exactly where the CBD pointer is pointing. And again, over here, all the stack rooms have linked together. 314 00:43:13.470 --> 00:43:17.850 William Cheng: The next one is moving ESP to EDP we saw that already. We're going to copy this value. 315 00:43:18.240 --> 00:43:26.400 William Cheng: We're going to copy from ESP over here to EDP. So again, when you finish doing that EDP will point to the same place as ESP, so therefore it's gonna be like that. 316 00:43:27.360 --> 00:43:39.330 William Cheng: Yeah. So again, that's what enter does. Okay. It's so what it does is that is set up this the frame pointer. So now all the frames are linked together and a BP point to the top sack. Right. 317 00:43:40.230 --> 00:43:48.120 William Cheng: Right. So again, these that's what these two machines can do right they link all the soccer games together and EDP point to the very top sacrum, which is the function that you're executing. Yeah. 318 00:43:48.720 --> 00:43:57.270 William Cheng: Alright, so in this example over here, the same register this call actually doesn't save any register, guys. So definitely, this guy is going to be empty. Right. I don't if you can see it over here says empty. 319 00:43:57.810 --> 00:44:10.800 William Cheng: So all we need to do is to create a space for local variable, the local variable over here. There are eight bytes. Right. Both of them are integer. So this guy is going to subtract a from ESP register over here so we can do that. So when it's done, it will look like this. 320 00:44:11.880 --> 00:44:23.550 William Cheng: Okay, so now we're ready to execute sicko. Why is that because ESP point to the top of the stack frame and then the stack rooms are linked together in a single link list and he BP point to the top of the stack. Right. 321 00:44:24.870 --> 00:44:29.850 William Cheng: Okay, so now this case, when you look at the sequel over here. What is the first see and seeing structure, you have to execute 322 00:44:31.230 --> 00:44:40.560 William Cheng: Okay, so if you look at this code over here the first instruction is result equals two, one, right. So even though you know the terminology is that we can say that we're initializing our local variable over here. 323 00:44:40.770 --> 00:44:54.300 William Cheng: But again, the compiler doesn't initialize any of the local variable automatically. It's up to your US application programmer. If you want to make sure that your local rappers I initialize, you have to write C code to initialize that 324 00:44:54.960 --> 00:45:00.570 William Cheng: Okay, so this code over here. I do not initialize I do not initialize I will initialize result. 325 00:45:01.950 --> 00:45:08.220 William Cheng: Okay. So this guy is worse I end result right there local variable be here. So, again, in this example is right here and results right here. 326 00:45:08.970 --> 00:45:16.650 William Cheng: Okay, so here is a result equals two to one. So I'm going to set this value equal to was how, what is the location 444444 result over here. 327 00:45:16.860 --> 00:45:22.500 William Cheng: Right EDP is pointing over here. There's zero bytes over here for the same register so ep, ep minus four. 328 00:45:22.710 --> 00:45:36.930 William Cheng: Is the memory location for resolved so therefore the next instructions over here is move our dollar wine right because over here is going to read value one into a BP minus four. Okay, so therefore this machine instruction over here is exactly the same as result equals two, one 329 00:45:37.980 --> 00:45:45.660 William Cheng: There. So again, I write it over here please be very, very clear that this is not local variable initialization. This is running sequel. 330 00:45:46.440 --> 00:45:54.090 William Cheng: Okay. So you write your secret over here to initialize your local variables. So again, we're doing this because you wrote the code to to to initialize resolve equal to one. 331 00:45:54.450 --> 00:46:02.040 William Cheng: Okay, the compiler doesn't initialize any local variable. So, therefore, local variable. The initial value is whatever you left over on the stack. 332 00:46:02.400 --> 00:46:14.190 William Cheng: Okay, because all we do over here is to create a space for whatever garbage that you left inside the stack space over here, they will be the initial value for all your local variables. Okay, so that's why you always have to initialize local variables there. 333 00:46:15.120 --> 00:46:21.330 William Cheng: Alright, the next instructions over here. So I'm going to continue to execute obvious next instruction. What is the next instruction is equal to zero. 334 00:46:21.840 --> 00:46:28.410 William Cheng: OK. So again, some people will think that, oh, I could have 00 initializing local variable. No, we don't do such a thing we're executing CFO. 335 00:46:28.650 --> 00:46:41.310 William Cheng: The CEO says. The next thing you need to do is I go to zero. Right. So again I is an ep, ep minus eight. Right. So, therefore, we say move our dollar zero cost zero into EDP minus eight. So we're gonna say i equal to zero and recycled one 336 00:46:42.750 --> 00:46:50.970 William Cheng: Okay, alright. So the next thing over here, what we started we were starting to form of already i equals zero, i less than one i plus blood data. 337 00:46:51.210 --> 00:46:57.090 William Cheng: I'm going to skip all this code over here. Okay, so this is the code over here for the sub function we're really not interested in what it's doing. Right. 338 00:46:57.510 --> 00:47:04.890 William Cheng: The only thing that we're doing over here is that in the end it's going to return the result. Okay. Otherwise, all the other stuff. We don't really care what they do. We can just put that out there. It doesn't really matter. 339 00:47:05.430 --> 00:47:11.970 William Cheng: So I'm going to skip all that right over here. You can also see that there's a go to and all that kind of stuff. And then it's going to go to the end over here. 340 00:47:12.840 --> 00:47:20.400 William Cheng: There. So in the habit is over here is that I need to return the result. And whereas the result right result we saw that already is EDP minus four. 341 00:47:20.970 --> 00:47:29.370 William Cheng: Okay, so they've all got a copy the value of that memory location was resolved. We're going to put it inside the extra gesture, right, because, because he is registered supposed to have the return value. 342 00:47:29.640 --> 00:47:36.780 William Cheng: Okay, so we're here, we're gonna say, Move LTV p minus four minus whatever the issue is resolved. We're going to start a bus operation read that memory. 343 00:47:37.410 --> 00:47:48.240 William Cheng: Read the memory location over here. We're going to put a data inside a year extra gesture. So when this one is done inside the CPU X register is going to contain the value of the result. Yeah. 344 00:47:48.930 --> 00:47:57.450 William Cheng: Alright, so in this case over here. And then the next thing that we do over here is that we need to tear down the stack over here. So, so this case, we need to return UDP to point right here. 345 00:47:58.500 --> 00:48:07.470 William Cheng: Okay, so, so, so, so, this guy's, what do we do right here. So the first thing that will do is it is that move out a BP to ESP. And we saw that in the main function. It doesn't do anything. 346 00:48:08.340 --> 00:48:12.210 William Cheng: Okay, in this case, you actually do something because now yes P BP are pointing at 347 00:48:12.570 --> 00:48:24.300 William Cheng: Different places. So in this case, what would it do right we're going to copy the content of a BP over here into the ESP register. When we finish copying that they will point to the same place ESP point it will point to exactly what you'd be responding to 348 00:48:25.050 --> 00:48:30.750 William Cheng: Okay, so what do we do here we pop off all the local variable and the same register only one shot. 349 00:48:31.710 --> 00:48:38.220 William Cheng: Okay, so we finished doing that, this thing's gone right because ESP now point right here. A BP point exactly the same place. Yeah. 350 00:48:38.790 --> 00:48:48.450 William Cheng: Alright, the next instructions over here you know that's instructional over here. So, so actually I guess I sort of forget to mention something like this. Okay. Sorry about that. 351 00:48:50.730 --> 00:48:55.230 William Cheng: Okay. So beginning part over here is called enter the last two instructions over here's all this noise leave 352 00:48:55.560 --> 00:49:05.220 William Cheng: Guys at the beginning over here. The first two lines over here, always the same school. Enter. And the last, you know, last two line followed by return. They're always the same. So therefore, the last line over here is is no asleep. Okay. 353 00:49:05.670 --> 00:49:11.940 William Cheng: So that's why, you know, the compiler always generally the same code because you know some, you know, because to be at that. That's the code for leaving. Yeah. 354 00:49:12.600 --> 00:49:20.370 William Cheng: All right. We just finished doing this right. The last thing over here, we saw before, pop out a BP over here. Again, what does it do over here right EDP over here, point to the next. 355 00:49:20.580 --> 00:49:29.460 William Cheng: Frame and then again, it's the next point to a point. And next, and next time frame. So again, in one machine instruction we are, we're gonna we're gonna take this MVP winder over here. 356 00:49:30.240 --> 00:49:33.720 William Cheng: We're going to take the value that store inside the ESP register over here. 357 00:49:33.960 --> 00:49:43.680 William Cheng: Take these four bytes copy them into a BP. When he finished doing that they will point to the same place over here and we're going to increment. Yes, p by four. So when we finished doing that it will look like this. 358 00:49:44.250 --> 00:49:50.610 William Cheng: So again EDP over your point to the next frame and then again next time friends over here linked together, you know, for the function below. 359 00:49:50.820 --> 00:49:56.250 William Cheng: And then, yes, P is going to point right here. So again, this is exactly what we started at the beginning of the sub function. 360 00:49:56.850 --> 00:50:01.890 William Cheng: There. So the next thing that we do over here is return again return over your VIP again. Where does the point to 361 00:50:02.130 --> 00:50:10.650 William Cheng: It points to the instruction right after the call machine instruction. Right, we're going to take this one we're going to put it into the VIP register and then we're going to increment USP by for 362 00:50:11.550 --> 00:50:20.940 William Cheng: That. So again, when we finished doing that it will look like this right ESP is going to increase by for over here. And now we're going to, we're going to go back to the instruction right after the call submachine instruction. 363 00:50:23.070 --> 00:50:26.220 William Cheng: Okay, so then we will continue to executable we seen all that code already 364 00:50:27.000 --> 00:50:36.090 William Cheng: OK. So again, this is how you do it. Well, you make a function call. First you save the contacts and you build a stack frame for the sub routine and then you transfer control. 365 00:50:36.300 --> 00:50:46.350 William Cheng: To the sub routine by by making the call me about executed accomplishing searching and now you're in the context of sobriety and the sub routine is going to finish building the sack race before it can execute any C code. 366 00:50:47.010 --> 00:50:55.410 William Cheng: Okay. And when you finish your sequel, it's going to tear down the top part of the software and then it's going to return to worry what before over here we're going to continue to tear down the stack rank. 367 00:50:55.620 --> 00:51:05.580 William Cheng: When the stock rain is at the in the right shape where ESP point to the top of the stack rank and all the sacrament linked together and EDP pointed the top segue. And then we're ready to execute it called again. 368 00:51:07.290 --> 00:51:14.910 William Cheng: Or is it again. It's very, very important for you to understand that because from this point on, whenever we talked about a function call. You need to understand that this is how it works. Okay. 369 00:51:17.970 --> 00:51:24.690 William Cheng: So, you know, so we talked about Intel. What about other CPU. Right. So there's a popular CPU. They use those know as a spark CPU. 370 00:51:25.050 --> 00:51:33.270 William Cheng: We don't use them anymore because now you know wintel basically Intel Intel basically win the war, everybody's into now. So again, we're not talking about the other CPU. 371 00:51:33.690 --> 00:51:39.210 William Cheng: The other CPU the architecture may be completely different. I think the spark CPU has over 100 registers. 372 00:51:39.840 --> 00:51:46.560 William Cheng: Okay, so I have a feeling registering the specs. So do you make a function call. The idea is completely different. The stack frame. 373 00:51:46.800 --> 00:51:49.380 William Cheng: The staff. We look completely different. But again, is that is that great. 374 00:51:49.620 --> 00:51:55.230 William Cheng: Do you need to store a local variable. Of course, you have to start local right do you need to still function argument. Of course, you have to start with still function, Eric, man. 375 00:51:55.470 --> 00:52:02.700 William Cheng: They have a slightly different architecture. But the basic idea is still the same. Do you need to store the return address. Of course, you need to store the return address. 376 00:52:03.300 --> 00:52:13.980 William Cheng: OK, so the architecture is a little different, that's backwards going to look different but the functionality are exactly the same. So that's why I will only look at one, one stack frame. So this way you understand what all staggering look like 377 00:52:14.430 --> 00:52:21.750 William Cheng: Okay, so when you take your sequel compile into machine code we execute function call. Basically, they all work exactly the same way. Okay. 378 00:52:23.250 --> 00:52:32.880 William Cheng: Alright, so this is the sub routine call again can see the completed event over here. So again, you're not responsible for them for the spark machine code there. Alright, so we finished the first part over here and met and 379 00:52:33.180 --> 00:52:40.980 William Cheng: In the second part of lecture seven we didn't look at how do you go from one thread to another thread safe the context and restore the contacts.