WEBVTT 1 00:00:02.909 --> 00:00:11.160 William Cheng: So this is part three of lecture six. So we were done with chapter two. And now we're going to, we're moving into chapter three. 2 00:00:11.759 --> 00:00:21.660 William Cheng: Chapter three is called basic concepts kind of a strange you know name. So, so far we talked about the basic abstractions. Right. Chapter one, we talked about processes. 3 00:00:22.380 --> 00:00:26.430 William Cheng: We talked a little bit about the file system, chapter two, we talked about threads. 4 00:00:27.210 --> 00:00:37.680 William Cheng: So, and we know that chapter five, six and seven. We're going to talk about implementation of, you know, processes that thread implementation of virtual memory. 5 00:00:38.070 --> 00:00:46.470 William Cheng: Implementation or file system. Right. So we're not ready to talk about how to implement that yet. So now we are in. We were in between. When in chapter three and four. Yeah. 6 00:00:47.220 --> 00:00:55.110 William Cheng: So, so, so, so that the next is to talk about something in between, right. So again, chapter one and two. We talked about the abstraction during the user space. 7 00:00:55.560 --> 00:01:01.050 William Cheng: Five, six, and seven we're deep down inside the Colonel. So now we're going to talk about the stuff in between. Yeah. 8 00:01:01.530 --> 00:01:11.430 William Cheng: So, chapter three, we're going to start talking about the context for execution. How do you say contacts. How do you restore contacts, what kind of content. Do we have to talk about. So again, we're going to go over all these 9 00:01:11.940 --> 00:01:21.360 William Cheng: We're going to briefly talk also about the IO architecture. How does the operating system, you know, how does optimism supposed to talk to iOS devices. 10 00:01:22.170 --> 00:01:29.820 William Cheng: Okay, so we're going to sort of take a look at what would that look like and we may already mentioned about the bus architectures. And now we're going to go over a little more about the bus architecture. Yeah. 11 00:01:30.120 --> 00:01:34.200 William Cheng: And then we're going to talk about dynamic storage allocation. So that's Malcolm free 12 00:01:34.620 --> 00:01:42.420 William Cheng: We're going to sort of show you exactly how Malik and free or implement it. So even though Malcolm free their memory allocated function that's using the user space program. 13 00:01:42.990 --> 00:01:48.990 William Cheng: Okay. So, but, but, again, it's very important when you to understand exactly, you know what, what, Malcolm free are doing. Yeah. 14 00:01:49.350 --> 00:01:59.370 William Cheng: And then we'll talk about Lincoln and loading so so so this is how you I guess linking is we've done a one on one and two, right. So you compiled into sci fi to 15 00:01:59.670 --> 00:02:06.030 William Cheng: File that compiling. And then finally, what you do is that you link all the data files together into executable program. 16 00:02:06.330 --> 00:02:13.200 William Cheng: Okay, so that's called linking so linking is about how do you create the expert on program, and we'll talk about what's inside the executable program. 17 00:02:13.620 --> 00:02:18.690 William Cheng: Yeah, and loadings to take the executable program on folded into memory and start executing code right there. 18 00:02:19.350 --> 00:02:23.370 William Cheng: Okay, so once you know what's inside the executable program or the last part is pretty straightforward. 19 00:02:23.910 --> 00:02:36.540 William Cheng: You go to the File yeah unfolded and then you you know you you copy them into memory and then you transfer control to, you know, something is that the tech side, man. And then, and then you start executing code. Yeah. 20 00:02:37.260 --> 00:02:43.260 William Cheng: Right. The last thing we're going to talk about in chapter three is booting rise uprooting is how do you how to get to the operating system. 21 00:02:43.770 --> 00:02:50.940 William Cheng: Right when you're when we start your machine to machine is not powered on. So what you do is that you flip your switch on 22 00:02:51.480 --> 00:03:00.180 William Cheng: On your machine at some point is that, excuse me, the operating system. So how does it get there. Okay. So that process known as booty. Okay, so. So again, these are covered covered in chapter three. 23 00:03:01.740 --> 00:03:09.540 William Cheng: All right. Alright, so first of all talk about context switching. We're going to talk about four different kinds of context switching. Right. The first one is called procedural context switching 24 00:03:10.380 --> 00:03:15.930 William Cheng: So when you make a function call. Okay, so, so, so, you know, so for example, x is going to call why 25 00:03:16.500 --> 00:03:23.250 William Cheng: When you're executing why you need to say the context of x, right. So I'm going to switch to actually in contact. So why so 26 00:03:23.670 --> 00:03:30.420 William Cheng: You need to save the context of x and then you need to build the context for why okay and then you can start excluding the function is that why 27 00:03:30.870 --> 00:03:40.350 William Cheng: Okay, so why it was so when when the function. Why is it is done, you need to tear down the context of a function y and then return it back to what it was before. Back to the contrast of x. 28 00:03:40.830 --> 00:03:45.090 William Cheng: Okay, so we're going to talk about. So that's called procedural context switching going to talk about how that's done. Yeah. 29 00:03:45.450 --> 00:03:50.280 William Cheng: And then we're going to talk about threats and CO routine. We're going to sort of explain what it means. 30 00:03:50.820 --> 00:03:59.460 William Cheng: So this guy's threat context switching your you're running one thread inside of CPU. How do you switch that thread to run a different thread inside of CPU well. So again, what we need to focus on 31 00:04:00.120 --> 00:04:07.380 William Cheng: Is how to say the context of that threat and then how to load the context of next threat. And this way you switch the context on one thread to another. 32 00:04:07.710 --> 00:04:13.950 William Cheng: Because we're going to see exactly how this is done. Apparently this is done using something called co routines and we're going to talk about that later. Yeah. 33 00:04:15.150 --> 00:04:23.520 William Cheng: And then the next one is system called right you start, you start in the context of a user threat and now when you make a system called you're going to go into the kernel. And now you become a colonel threat. 34 00:04:23.940 --> 00:04:32.880 William Cheng: Okay, so how do you actually go from a user three contacts to a colonel to our contacts, right. So that's what I'm talking about it. And also when you're done is that the Colonel. How do you return back into the user three contacts. 35 00:04:33.240 --> 00:04:42.120 William Cheng: Okay, so that's the system called context switching, if I may interrupt you can get a highway interrupt anytime. Alright, so we are in the user threat contact you can get a interrupt. Okay. 36 00:04:42.420 --> 00:04:49.620 William Cheng: So in this case, again, we need to save the contents of the user threat switch to executing our social team, we're done, we need to return back to the user three contacts. 37 00:04:49.860 --> 00:04:57.870 William Cheng: Okay. You can also get an interrupt inside the colonel threat contacts, your, your experience that kernel. And now you get hard to interrupt. We're going to say the context of the colonel threat. 38 00:04:58.110 --> 00:05:05.430 William Cheng: Go execute interrupt service routine. Are we done, we need to return back into the kernel three contacts. Yeah, it's also even possible that you are actually, you know, 39 00:05:06.000 --> 00:05:14.370 William Cheng: Execute interrupt service routine and now you get a more important interrupt more urgent interrupt. So in that case, you need to save the context of the interrupt service routine go to execute 40 00:05:14.970 --> 00:05:19.710 William Cheng: more urgent, you know, absorbs it and when you're done, you need to go back to the interrupt service routine that was there before. 41 00:05:20.520 --> 00:05:26.760 William Cheng: OK. So again, we're going to talk about these four different kinds of context switching first one I'm looking at the procedural context switching 42 00:05:27.630 --> 00:05:37.170 William Cheng: Yeah, so, so, so the magic of the operating system is to do context switching right and then what's more important over here is that when you do context switching, you're going to do it in a seamless fashion. 43 00:05:37.680 --> 00:05:49.110 William Cheng: Okay, so this way. It's as if you know so. So for example, if you want to put a threat to sleep. Okay, we're going to put a threat to sleep I savings context. Later on we're going to restore the context of this thread and this thread doesn't even know that he fell asleep. 44 00:05:50.490 --> 00:06:01.440 William Cheng: Okay, so, so, so we need to implement all this kind of stuff seamlessly seamlessly. So this way you know your your your your thread or your or your process, they won't even know that context switching is happening. 45 00:06:02.250 --> 00:06:12.600 William Cheng: There. Alright, so, so, so, so these kind of switching needs to be needs to happen transparently. So, so what what kind of a kind of switching is not transparent with. So for example, in the good old days. 46 00:06:13.080 --> 00:06:28.440 William Cheng: Or if you are sort of programmer. The, the, the, the right program for every operating system like the Macintosh or, you know, Microsoft Windows in the good old days over here where you try to, you know, we try to give up the CPU, you actually have to explicitly give out the CPU. 47 00:06:29.580 --> 00:06:36.390 William Cheng: Okay, we don't do it anymore. Right, we're implementing a one on one and warm up to, especially when you're warm up one. When do you give up the CPU. 48 00:06:37.170 --> 00:06:39.540 William Cheng: Well, as far as your code is concerned, you're never give up the CPU. 49 00:06:39.810 --> 00:06:49.110 William Cheng: Okay, but does your code give up a CPU. Well, sure enough. Yeah, if you poke around too long the organisms is going to yank your three of the CPU and give it to somebody else. And later on, it's going to combine and get a CPU back to you. 50 00:06:50.130 --> 00:06:55.500 William Cheng: Okay, so therefore we need to do this in a transparent way that your thread doesn't even know that is happening now. 51 00:06:56.340 --> 00:07:07.140 William Cheng: All right, and also over here as question. What is the operating system to what the application is running, right. So again, you need to answer this question when there's one CPU when there's multiple CPU. I mean, when when there's only one CPU. 52 00:07:07.560 --> 00:07:10.890 William Cheng: What if the application is running. And clearly the operating system is sleeping. 53 00:07:11.790 --> 00:07:20.010 William Cheng: Okay, so therefore get most of the time the organism doesn't do anything. You only get activated when you know the the application provide trapping to the operating system. 54 00:07:20.250 --> 00:07:28.920 William Cheng: Or when a hurry to outcomes and you switch to running the interrupt service routine. OK. So again, very important concept of understand most of the time you're operating system is really not doing anything. 55 00:07:29.550 --> 00:07:40.260 William Cheng: Okay. Alright, so, so, so, so, you know, here we have CPU with the colonel with the application. So all these juggling needs to happen, you know, transparent matter and the application doesn't know about this, this is happening. 56 00:07:41.040 --> 00:07:49.950 William Cheng: So and also you know needs to work when there's one CPU, where there are multiple CPU. They all needs to work in in sort of the only to work in a transparent manner. Okay. 57 00:07:52.110 --> 00:08:03.870 William Cheng: All right, so, so, so we talked about this term of execution context, right. So what is the execution context. So again, it's pretty complicated. It involves everything right, because the execution contract is whatever you need in order for you to execute code. 58 00:08:04.770 --> 00:08:11.850 William Cheng: Okay. So, therefore, if you want to start executing code, code. You want to go to sleep. We need to save the context. So in that case, in that case what do we have to say. 59 00:08:12.900 --> 00:08:22.140 William Cheng: Okay, you can say, well, I need to save the context inside the CPU or whatever the CPU. That was doing before, I need to save the entire state of the CPU into some data structure and later on when I want to resume execution. 60 00:08:22.320 --> 00:08:29.940 William Cheng: I'm going to take the state that storing some data structure, put it back into the CPU and then my my my second resume execution. 61 00:08:30.600 --> 00:08:34.470 William Cheng: OK, but so but but in that case, it's just a CPU. What about your address space. 62 00:08:35.130 --> 00:08:43.710 William Cheng: The address. This is also part of your contacts. Right, well you program is running, you want to be able to still have an address space. If for some reason the address space is gone. Well, then you'll progress not gonna be any good. 63 00:08:44.070 --> 00:08:54.960 William Cheng: Okay, what about your extended address space your extended address space the operating system has maintained that for you. Right. So in that case, again, will you continue to execute whatever that was the in the extended address space, you just assume that they're there. 64 00:08:55.830 --> 00:09:05.490 William Cheng: Okay, but for the operating system, the ordinances and need to make sure that you know all that information is there, right. So again, the operating system job is to keep all these contacts. So when you're running again he will restore all these contacts, back to you. 65 00:09:06.300 --> 00:09:09.810 William Cheng: That. But what about which machine that you're running on. Is that is a part of your contacts. 66 00:09:10.380 --> 00:09:19.170 William Cheng: Okay, I don't know if you have done this before, right, if you're running a virtual machine, you can actually save the entire state of the virtual machine, send it to your friend that has the same kind of, you know, 67 00:09:19.800 --> 00:09:23.670 William Cheng: Virtual Box or something like that. And you can actually pick up where you left off on a different machine. 68 00:09:24.360 --> 00:09:33.750 William Cheng: Okay. So clearly, you know, which machine that you're running on. Doesn't really matter. But maybe what type of virtual machine that you're running on. Maybe that will matter, right, because otherwise, if you send it to a different virtual machine vendor. 69 00:09:34.080 --> 00:09:35.010 William Cheng: Maybe it's not gonna work. 70 00:09:35.730 --> 00:09:45.450 William Cheng: Okay, so, and also what, you know, you know, what about where you are. You're running you're running a program as your home is because it matters running your home. If you move it into a different location. 71 00:09:45.630 --> 00:09:53.640 William Cheng: Is everything's going to work. Well, chances are it's going to work, right. So, so, you know, as long as you have a way to save the state for your machine and you can restore it somewhere else in that case it will work. 72 00:09:54.480 --> 00:10:00.060 William Cheng: Okay, so typically will really don't worry about, you know, whether you're running the same virtual machine where the urine is the same geographic location. 73 00:10:00.360 --> 00:10:05.970 William Cheng: Those things doesn't seem to matter right but all the other stuff your address space or extended address space to stay inside your CPU. 74 00:10:06.210 --> 00:10:12.960 William Cheng: They're all part of your execution context. Okay. So, therefore, we're going to do is I want to sort of figure out where do you say, well, these execution context. 75 00:10:13.530 --> 00:10:22.770 William Cheng: Okay, sort of, the bottom line is that the exclusion conduct is safe all over the place, right, for example, if the stuff inside you extended address space. Well, they will be saved inside your extended address space. 76 00:10:23.640 --> 00:10:27.660 William Cheng: Okay if stuff that are inside your address space you address space needs to be maintained somehow 77 00:10:28.140 --> 00:10:35.430 William Cheng: In case your outdoor spaces. Destroy what the audience is in need to have a way to do to sort of, you know, recreate your address space, all these things need to be maintained. 78 00:10:35.850 --> 00:10:46.950 William Cheng: OK, so the contact information is scattered all over the place. So, so we're going to talk about these four different kinds of context switching and you will see that the contact information for different situation. They are stored in different places. 79 00:10:48.180 --> 00:10:58.530 William Cheng: Okay, so. So in the end, it's pretty complicated, right. So, so the first thing we're going to do that, we will we will talk about inside your address space there is the stack space. 80 00:10:59.220 --> 00:11:08.460 William Cheng: There the stats, as we mentioned before, right here is your address space in the bottom over here. Here is your stack space right instead of static space over here at the survey funnel funnel. 81 00:11:08.970 --> 00:11:17.130 William Cheng: We start building stack frames right here stipend for the main function and they may call x. It's called why and then a woman, so we can make more function calls over here. 82 00:11:17.820 --> 00:11:24.360 William Cheng: Yeah, so, so, so, so in this case, you know, when you're executing a function, then your contacts is the current stack rank. 83 00:11:24.900 --> 00:11:31.650 William Cheng: Right, if I'm, if I'm actually execute a function over here. Here's my here's my tech segment instead of excitement. There's a function x 84 00:11:31.980 --> 00:11:44.520 William Cheng: Right, so. So at the top stack frame over here inside the stack space. This one correspond to X guys will get. What does that mean to say that the corresponding x that means that the function argument and local variable. They live inside this doctrine. 85 00:11:46.320 --> 00:11:55.950 William Cheng: OK. So again, when you think about any kind of a programming language or any kind of a C code or C plus plus, plus code that you have ever written all the code that you have written, they're all inside of a function 86 00:11:56.760 --> 00:12:02.400 William Cheng: Okay, so therefore the concept of a function is very, very important, because whenever you run code your code is always inside the function 87 00:12:02.490 --> 00:12:07.560 William Cheng: So again what that means that the IP is going to point to the text segment pointing to the code within the function 88 00:12:07.740 --> 00:12:15.300 William Cheng: And ESP is going to point to the top of the stack over here and there's a sack frame at the top, the stack rank correspond to that function and EDP is going to point to the middle section. 89 00:12:16.410 --> 00:12:21.480 William Cheng: Okay, so in chapter three, we're going to see exactly what these income from. Okay. But again, this is how you maintain 90 00:12:21.870 --> 00:12:29.010 William Cheng: You know, the, the, sort of, sort of that sort of the function, contact the context where your function you keep them, you know, you keep any satisfactory. 91 00:12:29.580 --> 00:12:38.730 William Cheng: Now, or so. So when we talk about procedural context switching if x is going to call another function y. So, x is right right now and then the top stack frame is the one is the function that you're executing right now. 92 00:12:39.180 --> 00:12:48.000 William Cheng: Okay, so if you want to call why that means that we need to say the context of function x and there. How do we call function why. Well, we need to build a stipend for function way over here. 93 00:12:48.510 --> 00:13:00.330 William Cheng: Okay, and then we need to switch context to to to y. So, now, now I can execute the code is that is that why we also need to make sure that the function argument and local variable. There are there before function wise ready to execute 94 00:13:01.740 --> 00:13:07.800 William Cheng: OK. So again, we need to say the context of x and then we need to switch to the context for why by building a building economists worldwide. 95 00:13:07.980 --> 00:13:11.280 William Cheng: And then we switch the function y and eventually when function wise finish. 96 00:13:11.460 --> 00:13:21.360 William Cheng: We need to tear down the stack frame and then return back to the previous context. So, so when we're done over here again. Yes, he's got a part right here, he'd be peace. Good point here and we start executing of code in x again. 97 00:13:22.500 --> 00:13:26.070 William Cheng: Alright, so now we're going to see exactly this is how this is done. Yeah. 98 00:13:27.360 --> 00:13:31.680 William Cheng: Alright, so, so this is your procedure context switching X can call why and I'm going to reach a problem. Okay. 99 00:13:32.790 --> 00:13:42.270 William Cheng: Okay. The example that we're going to use that we're gonna have to function is one is called the main function now at the main function is that would be the sort of the first procedure for your main threat and it's going to call a function called sub routine. 100 00:13:42.720 --> 00:13:50.220 William Cheng: So here's my sub routine over here. What it does is that compute x to the power of why. Okay, so we're here x as the first argument. Why is the second argument. 101 00:13:50.730 --> 00:14:00.780 William Cheng: So in this example over here. The main function. What it will do is that they would do a bunch of stuff that is going to run some sequel at some point is going to call the sub function over here to compute x to the power of one 102 00:14:01.890 --> 00:14:03.810 William Cheng: Okay, so we know the extra power one is going to be 103 00:14:04.320 --> 00:14:08.370 William Cheng: A to the power of wine. So in this case, whatever a value is if a equal to seven. 104 00:14:08.610 --> 00:14:13.380 William Cheng: When this function return it needs to return the value of seven right because seven to the power of one is equal to is equal to seven. 105 00:14:13.620 --> 00:14:22.590 William Cheng: Okay, so here's a several teen right so what the sub routine does over here is that instead of loop is going to multiply x by itself. Why times. Okay, that's how you raise x to the power of why 106 00:14:22.980 --> 00:14:31.110 William Cheng: Okay, and then eventually. So what it will do is use a result variable. We had to hold a temporary result would X, multiply by itself. Why times we're going to return a result 107 00:14:32.010 --> 00:14:38.670 William Cheng: Okay. Simple enough, right. So we're going to see how do you starting out, you're inside the function may, how do you build 108 00:14:38.970 --> 00:14:46.890 William Cheng: You know, build a context for sub routine a sub and then you transfer control to the sub routine and then had the sub routine, you know, actually all this code over here. 109 00:14:47.100 --> 00:14:55.920 William Cheng: And and then when it's when it's done is going to return back to what it was doing before. And we return back to what it was doing before you know everything inside the main function. Everything is going to stay the same. 110 00:14:57.510 --> 00:15:00.480 William Cheng: Or is it again, we're gonna see how you say context and restore contacts. Yeah. 111 00:15:02.100 --> 00:15:02.610 William Cheng: All right. 112 00:15:04.380 --> 00:15:16.710 William Cheng: Okay. So, so what is the context of May, right, the colors, the middle of years, as I said, right before you caught up okay we need to save the contact. So in this case, what, what is the context of a main over here, right. Get the causes of may appear include all the CPU registers. 113 00:15:17.790 --> 00:15:30.540 William Cheng: Okay. So, okay, well you're running this code inside main over here, we're running some code inside the CPU right so if you return from the support team over here. You need to find all the CPU register all in exactly the same state as before you cause up 114 00:15:31.620 --> 00:15:36.390 William Cheng: Right, because otherwise what he's actually getting cold over here. What, then, the new code is not going to work if the register get messed up. 115 00:15:37.050 --> 00:15:46.500 William Cheng: Okay, so therefore from the main function to the separately. We need to save all the CP register over here for the main function. So this way. When we return, you know, all the register will have the right values now. 116 00:15:47.880 --> 00:15:58.710 William Cheng: What about the global variables, right, assuming that the cell function over here doesn't change any global variable. Well, in this case, when you make the sub function called when sub return all the global variable. They need to be exactly what the value had before. 117 00:15:59.610 --> 00:16:07.740 William Cheng: Okay, so look at the address space shouldn't be touched. You know if this is a sub routine over here. It doesn't modify any memory location inside the address space. Yeah. 118 00:16:08.520 --> 00:16:17.490 William Cheng: All right, what about the main function over here, their local variables, a local variable also should be exactly the same way as before, right, unless you sort of pass the address of a local variable as a function to modify it. 119 00:16:17.730 --> 00:16:21.930 William Cheng: Otherwise, you know, the sub function novia shouldn't shouldn't modify any of the local variables. 120 00:16:22.200 --> 00:16:29.430 William Cheng: OK. So again, the local variables. They live inside the stack rain. Right. So when you call this function. The cell function only mess around with their own stack frame. 121 00:16:29.820 --> 00:16:38.310 William Cheng: When you're done, you're going to return back to me. Well, then, in this case I a nation state exactly what it was before. And then you say i equal to something in the only at that point, you gotta change the value by 122 00:16:39.540 --> 00:16:50.070 William Cheng: OK. So again, these are the contents of May, right. So again, they need to be saved and restored. Okay, what about the contents of sub in order for you to run this function. What do you have to do while you have to copy at you. 123 00:16:50.520 --> 00:16:58.260 William Cheng: You have to copy the value of x or y. So remember when you have when you write a function, this function can be caught anywhere inside your program. 124 00:16:58.920 --> 00:17:09.990 William Cheng: Okay, so this function compute x to the power of y. Right. So where can you do this, you can do this anywhere I can, I can call this one May, it may call another function called X and another phone call, why and why is going to cause up is that allowed 125 00:17:10.320 --> 00:17:15.150 William Cheng: Well, yeah, that's also allowed right so maybe why when it costs are going to raise you know 72 to the 56 126 00:17:15.600 --> 00:17:20.790 William Cheng: Power or something like that. And I said, this guy's again in this case x will be 72 and y will be 56 127 00:17:21.090 --> 00:17:30.870 William Cheng: Okay, in the main function over here. A will be we will be the example I give a will be seven. And then while we're here will be will be one. So the x and y over here needs to be set up before this function is called 128 00:17:31.770 --> 00:17:37.590 William Cheng: That and also what about a local variable over here, you need to create storage space for the local variable, okay. 129 00:17:37.950 --> 00:17:42.150 William Cheng: So I guess I just answer some questions in the class Google who recently about local variable. 130 00:17:42.450 --> 00:17:48.570 William Cheng: You know, so, so, so, so again, when you make a function called a lot the space for the local variable is going to get automatically created 131 00:17:48.990 --> 00:17:56.130 William Cheng: Okay. And when the function return they will be automatically destroy that. So that's why some people will call local variable automatic variables. 132 00:17:56.580 --> 00:18:02.310 William Cheng: Okay, because their, their memory location or automatically allocated when you call the function and they will get automatically 133 00:18:02.610 --> 00:18:08.550 William Cheng: The allocated we return a function that very different from Malcolm free. Right. Well, you start to allocate memory in the heap. 134 00:18:08.730 --> 00:18:15.690 William Cheng: You have to make an explicit call to Malik. And this way you allocate memory and eventually when you're done with the evening called free to free up their memory location. 135 00:18:16.140 --> 00:18:20.310 William Cheng: Okay, for a local variable. They need to be automatically allocated. So how do we actually do that. 136 00:18:20.640 --> 00:18:30.330 William Cheng: Yeah. So in order for sub to execute. We need to, you know, set up x and y, we need to create the space for the local variable and then we're ready to execute the functions up 137 00:18:31.230 --> 00:18:39.840 William Cheng: Again, so again you know how to actually set all these things up and when, when we're done, we need to tear down you know all these contacts and then we need to go back to the main function now. 138 00:18:41.250 --> 00:18:49.800 William Cheng: The global variables. So, so get there. He variables. They are local variables. What about global variable, the global variable are there in the fixed location is that the address space. 139 00:18:50.070 --> 00:18:55.200 William Cheng: Once you if you have a global variable, okay like Aaron number right here number over here, right. It's a global variable. 140 00:18:56.280 --> 00:19:01.860 William Cheng: Okay, why is your program. Start whatever this address. The address for air number will never change. 141 00:19:02.760 --> 00:19:15.990 William Cheng: OK, so the lifetime for global variable is the entire lifetime of your program, we will provide starts to global variables going to stay where they are until the program dies, right. The one program does the entire address basis gone well then the global variable cease to exist then. 142 00:19:17.280 --> 00:19:24.780 William Cheng: Again, what's very important for you to understand. I'm going to explain the detail. Very, very soon, the local variable and function arguments they are inside the current stack right 143 00:19:25.440 --> 00:19:37.260 William Cheng: Okay. So, therefore, what the main function calls up, you need to build a stack frame and you need to establish x and y. And I end result inside the secular. Great. And then they need to be able to access them. So we're going to see how this is all that. Yeah. 144 00:19:39.810 --> 00:19:49.830 William Cheng: All right, so, so for the x86 CPU. So again, long time ago, the compiler people the hardware people, the, the, the organization, people got together and they decide who's going to figure out what 145 00:19:50.370 --> 00:20:00.270 William Cheng: Guy. So at that time they agree what a staggering look like on the x86 CPU. They are so every story has five things in it right over here. Here's one stat RAM. Here's another stack frame. 146 00:20:00.810 --> 00:20:11.010 William Cheng: The bottom stuck where maybe this one is for the main function right when main course up, then this will be the stack for and for the cell function and every stack frame will have five things right let's let's take a look at what these five things are 147 00:20:11.400 --> 00:20:14.280 William Cheng: On the top of the stack frame over here is going to be your local variable. 148 00:20:14.760 --> 00:20:22.980 William Cheng: Okay, so for the sub function over here, the local great, but what are the local variable is I end result. Right, so therefore the sub function I and result is going to be sitting right there. 149 00:20:23.490 --> 00:20:26.190 William Cheng: There. And then there's something for the same register. 150 00:20:26.760 --> 00:20:36.960 William Cheng: Okay, so one of the same register over here. So I haven't decided the main function is going to call the function in the cell phones and start executing code the stuff bumps you may be very, very complicated. It's going to mess up all the CPU registers. 151 00:20:37.320 --> 00:20:43.830 William Cheng: Okay, if you mess up all the CPU register when you return to the main function. The main function will be very surprised because all the CP register have changed values. 152 00:20:44.940 --> 00:20:56.280 William Cheng: Okay, so therefore the deal over here is that for the self function. The cell function nodes which register is going to mess up. So therefore, right before they mess up the register. They need to save all the register over here inside this area. 153 00:20:57.420 --> 00:21:02.340 William Cheng: Okay. So at the beginning of the sub function when they start executing code you need to save all the register that you're gonna mess up. 154 00:21:02.490 --> 00:21:10.290 William Cheng: Save them into this area over here and then you can go ahead and mess up all the register. When you finish messing up the register. When you find it will be, we're going to return back into the main function. 155 00:21:10.470 --> 00:21:16.200 William Cheng: What you will do is that you will take all these say register value, put it back into the CPU and then returned to the main function. 156 00:21:16.890 --> 00:21:23.160 William Cheng: Okay, so this way when you return to the main function all the CPU registered over here will have exactly the same value before you call this function. 157 00:21:23.970 --> 00:21:28.860 William Cheng: Guys, so therefore that's what it's for. Alright, so again, only the cell function knows what register has to be saved. 158 00:21:29.370 --> 00:21:40.680 William Cheng: Okay, because the compiler is the one that generate code for the for the cell function, so he knows. Oh, I'm going to mess up three register. I better say them right here so they don't. I can restore them right. So again, saving contacts and restore contacts. 159 00:21:41.850 --> 00:21:43.620 William Cheng: The next point over here is called a BP. 160 00:21:44.700 --> 00:21:53.190 William Cheng: Okay, so a VP over here. So he will mentioned before, it points to the middle of the stack ramps on the right hand side, we actually have the CPU CPUs it on the right over here. 161 00:21:53.610 --> 00:21:58.440 William Cheng: The three most important registering says instead of CPU is he IP. Right. So, so what does the IP point do 162 00:21:58.830 --> 00:22:04.170 William Cheng: Well, yeah, P point to the text admin rights over here we're showing the stack. So this one points, way up into the 163 00:22:04.590 --> 00:22:08.640 William Cheng: Tech segment right so we're not gonna sort of draw that right now when we need to draw that I'm going to draw a red arrow. 164 00:22:08.970 --> 00:22:17.640 William Cheng: That what about ESP ESP always point to the top of the stack rain. So this way when we make another function call. We know exactly what to build next act right 165 00:22:18.630 --> 00:22:24.810 William Cheng: Okay so ESP, you know. So again, the staff room has five things in an ESP always point to the very top that 166 00:22:25.470 --> 00:22:34.560 William Cheng: EDP over your point of the middle of the Sackler instead. Now we know now we're going to show you exactly where points to it points to the memory location where you store the next ep pointer. 167 00:22:34.950 --> 00:22:42.960 William Cheng: OK, the next ep point over here is going to point to the next stack rank. And what about the next item over here. Again, it stores the EDP point are we here at points to the next document. 168 00:22:43.920 --> 00:22:55.290 William Cheng: That. So in a way, the EDP over here is basically it's like a linguist link. This has a next pointer. So yeah, we draw a picture like lateness right it is pointing like this, right, the pointer over here. We're going to call it next. 169 00:22:56.970 --> 00:23:06.420 William Cheng: Okay, so this EDP that's inside the staffing is the next point or points to the next document. So this way all the stack frames can be linked together in a single a linked list. 170 00:23:07.980 --> 00:23:15.390 William Cheng: OK. So again, let me say it again, all the stack frames are linked together in a single link is right here is the top stack frame. The next point over here, point to the next stack rank. 171 00:23:15.600 --> 00:23:24.900 William Cheng: And then the EP point of your point to the next segment. So therefore, all the staff room are linked together. So if you have a single a linguist and all the second are linked together, right. What else do you need 172 00:23:25.170 --> 00:23:33.000 William Cheng: You need a pointer to point to the head of the linked lists. OK, so the head of the links is inside the CPU it's inside the BP register. 173 00:23:34.860 --> 00:23:44.340 William Cheng: Okay, so therefore you know for some CPU, the entire call a BP. There are some other CPU. They call this the frame pointer or FP over here frame pointer. 174 00:23:44.910 --> 00:23:51.090 William Cheng: So what is the ideal over here at it points to the head of the link list that link all the sacrum together and that's why it's called the frame pointer. 175 00:23:52.530 --> 00:23:57.210 William Cheng: Okay, so, so for intel is called MVP, so you can also refer to them as the frame pointer. 176 00:23:58.890 --> 00:24:08.550 William Cheng: Right. Okay, so, so, so again, so we're going to show you how this link list has built. So this way you know all the suffering or LinkedIn. Yeah. 177 00:24:09.090 --> 00:24:11.340 William Cheng: And so why is it also called the base pointer. 178 00:24:11.910 --> 00:24:19.020 William Cheng: Guys EDP beast. That's what base point or the base point is like a base of an array, right, if you have the base point or if you want to access any elements that is that the array. 179 00:24:19.230 --> 00:24:24.180 William Cheng: You will add integers to it. And this way you could be asked, you get the address of elements inside the array. 180 00:24:24.600 --> 00:24:31.170 William Cheng: Okay, so that's why this is also known as the base pointer. If you have an address right here pointing right here. How do you get to the local variables. 181 00:24:31.620 --> 00:24:37.050 William Cheng: What you're going to add negative numbers to a VP, and you're going to get the address for local variables. 182 00:24:37.890 --> 00:24:45.840 William Cheng: Great. So it's kind of weird that you need to add negative number. But again, you know, if you have an array index, you have an array, you know it's wrong to use a negative or a index. 183 00:24:46.620 --> 00:24:52.980 William Cheng: OK. So again, you need to do that very carefully the compiler might not like like it but if you can get around the compiler. There's actually nothing wrong with 184 00:24:53.400 --> 00:25:01.770 William Cheng: With accessing data that is a negative address because all you're doing is pointer math. Okay, so therefore it's actually okay to go in a negative direction. Yeah. 185 00:25:02.670 --> 00:25:09.510 William Cheng: Alright, the next pointer over here is noisy IP. So over here, it says he IP contains the caller's instruction pointer. 186 00:25:09.870 --> 00:25:17.760 William Cheng: OK, so the color over here is the main function over here right when main function called the sub routine over here, we need to remember where to return to in the main function. 187 00:25:18.540 --> 00:25:25.800 William Cheng: Whereas over here, we start the car right here right main course up. Okay. So in this case, when you return. What do you need to return to when when we return so 188 00:25:26.430 --> 00:25:34.260 William Cheng: Let's back up a little bit. So when you're executing this co inside the CPU. We have a VIP register so yeah i p will be pointing to this instruction right here. 189 00:25:35.820 --> 00:25:43.050 William Cheng: Okay, so in this case we need to save tip register, because when we start executing code inside the sub routine, then we need to have VIP pointed a call right here. 190 00:25:43.500 --> 00:25:47.430 William Cheng: Right, so we need to save the contact. So, so we're going to take the VIP register over here. 191 00:25:47.610 --> 00:25:52.860 William Cheng: We're going to save it inside this document right here. And then when we are about to return back into the main function over here. 192 00:25:53.040 --> 00:26:00.540 William Cheng: We need to go to the stack frame over there and then pull out the P value over here, put it back into the CPU, so we can continue to execute a function inside me. 193 00:26:02.040 --> 00:26:16.650 William Cheng: Alright. So over here we wrote it as a VIP some other textbook by my my right here says return address. Okay. So over here we IPOs return address. So this way you know where to return to where you need to go back into the main function. 194 00:26:17.790 --> 00:26:28.740 William Cheng: Then finally the last point is that a stipend, what we hear is called the function arguments over here. And we saw that before the function argument is x and y. So, therefore, you will say access here. Why is yours up like that. OK. So again, 195 00:26:28.980 --> 00:26:38.730 William Cheng: That's part of the you know that that's part of the stack rank and you can see now inside the stack frame we have the local variable and function argument they live inside the staggering. 196 00:26:39.780 --> 00:26:47.910 William Cheng: Okay. And the way that you try to get to the function argument. How'd you. How'd you get to the function argument, right, I have the EDP register over here. If I add positive addresses to her. 197 00:26:48.120 --> 00:26:55.050 William Cheng: I can access function arguments if I add negative addresses to it or negative also to it. I will end up accessing local variables. 198 00:26:56.610 --> 00:27:07.530 William Cheng: Guys again abt EDP has dual purpose. One is the base address that you can add and subtract addresses from so you can access function argument local grab all is also used as a frame pointer. 199 00:27:07.710 --> 00:27:14.490 William Cheng: Because the link all the stack rank together and the head of the link lists goes inside the BP a pointer inside the CPU. 200 00:27:16.500 --> 00:27:26.040 William Cheng: Alright, so, so, so again the staff room are linked together. So this way when you get a break point is I, you know, use that use HGTV when you type where it shows you all the function 201 00:27:27.270 --> 00:27:32.010 William Cheng: Sort of show you all the functions that attack where they also show you that way. You, you know, we try to call a function 202 00:27:32.220 --> 00:27:37.440 William Cheng: What are the function arguments. And if you switch to a different frame can actually print out all the local variable. So yeah, there's no magic. 203 00:27:38.070 --> 00:27:44.040 William Cheng: Right. So what it does is that usually pp pointer and you know the stack rooms are linked together. So therefore, they can actually display all that information for you. 204 00:27:44.970 --> 00:28:00.120 William Cheng: Then maybe just because you know everything running out of CPU x86 CPU, they will all follow the same convention. Yeah. So yeah, another another reminder that the textbook. Flip the picture over here for us the low addresses that a tab over here in the high just that about it. Yeah. 205 00:28:02.400 --> 00:28:07.740 William Cheng: Alright, the next thing over here it says yes. Okay. So as it turns out, when you call a function 206 00:28:08.100 --> 00:28:21.300 William Cheng: You know, and when you return to the main function all the register you need to keep you need to restore the context. There's only one exception, yes you are allowed to mess up. Okay. As it turns out, yeah actually store the return value of a function 207 00:28:22.320 --> 00:28:33.330 William Cheng: Okay, so this is why, you know, typically, when we look at look at all the functions, the return value over here is going to be at most a 32 bit integer, because the return value over here. It's going to get stored inside. Yes. 208 00:28:34.500 --> 00:28:44.880 William Cheng: Okay. So whenever you have a function, right, like the example that we saw before we hear when you return value over here. Will you return results, you need to put result into EAS right so you can, yes, a general purpose operating system. So, 209 00:28:45.270 --> 00:28:46.530 William Cheng: Yes, a general purpose. 210 00:28:46.920 --> 00:28:57.120 William Cheng: You know, register over here. So when you return to the cell function over here the return value will be stored inside. Yeah, so I just here. So if you want to say i equals to the return value what you take the value 211 00:28:57.360 --> 00:29:06.480 William Cheng: Inside the X register and you put into the memory location that's known as that. Okay, so that's how you get the return value over here and then stored in memory location I get 212 00:29:08.280 --> 00:29:20.430 William Cheng: Alright, so that's the, that's one important function of yours is I store the return value of a function. Okay. Over here, it says some fields are not always present the compiler get to decide, okay, so does every function or 213 00:29:20.910 --> 00:29:27.060 William Cheng: Required to have all these five things. Okay. So, in the worst case, they will have these five things is your program allowed to have 214 00:29:27.720 --> 00:29:33.240 William Cheng: Not have any local variables. Well, of course you know each other right the function. If you don't want any local variables within this will be empty. 215 00:29:34.080 --> 00:29:40.770 William Cheng: Okay, what about functional arguments can you write a function that doesn't have any function argument. Well, that's also allowed. Right. So, therefore, this function. The arguments also allow 216 00:29:41.070 --> 00:29:51.090 William Cheng: As it turns out, you don't have local variable here. We don't have function arguments over here, then in that case, you don't even have to story save ep, ep can actually point to the previous diagram below it. 217 00:29:51.990 --> 00:30:02.250 William Cheng: There's. Why is that okay right because because you don't need to access function argument local variable. Okay, so in this case EDP point to the next guy friend because he BP still linked all the sacraments together. 218 00:30:02.760 --> 00:30:11.370 William Cheng: Well, yeah, right. Starting with this diagram, you know, all the VP over here link all the platforms together and a VIP. In this case it will point to the top stack rank. What this will be the top segue. 219 00:30:12.090 --> 00:30:16.410 William Cheng: Okay. Is that okay, well it's perfectly okay because this stuff over here. We don't need anything. 220 00:30:16.950 --> 00:30:23.700 William Cheng: Right. We don't function argument was only a local variable. So, therefore, we don't really care what he points to. Okay. Oh, we need to do is to make sure when we reach up on this function. 221 00:30:23.970 --> 00:30:28.590 William Cheng: EDP will point to the right place so that we can actually point to the next frame that's perfectly okay 222 00:30:28.980 --> 00:30:40.410 William Cheng: Yeah. What about the same register over here. Do you have to save any registered law make you feel function doesn't really do anything. Okay. It doesn't mess up any of the CPU register why in this way. You have nothing to say. So in this case, this can also be empty. 223 00:30:41.820 --> 00:30:48.780 William Cheng: So instead of staff frame only one thing is required and that will be the VIP register, do you have to say the IP register. 224 00:30:49.500 --> 00:30:52.440 William Cheng: Well, yeah, you have to read because otherwise how do you return back to the main function. 225 00:30:53.280 --> 00:31:04.230 William Cheng: Okay. So, therefore, you have to say the return address all the other four parts over here. They're optional. OK. So again, the compiler will figure this out. And then he will try to sort of decide how biggest airframe is there. 226 00:31:06.600 --> 00:31:11.400 William Cheng: Alright, so, so you know when you try to construct the stock rain a who construct the sacrament. 227 00:31:12.000 --> 00:31:18.150 William Cheng: Okay. So over here, it says the arcs are explicitly set up by the color right so again we go back to the example we get there. 228 00:31:18.840 --> 00:31:26.370 William Cheng: You know when you try to call this a function. Where does X come from, right. So this case, you know, if you look at the code over here x COM FOR X come from the value of a 229 00:31:27.540 --> 00:31:34.230 William Cheng: Okay, why come from the value of one. Right. And the other example, I say the main functions are called x as going to call why now why is gonna 230 00:31:34.800 --> 00:31:42.360 William Cheng: Is gonna set x equal to 57 and y equal to 24. Okay. So in our case, we have 5724 come from a different function. 231 00:31:43.290 --> 00:31:54.600 William Cheng: Okay, so therefore, x and y, they have to come from the color function, right, because every time you call the function sub maybe it's called from a different place, so only the color function would know what x and y supposed to be. 232 00:31:55.290 --> 00:32:00.780 William Cheng: Okay, so therefore over here, we say that the RSI explicitly set up by the call it 233 00:32:01.740 --> 00:32:08.370 William Cheng: Okay there. So, so again, our in our example main function is the color, the sub function is the call leads. That's one of the being called 234 00:32:08.730 --> 00:32:16.680 William Cheng: Now, what about the next one, or VIP okay so yeah i p is copy into the stack rank by a call machine structure in the color function. 235 00:32:16.890 --> 00:32:25.530 William Cheng: Right, the color functioning these major function call. As it turns out, there's a machine instruction called call. And then the other sort of machine should look like this. This call sub 236 00:32:26.640 --> 00:32:37.950 William Cheng: Now, that will be the machine surgeon. So we execute a call submachine instruction over here, what he will do is it will take the CPU that take the VIP red shootings data CPU and then what we'll do is, is that it will copy down to the sack. 237 00:32:38.430 --> 00:32:43.920 William Cheng: Okay, so therefore he IP is copied into the stack ranked by the call machine structure inside call a function 238 00:32:45.060 --> 00:32:56.160 William Cheng: Okay, what about the BP registered over here, right. So as it turns out that, you know, so once you make the once you execute a call machine structuring over here. Now we're in the context of the cell function. 239 00:32:56.970 --> 00:33:06.150 William Cheng: Right, because the call you on Wednesday, call us up. We're gonna, you know, we're going to save the return address over onto the stack and also we're going to replace the VIP register with the address of sub 240 00:33:06.840 --> 00:33:16.380 William Cheng: Okay, so once we replace the IP address or somewhat now we're executing the code for this up. Okay, so therefore the rest of the staff room over here has to be set up by the sub function. 241 00:33:17.250 --> 00:33:26.160 William Cheng: Maybe there's once you actually the so called machine start you know you don't know you the context of sub so all the other stuff or say inside the inside the contents of the cell function. Yeah. 242 00:33:26.940 --> 00:33:32.640 William Cheng: Right, so he BP is copy explicitly by the Cali and later on we're going to see exactly how this is done. Yeah. 243 00:33:32.880 --> 00:33:42.660 William Cheng: All these other register only the call a function know which register register need to be saved. Right. So, what it will do is it will take the register inside of CPU and then it will start saving them. 244 00:33:43.110 --> 00:33:53.190 William Cheng: Into this part of the stack rank. Okay. And then what it will do that, then what it will do is that the space for the local variable is created explicitly by the quality code. 245 00:33:53.730 --> 00:34:01.710 William Cheng: Okay, the call the function again, the code is generated by the compiler, the compiler knows how big of a stack of how big of a space, it needs for the local variable. 246 00:34:02.340 --> 00:34:15.000 William Cheng: Okay. So in our example over here, right, the compiler will compile disco say, oh, you have to local variable. One is the integer it. The other one is engine result, each one of them take four bytes. So I need to create the space over here for a bite for iron results. 247 00:34:16.620 --> 00:34:27.090 William Cheng: OK, so the space over here, over here is exactly a bias long in our example. Right. So this space is created by the call eco so please understand that the local variables are not initialized. 248 00:34:27.690 --> 00:34:36.150 William Cheng: Okay, so therefore, when you you know when you create a stack rank the local variable will be initialized to be whatever garbage that you left inside the stack space. 249 00:34:36.960 --> 00:34:43.860 William Cheng: Okay, so whatever the values are just happened to be there, they will be the initial value for your local variable. Okay, so why don't you initialize a local variable. 250 00:34:44.910 --> 00:34:47.610 William Cheng: I don't know. Because, you know, I guess people try to run fast. 251 00:34:48.000 --> 00:34:54.990 William Cheng: So don't bother to initialize local variable values over here where it will be nice if there are initialized, whereas it this way. We won't pick up garbage. But again, 252 00:34:55.260 --> 00:35:04.710 William Cheng: As far as I know, for Unix and Linux system local variables are never initialized. Now, right. So, therefore, if you want to initialize them you have to write your own code to make sure that they're initialized. 253 00:35:05.460 --> 00:35:17.790 William Cheng: That alright so here is the first question over here. What does this look like for the following function right here is a function has no function or not local function argument. So this is God no local variable local variable years no local variable. 254 00:35:18.960 --> 00:35:28.590 William Cheng: So EP. So again, we don't need a VP right if you have no function very little local variable no functionality around with me say local variable. So, in this case EDP will point to the next stack rank. Okay. 255 00:35:29.190 --> 00:35:39.240 William Cheng: This is have saved register. Okay, so this function we simply call another function. Okay. So, therefore, in this case we don't mess up any of the CP register. So in this case, we also don't need to save any registers. 256 00:35:40.320 --> 00:35:50.010 William Cheng: Right, right. So, therefore, get the stack rank for this function was simply container return address. Okay, so this way, whatever function that calls you, you know how to return back to that function. Yeah. 257 00:35:52.860 --> 00:36:01.230 William Cheng: What about between the sacrament, can you store anything right. So over here, it says in reality right between the stack frame over here you can store anything you want, between the stack. Right. 258 00:36:02.160 --> 00:36:09.720 William Cheng: Okay, especially if you write code you're in assembly language right over here. You know, I read some some kind of code over here in Z i read some other phone call in and see 259 00:36:10.440 --> 00:36:13.560 William Cheng: If you write code assembly language, you can actually put anything in between the sack. Right. 260 00:36:14.520 --> 00:36:21.480 William Cheng: Okay, so, so, by, by convention, some of the register over here. Our state between the stack rank and then 261 00:36:21.930 --> 00:36:28.020 William Cheng: You know, so again, this can be depend on the compilers can depend on a lot of other different factors. Been a while to kind of operating system you're running 262 00:36:28.260 --> 00:36:35.850 William Cheng: Maybe depends on the CPU or something like that. Okay, or CPU configuration or whatever. So as far as this class is concerned would assume there's nothing in between that 263 00:36:36.870 --> 00:36:48.390 William Cheng: Okay, we're going to assume that on couple ones that frame just have enough background, right, knowing that in reality right there could be something between the stack. Right. Okay. But it's going to simplify our discussion to say that there's nothing between the separate yeah 264 00:36:50.280 --> 00:37:01.440 William Cheng: Alright, so the next thing that we're going to do that we're going to look at the example code over here. So, so we're going to look at assembly code, right. So the first question I will always get to we have to memorize this for the exam. 265 00:37:02.490 --> 00:37:16.290 William Cheng: Okay, so what we're going to do for the exam is that in the exam. I will not ask you to write assembly code. Okay, I will not ask you to write assembly code. But if it's assembly code that we have gone over in class. If I asked you, What is it assembly code doing over here, you need to know. 266 00:37:17.370 --> 00:37:23.550 William Cheng: Okay, so get you, you won't have to write it. But, you know, if I asked you. Something I explained for you already in class. 267 00:37:23.760 --> 00:37:31.740 William Cheng: If I didn't explain what we're in class. You've been you're not responsible for it. Right. But if I explain the code in class, then you are responsible for it as far as exam is concerned. 268 00:37:32.190 --> 00:37:40.590 William Cheng: Yeah. Alright, so the the co op is actually pretty complicated. You can see that will be here. I don't even consider devices, a textbook is wrong. Okay so calculus textbook actually get some of these types of wrong. 269 00:37:41.340 --> 00:37:46.980 William Cheng: OK, so again you know textbook. There's no way for me to modify. So therefore, I can only modify lecture slides, guys, so therefore over here I'm 270 00:37:47.340 --> 00:37:52.860 William Cheng: Telling you that the textbook is wrong, make you understand. Yeah. So next I'm going to go over this code over here. 271 00:37:53.220 --> 00:38:02.280 William Cheng: Okay, so again, the idea here is I'm studying in the main function over here. Here's a stat waiting for me at some point, I'm going to call the sub routine right over here, this is the one I mentioned before, call us up. 272 00:38:02.970 --> 00:38:12.270 William Cheng: Okay, so then I need to build a segment for sub make the make the function call, say the contacts and then switch to a different context. Go the contact for South by building all the site for a 273 00:38:12.510 --> 00:38:18.810 William Cheng: Call this function. And when sub return. We're going to sort of tear everything down. We're going to return back to what words before and then to make sure that 274 00:38:19.050 --> 00:38:26.520 William Cheng: All the CP register has been restored except for yes okay and then we're going to continue to execute over here and I'm going to go through all the code over here. 275 00:38:26.670 --> 00:38:37.860 William Cheng: And eventually, you may also need to return. Right. So in this case, we may return. What is the return to what, there's a startup function at the bottom over here. He also has his own stack RAM. So again, we need to we need to, again, restore the context to the startup function. 276 00:38:39.330 --> 00:38:43.560 William Cheng: Alright, so as I'm going to stop right now. So next time we're going to continue for this point. Yeah.