1 00:00:07,040 --> 00:00:08,480 Hello, everyone, and welcome back. 2 00:00:08,760 --> 00:00:15,620 So let's start the last time we ended up here where we said talked about the stark frames and always, 3 00:00:15,620 --> 00:00:20,690 again, just remember, always the current active static frame is number zero. 4 00:00:20,960 --> 00:00:24,790 The previous one will be one, the previous will be two and so on and so forth. 5 00:00:25,100 --> 00:00:28,410 Just make sure you remember that the current active stack is number zero. 6 00:00:29,000 --> 00:00:35,750 Now managing stack frames, now the current stack frame is always the newest frame and a start frame 7 00:00:35,750 --> 00:00:41,140 gets created when you call when a function is done. 8 00:00:41,150 --> 00:00:47,050 So when you when your program calls a function, that's when we start playing with with a with a frame 9 00:00:47,060 --> 00:00:50,890 or that's when the stack frame comes into the player of all or all of this. 10 00:00:51,320 --> 00:00:57,600 So E.S.P points to the top of the current stack frame and it points to the top of the stack as well. 11 00:00:57,620 --> 00:00:57,950 Why? 12 00:00:57,960 --> 00:01:00,110 Because we just started our program. 13 00:01:00,110 --> 00:01:01,970 Let's say let's just think of it like this. 14 00:01:02,240 --> 00:01:03,670 We just started our program. 15 00:01:03,680 --> 00:01:07,580 So we are now going to code mean so E.S.P will be top. 16 00:01:07,610 --> 00:01:12,880 We we will now be in stock from zero and the E.S.P will be pointing to the top of the stack. 17 00:01:13,280 --> 00:01:18,200 Now, whenever a function is called, a new start frame is created again. 18 00:01:18,500 --> 00:01:23,480 Now, meaning, let's say when we looked at the previous code main called Myfanwy one. 19 00:01:23,900 --> 00:01:27,980 When we want to do that, we will be creating a new stack for him, not us, but the system. 20 00:01:27,980 --> 00:01:30,880 I mean, just just keep that in mind. 21 00:01:30,890 --> 00:01:32,340 We are not the ones doing this. 22 00:01:32,870 --> 00:01:39,400 So with that happening, local variables also are going to be allocated at the bottom of the creative 23 00:01:39,410 --> 00:01:39,680 stack. 24 00:01:40,130 --> 00:01:48,140 So once a new start frame has created, any local variables in that function will be added to the stack. 25 00:01:48,950 --> 00:01:50,690 At the bottom of the stack, OK? 26 00:01:50,780 --> 00:01:54,010 So and we will see this now when we go through this. 27 00:01:54,950 --> 00:02:01,460 So let's start so to create a new start for him, simply change AVP value to be equal to E.S.P. 28 00:02:01,460 --> 00:02:02,780 So that's what's going to happen. 29 00:02:03,080 --> 00:02:06,330 SBP now will move it were to SBP E.S.P. 30 00:02:06,560 --> 00:02:11,520 So now SBP equals E.S.P because now we want to create a new stock for him. 31 00:02:11,540 --> 00:02:13,190 And again, why do we need to do that? 32 00:02:13,190 --> 00:02:21,800 Because EBP will always be pointing to the base of that active stock from SBP again is always pointing 33 00:02:21,800 --> 00:02:23,570 to the base of the active stock. 34 00:02:24,440 --> 00:02:27,040 So now IDP's equal to E.S.P. 35 00:02:27,080 --> 00:02:29,240 This means that the new stock frame is what? 36 00:02:29,240 --> 00:02:34,640 It's empty like, like the the slide says and the previous stock frame now is indexed. 37 00:02:34,640 --> 00:02:39,950 Astatke that number one, because this is like frame number zero, the one we are creating now. 38 00:02:39,950 --> 00:02:41,420 But we have a problem here. 39 00:02:41,480 --> 00:02:46,100 Hey, so how are we let's say we finish our function. 40 00:02:47,980 --> 00:02:54,070 The computer or the system finishes executing that function, which is my first one, maybe, and we 41 00:02:54,070 --> 00:02:55,160 want to go back to me. 42 00:02:55,210 --> 00:03:02,620 How are we going to go back to I mean, we don't know where the base of the main stream or the stock 43 00:03:02,620 --> 00:03:04,080 frame for the function mean. 44 00:03:04,090 --> 00:03:05,240 We don't know where that is. 45 00:03:05,410 --> 00:03:06,700 So how are we going to do that? 46 00:03:07,150 --> 00:03:08,120 So let's try again. 47 00:03:08,290 --> 00:03:12,120 OK, but this time we'll need to save Gbps value before changing it. 48 00:03:12,310 --> 00:03:18,700 So before we change it, just not not rushing into things, just saying, yeah, let's keep it, let's 49 00:03:18,700 --> 00:03:20,200 make it equal to S.P.. 50 00:03:20,320 --> 00:03:20,860 It's good. 51 00:03:20,860 --> 00:03:22,720 But we missed something. 52 00:03:22,720 --> 00:03:28,390 And that mess made us actually not able to go back to our previous frame. 53 00:03:28,450 --> 00:03:30,100 So let's repeat the same steps. 54 00:03:30,100 --> 00:03:32,600 But this time we will save EBP. 55 00:03:33,160 --> 00:03:37,290 So first thing pushed the value of BP to save it were on top of the stack. 56 00:03:37,300 --> 00:03:38,980 So that's what's going to happen now. 57 00:03:38,990 --> 00:03:40,150 We copied that value. 58 00:03:40,150 --> 00:03:41,680 We're on top of the stack. 59 00:03:41,830 --> 00:03:42,880 What happened to E.S.P? 60 00:03:42,880 --> 00:03:47,920 Look what happened to E.S.P and back and forth E.S.P changed. 61 00:03:47,920 --> 00:03:48,280 Why? 62 00:03:48,280 --> 00:03:51,610 Because E.S.P again, is always pointed to the top. 63 00:03:51,620 --> 00:03:56,370 And since we pushed a value on top of the stack, then E.S.P also changed. 64 00:03:56,830 --> 00:03:59,800 Now change the value of because we started. 65 00:03:59,800 --> 00:04:03,960 So we currently have the value of EBP on top of the stack. 66 00:04:04,300 --> 00:04:05,560 So now we can change it. 67 00:04:05,560 --> 00:04:05,860 Why. 68 00:04:05,860 --> 00:04:07,930 Because we don't need to worry about it. 69 00:04:07,930 --> 00:04:08,830 It's already there. 70 00:04:08,830 --> 00:04:10,390 We, we saved it. 71 00:04:10,390 --> 00:04:11,760 We can come back to it later. 72 00:04:12,400 --> 00:04:14,260 Now SBP equals to E.S.P. 73 00:04:14,260 --> 00:04:16,930 So now we are creating a new stack for good. 74 00:04:17,020 --> 00:04:17,760 Let's continue. 75 00:04:18,730 --> 00:04:21,490 This is what is called, by the way, the prologue. 76 00:04:22,030 --> 00:04:23,760 These operations is called the prologue. 77 00:04:23,770 --> 00:04:28,480 So the prologue is creating a new start frame, then allocating space for local variables. 78 00:04:28,480 --> 00:04:35,450 So assuming we have some function which requires local variables, we will be creating when we create 79 00:04:35,450 --> 00:04:42,010 the static frame and then use that frame and allocate space for those local variables that start of 80 00:04:42,010 --> 00:04:43,800 operations is called prologue. 81 00:04:44,020 --> 00:04:46,340 OK, let's continue. 82 00:04:46,870 --> 00:04:48,820 So now push and pop operations. 83 00:04:48,820 --> 00:04:49,600 Keep that in mind. 84 00:04:49,600 --> 00:04:52,690 Push and pop operations affect E.S.P value only. 85 00:04:53,410 --> 00:04:59,180 Remember, when we push a value, then the the top of the stack needs to change. 86 00:04:59,200 --> 00:05:03,460 So we need the E.S.P registered to always point on the top of the stack. 87 00:05:03,640 --> 00:05:06,520 So it's going to change when we remove the value from the stack. 88 00:05:06,700 --> 00:05:08,550 E.S.P is going to also change. 89 00:05:08,920 --> 00:05:14,890 So and just keep in mind, when we push a value, the the address is going to be decreasing when we 90 00:05:14,920 --> 00:05:16,180 when we paper value. 91 00:05:16,420 --> 00:05:22,810 The address is going to be increasing y again because as you can see the the memories upside down. 92 00:05:22,810 --> 00:05:26,040 Keep that in mind and I'm going to continue to remind you about that. 93 00:05:26,770 --> 00:05:32,940 Now we don't need to see Vesp y for the previous Thackray because SPG actually equal to the SBP value. 94 00:05:33,130 --> 00:05:40,090 We know the top of the previous stack frame is now actually the EBP value, so we don't need to save 95 00:05:40,090 --> 00:05:40,170 it. 96 00:05:40,180 --> 00:05:41,570 We already have it saved. 97 00:05:41,620 --> 00:05:43,390 OK, the system already saved that. 98 00:05:43,390 --> 00:05:47,890 So we don't need to worry about where was the top of the previous thackray. 99 00:05:48,100 --> 00:05:52,120 We already have that value now to empty this current stack frame. 100 00:05:52,120 --> 00:05:53,050 We want to empty it. 101 00:05:53,050 --> 00:05:57,310 Let's say my fun one was done and I want to go back to mine now. 102 00:05:57,310 --> 00:06:01,380 What will happen is ESPs value should be set to the same value of SBP. 103 00:06:02,110 --> 00:06:07,000 What we need to do is just set E.S.P equal to SBP. 104 00:06:07,150 --> 00:06:14,320 So what will happen here if we set E.S.P now TBP we actually deleted or we removed or it's really not 105 00:06:14,320 --> 00:06:14,770 deleted? 106 00:06:14,770 --> 00:06:19,660 I wouldn't say because it's not that, that's not what's going to happen. 107 00:06:19,900 --> 00:06:26,290 The date is probably still there, but now we don't have access to it because the pointers have been 108 00:06:26,290 --> 00:06:26,650 removed. 109 00:06:26,920 --> 00:06:32,860 So to delete the current stack frame and return back to the previous one, we should pop out the top 110 00:06:32,860 --> 00:06:37,410 of the top, out the top value from the stack into SBP. 111 00:06:37,450 --> 00:06:39,700 OK, so now we we do that. 112 00:06:39,700 --> 00:06:47,110 Now we are pointing to E.S.P, pointing to the top of the stack, which is also the top of the previous 113 00:06:47,110 --> 00:06:47,500 iFrame. 114 00:06:48,010 --> 00:06:48,760 Let's continue. 115 00:06:50,290 --> 00:06:58,570 Again, the current stock frame is always the newest frame, and now we have since we see here now, 116 00:06:58,570 --> 00:07:07,810 since we popped out the value of EBP, so this value which ESPN, EVP both are pointing to when we pop 117 00:07:07,810 --> 00:07:08,570 that out. 118 00:07:08,710 --> 00:07:14,280 So when we do a pop SBP, that value is now going to be stored in the register. 119 00:07:14,740 --> 00:07:20,200 And what that means is now EBP will be pointing back to where it was at the beginning. 120 00:07:20,380 --> 00:07:29,200 So this is how actually navigating the stack and creating frames and creating new frames, the prologue 121 00:07:29,560 --> 00:07:34,090 removing frames, which is actually called the epilogue, this is how all of this happened. 122 00:07:34,110 --> 00:07:40,070 So Epilog is emptying the current stack frame and deleting it, then returning to the content function. 123 00:07:40,450 --> 00:07:46,960 So let's say we were in my fun one or my function one, and we finished the execution or the system 124 00:07:46,960 --> 00:07:47,980 finished the execution. 125 00:07:48,190 --> 00:07:54,190 Now my function one wants to go back to or the system wants to go back to Maine because that was the 126 00:07:54,190 --> 00:07:56,650 one who called my function one or my phone one. 127 00:07:57,140 --> 00:08:02,320 Then the epilogue is emptying the stack frame and then going back to the calling function. 128 00:08:04,020 --> 00:08:08,880 So let's look at now functions both from a low level view. 129 00:08:08,940 --> 00:08:15,010 OK, so we are we are going to understand the process by looking at the functions from a low level view. 130 00:08:15,030 --> 00:08:21,720 So a simple function called in high level language is not simple operation as it seems like when we 131 00:08:21,720 --> 00:08:27,080 do add X and Y, that's not just as easy as that. 132 00:08:27,090 --> 00:08:28,820 That's not just one single instruction. 133 00:08:29,070 --> 00:08:33,780 There's a set of instructions going to be happening behind the scene in order to do that. 134 00:08:34,230 --> 00:08:37,460 So first, what's going to happen is we are going to push the arguments. 135 00:08:37,470 --> 00:08:42,030 If there are any arguments, like if we have here X and Y, we are going to push those on top of the 136 00:08:42,030 --> 00:08:44,960 stack and then we are going to do the call to the function. 137 00:08:45,180 --> 00:08:52,590 And in order to do the function, this means we need to create a new frame for start working on the 138 00:08:52,590 --> 00:08:53,470 ADD function. 139 00:08:53,790 --> 00:08:57,260 So a prologue will happen, prolog operations will happen. 140 00:08:57,630 --> 00:08:59,010 We will execute the function. 141 00:08:59,280 --> 00:09:06,300 After the execution finishes, Epilog will jump in or the epilogue operation will jump in to clear the 142 00:09:06,300 --> 00:09:10,920 stack in the frame, go back to the previous one and then any arguments which we pushed. 143 00:09:10,920 --> 00:09:12,380 We mentioned this earlier. 144 00:09:12,810 --> 00:09:19,370 They have to be popped out with a matching set of instructions. 145 00:09:19,370 --> 00:09:23,120 So we have one push we need for that push. 146 00:09:23,460 --> 00:09:29,470 So popping the arguments that we pushed will happen at the end and that's how the execution will happen. 147 00:09:29,640 --> 00:09:31,800 Let's go to a little bit more details. 148 00:09:32,550 --> 00:09:35,910 Let's look at that now, how we are breaking things down again. 149 00:09:35,910 --> 00:09:38,850 We are we looked at the function, high level view. 150 00:09:39,120 --> 00:09:40,170 We made it now. 151 00:09:40,170 --> 00:09:45,660 We looked at the low level view, but we are now breaking things into how they really will look like. 152 00:09:46,060 --> 00:09:47,700 So, again, push the arguments. 153 00:09:47,700 --> 00:09:50,400 If we have any arguments, then we are going to push IP. 154 00:09:50,880 --> 00:09:55,560 Why we are going to push IP because IP is the position where we need to come back. 155 00:09:56,130 --> 00:10:03,030 When we start the execution, when we finish the execution of the function now jump to the functions 156 00:10:03,030 --> 00:10:03,960 first instruction. 157 00:10:03,960 --> 00:10:06,600 So we we jump to the functions first instruction. 158 00:10:07,680 --> 00:10:13,320 But in order also to create the the frame for that new function, we need to push EBP. 159 00:10:13,530 --> 00:10:21,900 So now we are saving the base pointer base of the stack on the on the stack, OK, the base pointer 160 00:10:21,900 --> 00:10:22,380 on the stack. 161 00:10:22,740 --> 00:10:25,290 Then we will set SBP to equal E.S.P. 162 00:10:25,290 --> 00:10:28,410 So now they point at the same place. 163 00:10:28,410 --> 00:10:30,180 So we now have a new function. 164 00:10:30,360 --> 00:10:36,300 If there are any local variables, we will push them, execute the function now we'll do the reverse 165 00:10:36,300 --> 00:10:37,530 thing now. 166 00:10:37,530 --> 00:10:44,010 We'll pop out our local variables so we remove all of those because those were the last thing added 167 00:10:44,010 --> 00:10:44,940 to the stack. 168 00:10:44,940 --> 00:10:48,330 So Will, again, it's it's first in, last out. 169 00:10:48,330 --> 00:10:51,840 So this was the first and so that's the last one out. 170 00:10:51,840 --> 00:10:58,320 So we are going to remove them and then we are going to pop up so this value will pop back up. 171 00:10:58,320 --> 00:11:05,760 So we are restoring the frame of the previous function and then pop IP. 172 00:11:05,760 --> 00:11:13,980 So popping the IP, this is actually where the this push operation pupping IP. 173 00:11:13,980 --> 00:11:19,350 So we are going to pop out this value, which is the position where I need to go back in order to do 174 00:11:19,350 --> 00:11:25,830 the execution or continue the execution and then finally pop the arguments which were passed on to the 175 00:11:25,830 --> 00:11:26,400 function. 176 00:11:26,640 --> 00:11:27,990 We are also going to pop them. 177 00:11:28,440 --> 00:11:33,480 OK, so they are breaking things down a little bit more and diving deeper into them. 178 00:11:33,510 --> 00:11:34,770 Let's go continue. 179 00:11:36,020 --> 00:11:41,810 Now, again, as you can see here, each push operation must be reversed by a proper operation somewhere 180 00:11:41,810 --> 00:11:42,680 in the execution. 181 00:11:42,800 --> 00:11:50,240 So as you can see here, this push for local variables was reversed with this pop, this push SBP was 182 00:11:50,240 --> 00:11:55,520 reversed with this pop, this push i.p was reversed with this pop hip. 183 00:11:55,820 --> 00:12:01,550 And then this push for the arguments was reversed by this pop of the arguments. 184 00:12:01,700 --> 00:12:03,040 OK, so keep that in mind. 185 00:12:04,220 --> 00:12:08,140 Now, performing a push up argument is done by the colored function. 186 00:12:08,150 --> 00:12:13,250 So the caller, let's say the main was to call my friend one or my function one. 187 00:12:13,460 --> 00:12:16,460 So the caller will push those values onto the stock. 188 00:12:16,760 --> 00:12:19,660 Arguments are going to be pushed in reverse order. 189 00:12:20,210 --> 00:12:22,370 Arguments are going to be pushed in reverse order. 190 00:12:22,640 --> 00:12:24,380 So as you saw previously. 191 00:12:26,850 --> 00:12:32,130 Here in this one, like add X Y, we will be pushing them in reverse order. 192 00:12:32,160 --> 00:12:35,050 So just keep that in mind and you're going to see that in a minute. 193 00:12:35,520 --> 00:12:41,790 Now, performing a pop argument, pop performing puppet arguments can be done by the caller or the calling 194 00:12:41,790 --> 00:12:42,300 function. 195 00:12:42,770 --> 00:12:44,250 Again, the pop. 196 00:12:45,590 --> 00:12:52,730 Of the arguments will be done either by the caller or the COLLY, so when when we want to call on and 197 00:12:52,730 --> 00:12:56,890 what if we go back to the mind, the mean and McPhun one? 198 00:12:57,650 --> 00:13:04,400 So if me calls my phone, one man will be considered the caller and my phone one will be considered 199 00:13:04,400 --> 00:13:04,850 the caller. 200 00:13:05,310 --> 00:13:05,730 OK. 201 00:13:06,720 --> 00:13:12,390 Keep that in mind, Maine is going to be considered the caller and my friend one is going to be considered 202 00:13:12,780 --> 00:13:12,990 the. 203 00:13:13,830 --> 00:13:20,610 OK, now who's going to be doing the pop of those arguments which the caller pushed on the stack? 204 00:13:20,790 --> 00:13:23,010 So the caller puts those arguments on the stack. 205 00:13:23,580 --> 00:13:26,850 Who's going to be popping those arguments out of the stack? 206 00:13:26,850 --> 00:13:27,760 Who's going to be doing that? 207 00:13:28,080 --> 00:13:29,910 That depends on the call type. 208 00:13:30,090 --> 00:13:34,520 OK, so this is specified by the call type of the function. 209 00:13:34,740 --> 00:13:36,780 OK, we're going to see what that is. 210 00:13:37,080 --> 00:13:38,100 This is specified. 211 00:13:38,700 --> 00:13:45,990 Who's going to do that as specified by the type of sorry, the call type of the calling function. 212 00:13:47,010 --> 00:13:51,620 So return value of the caller call is served inside Ekso. 213 00:13:51,660 --> 00:13:52,650 There is a return value. 214 00:13:52,650 --> 00:13:55,560 Let's say you did add X and Y, you now return. 215 00:13:55,560 --> 00:14:03,180 You want to return the sum of X and Y, that value is going to be returned into the X register while 216 00:14:03,180 --> 00:14:05,140 the execution of the functions body. 217 00:14:05,160 --> 00:14:17,280 So once the function is body is done, now that value which was to some, to some X and Y will be stored 218 00:14:17,280 --> 00:14:18,840 in X, keep that in mind. 219 00:14:19,110 --> 00:14:24,390 It doesn't have to mean, by the way, any function which will return a value, because in our case 220 00:14:24,390 --> 00:14:26,880 it was at X and Y in any function. 221 00:14:26,880 --> 00:14:34,110 Let's say it will return, let's say a memory location, it will return a subtraction, it will do any 222 00:14:34,110 --> 00:14:37,440 kind of operation, that return value will be stored. 223 00:14:37,680 --> 00:14:45,150 If it's a 32 bit value or it can fit within a 32 bit value will be stored into X or keep that in mind. 224 00:14:47,080 --> 00:14:52,890 Now functions low level view, but this time focusing on cold types, now programming languages provide 225 00:14:52,900 --> 00:14:57,910 a mechanism to specify the cold type of the function programming languages. 226 00:14:58,390 --> 00:15:04,460 They can provide the mechanism or they provide some kind of mechanism that you can specify the call 227 00:15:04,480 --> 00:15:05,560 type of the function. 228 00:15:05,800 --> 00:15:09,610 Now, the call type guys is not a return value type. 229 00:15:09,740 --> 00:15:14,950 OK, so we're not talking about the return value like integer void card or whatever. 230 00:15:14,980 --> 00:15:16,660 That's not what we are talking about. 231 00:15:17,350 --> 00:15:25,150 This is how the mechanism which we will be dealing with or the system will be dealing with and who is 232 00:15:25,150 --> 00:15:27,910 going to be responsible of cleaning the stack. 233 00:15:27,910 --> 00:15:35,080 So the caller needs to know the call type of the cauli, OK, to specify how arguments should be passed 234 00:15:35,080 --> 00:15:37,740 and how that frame should be clean again. 235 00:15:38,020 --> 00:15:45,820 Mean in order to call my phone, one needs to know what type what is the call type of my phone one. 236 00:15:46,240 --> 00:15:46,600 Why? 237 00:15:46,600 --> 00:15:54,030 Because based on the call type, the caller will decide who's going to be responsible of cleaning this 238 00:15:54,030 --> 00:15:55,120 stack frame. 239 00:15:55,420 --> 00:15:57,660 Is it going to be the argument? 240 00:15:57,680 --> 00:16:00,700 I mean, is it going to be the COLLY or is it going to be the caller? 241 00:16:00,730 --> 00:16:02,920 So let's see that in in some example. 242 00:16:03,340 --> 00:16:11,350 Now, we are let's just talk about two quick, two quick different types of call types. 243 00:16:11,350 --> 00:16:12,430 So these are two of them. 244 00:16:12,440 --> 00:16:14,170 These are the most commonly used ones. 245 00:16:14,380 --> 00:16:17,320 See declaration, which is the call type for functions. 246 00:16:17,530 --> 00:16:20,890 Usually you can find this on Linux systems. 247 00:16:20,890 --> 00:16:27,250 So the caller is responsible for cleaning the stack from the caller, is responsible for cleaning the 248 00:16:27,250 --> 00:16:27,820 stack frame. 249 00:16:27,910 --> 00:16:34,040 So if the caller is going to call a function and it's called type of C declaration or C, they can, 250 00:16:34,390 --> 00:16:37,330 then the caller will be responsible of doing what? 251 00:16:37,330 --> 00:16:38,370 Of doing the cleaning. 252 00:16:38,830 --> 00:16:45,550 Now, as to Standard Caller, which is a default call for Win32 API, which is used on Windows, the 253 00:16:45,550 --> 00:16:48,310 Cawley's responsible of cleaning, cleaning the stack. 254 00:16:48,440 --> 00:16:53,800 OK, so the call the caller, let's say main cause my phone one. 255 00:16:54,490 --> 00:17:00,730 And if it's using a static call, if my phone one is using a static call, then my phone one will be 256 00:17:00,730 --> 00:17:03,090 responsible of cleaning the stack frame. 257 00:17:03,610 --> 00:17:04,780 Let's see it individually. 258 00:17:05,470 --> 00:17:05,860 So. 259 00:17:07,390 --> 00:17:11,930 Again, if we go back to this, each of these steps are processed by one or many instructions. 260 00:17:11,950 --> 00:17:20,160 Again, this is just like some visual view of what's happening, but it might be more than these instructions, 261 00:17:20,170 --> 00:17:20,490 OK? 262 00:17:20,530 --> 00:17:28,120 And we will see that actually in in a couple of minutes now as like as like as other programming languages. 263 00:17:28,120 --> 00:17:31,100 Assembly provides many ways to perform the same operation. 264 00:17:31,510 --> 00:17:36,250 Therefore, the disassemble code can vary from one compiler to the other another. 265 00:17:36,640 --> 00:17:44,740 What this means here, and in case you are reading it, is sometimes we will see a push and pop, but 266 00:17:44,740 --> 00:17:51,730 other times we will have probably some other ways of adding and removing values from the stack. 267 00:17:52,000 --> 00:17:55,040 Now, this will depend on optimization. 268 00:17:55,210 --> 00:17:59,990 We will talk a little bit about that after probably this video in the next video. 269 00:18:00,190 --> 00:18:09,550 OK, but these operations will be different depending on the compiler and how the how the compiler actually 270 00:18:09,550 --> 00:18:10,390 compiled the code. 271 00:18:10,420 --> 00:18:14,950 OK, so this will be different from one compiler to another. 272 00:18:15,140 --> 00:18:16,300 OK, keep that in mind. 273 00:18:16,810 --> 00:18:19,530 But this is the base of all of them. 274 00:18:19,540 --> 00:18:25,690 At the end, they even if they change them, even if they change these instructions, the concept is 275 00:18:25,690 --> 00:18:26,260 the same. 276 00:18:26,590 --> 00:18:33,610 But what's happening is at the lower end of the instructions, it's different for optimization reasons. 277 00:18:33,820 --> 00:18:40,000 OK, so now we are going to go going to introduce the default way of performing each of these steps 278 00:18:40,000 --> 00:18:41,170 using assembly language. 279 00:18:41,200 --> 00:18:46,240 Again, this is the default or the basic way of how these operations are going to actually happen. 280 00:18:47,050 --> 00:18:47,850 Let's continue. 281 00:18:48,160 --> 00:18:50,950 So we have here side by side the declaration. 282 00:18:50,950 --> 00:19:00,310 And then second, we can see here that the first to the one and this one was done are things done by 283 00:19:00,310 --> 00:19:00,950 the caller. 284 00:19:00,970 --> 00:19:03,680 And so these are the operations done by the caller. 285 00:19:04,120 --> 00:19:05,940 These are the Cawley's operations. 286 00:19:05,950 --> 00:19:10,540 So these are things done by the Collee and then these are operations done by the caller again. 287 00:19:10,790 --> 00:19:12,430 OK, now. 288 00:19:12,640 --> 00:19:19,090 And so this is when we have the declaration, when we said the caller will be responsible of the cleaning 289 00:19:19,300 --> 00:19:20,360 as the call. 290 00:19:20,380 --> 00:19:21,330 We have the same thing. 291 00:19:21,340 --> 00:19:22,170 Nothing's changed. 292 00:19:22,180 --> 00:19:23,140 Everything is the same. 293 00:19:23,500 --> 00:19:25,000 But the only difference is here. 294 00:19:25,000 --> 00:19:31,720 The return return will take some value, which is the size of the arguments that have been pushed onto 295 00:19:31,720 --> 00:19:32,170 the stack. 296 00:19:32,180 --> 00:19:38,620 So let's look at this again, push argument to push argument one they are pushing in reverse order called 297 00:19:38,620 --> 00:19:40,030 the poly, which is my function. 298 00:19:40,030 --> 00:19:41,560 One, push up. 299 00:19:41,560 --> 00:19:49,420 We want to store the base of the, uh, the the main function, the base of the frame of the main function, 300 00:19:49,420 --> 00:19:50,690 movable PSP. 301 00:19:50,730 --> 00:19:52,270 Now we point them on the top. 302 00:19:52,510 --> 00:19:55,240 If we have any local variables, we are going to push them. 303 00:19:55,810 --> 00:20:00,670 Once that's finished, we are going to pop X, just pop that value, which is this value. 304 00:20:00,670 --> 00:20:03,310 We are going to pop it into X pop. 305 00:20:03,340 --> 00:20:09,750 So now we restore the the the base pointer for the previous frame return. 306 00:20:09,970 --> 00:20:16,990 This will actually what, what it will do, it will take the position, put it into the EIP and then 307 00:20:16,990 --> 00:20:23,290 continue the execution from there and then pop pop to put these two values from the top of the stack 308 00:20:23,500 --> 00:20:28,900 while an entity called Push Push that's the same call, the same SBP move. 309 00:20:28,900 --> 00:20:32,680 All of this is the same topics when everything is done is going to be the same. 310 00:20:32,680 --> 00:20:35,550 Removing the local variable pop. 311 00:20:35,590 --> 00:20:42,100 We are restoring the base of the main function return here is going to take a slice of, let's say, 312 00:20:42,460 --> 00:20:43,510 these two values. 313 00:20:43,510 --> 00:20:45,190 Each one of them was four bytes. 314 00:20:45,190 --> 00:20:53,050 Then we are going to do return eight, which means remove from the stack eight bytes or just do that 315 00:20:53,050 --> 00:20:58,420 and then go to the position where we have on the stack and continue the execution from that. 316 00:21:00,070 --> 00:21:01,250 Let's look at an example. 317 00:21:01,510 --> 00:21:06,640 So let's trace this, OK, by looking at this example. 318 00:21:07,770 --> 00:21:15,080 And instead, we have not dive deeper into assembly will just giving basic high level view, so appears 319 00:21:15,090 --> 00:21:19,500 the register is always pointing to the next instruction to be executed. 320 00:21:19,800 --> 00:21:24,780 Once the CPU executes the instruction, it automatically moves EIP forward. 321 00:21:24,930 --> 00:21:26,280 OK, so again. 322 00:21:26,280 --> 00:21:26,910 Yipes. 323 00:21:26,910 --> 00:21:34,500 Always pointing to the next instruction and once that instruction got executed by the CPU, the system 324 00:21:34,500 --> 00:21:38,730 or the CPU will automatically move yippy to the next instruction. 325 00:21:38,760 --> 00:21:41,200 OK, point to the next one or move it forward. 326 00:21:41,490 --> 00:21:45,580 So now it is going to do the push argument to let's see what's going to happen. 327 00:21:45,870 --> 00:21:48,720 So argument two got pushed again. 328 00:21:48,720 --> 00:21:49,290 What happened? 329 00:21:49,290 --> 00:21:50,010 E.S.P. 330 00:21:50,010 --> 00:21:53,940 Now pointing to the top of the stack argument, one is going to be pushed. 331 00:21:53,940 --> 00:22:01,290 So E.S.P again got decreased because we're now pointing also to the top of the stack and going to the 332 00:22:01,290 --> 00:22:02,300 lower end of the memory. 333 00:22:02,940 --> 00:22:05,700 Now, call what will what the call will do. 334 00:22:05,910 --> 00:22:12,270 It will automatically push IP of the next instruction to be executed. 335 00:22:12,270 --> 00:22:13,650 Were on top of the stack. 336 00:22:13,890 --> 00:22:16,310 OK, and then do the unconditional jump. 337 00:22:16,320 --> 00:22:22,130 So first it will push the position onto the stack and then it will do unconditional jumps. 338 00:22:22,170 --> 00:22:22,790 What it will. 339 00:22:22,890 --> 00:22:23,650 What does that mean? 340 00:22:23,970 --> 00:22:30,120 It means it will go to the location where my fun one is located in memory. 341 00:22:30,450 --> 00:22:31,430 So it's going to do that. 342 00:22:31,680 --> 00:22:34,830 So that's why IP got put onto the stack. 343 00:22:35,670 --> 00:22:37,950 Now we want to save the base pointer. 344 00:22:37,950 --> 00:22:39,540 So SBP got saved. 345 00:22:40,170 --> 00:22:41,760 EVP now equals DSP. 346 00:22:41,760 --> 00:22:48,840 So now we have, we are creating a new frame and let's say we have one local variable here of type integer. 347 00:22:48,850 --> 00:22:49,800 So it's four bytes. 348 00:22:50,250 --> 00:22:52,870 We add that let's say it has a value of zero. 349 00:22:53,430 --> 00:23:01,410 Now again, one very important thing E.S.P may change inside the call body, but SBP does not change. 350 00:23:01,440 --> 00:23:08,280 Again, E.S.P might change inside the Colly body because it's going to grow up and down. 351 00:23:08,640 --> 00:23:10,410 But SBP does not change. 352 00:23:10,410 --> 00:23:12,280 Therefore, what what does that mean? 353 00:23:12,540 --> 00:23:18,980 It means the system can use SBP to refer to locations of values on the stack. 354 00:23:19,140 --> 00:23:25,230 Again, what this means since ESPs jumping up and down, depending on what you've done added to the 355 00:23:25,230 --> 00:23:33,090 stack, we probably it's not a, let's say, a good way to refer to values on the stack because it's 356 00:23:33,090 --> 00:23:35,160 keep up and down moving. 357 00:23:35,430 --> 00:23:42,750 But EBP is always the location is always fixed so we can use it to refer to variable variables and arguments 358 00:23:42,750 --> 00:23:44,550 which are already pushed onto the stack. 359 00:23:44,690 --> 00:23:47,820 OK, so we will be using SBP to refer to values. 360 00:23:48,000 --> 00:23:49,430 Let's look at an example here. 361 00:23:50,130 --> 00:23:53,040 So if we look at EBP. 362 00:23:54,210 --> 00:24:00,790 Which is pointing to the Eppy value, as you can see, EBP minus four points to the local variable. 363 00:24:00,870 --> 00:24:02,940 Now Y when Y minus four. 364 00:24:03,060 --> 00:24:04,830 And I mean by SBP minus four. 365 00:24:04,830 --> 00:24:13,020 I mean the the memory address at that location, at the memory of SBP minus four will be pointing to 366 00:24:13,020 --> 00:24:19,950 zero Y because remember the strong grows in the other way around, so it's growing to the the lower 367 00:24:19,950 --> 00:24:26,760 end, so the addresses will be decreased and that and that end. 368 00:24:27,180 --> 00:24:34,110 And as you can see here from EBP, if I want to access the first argument again, please make sure you 369 00:24:34,110 --> 00:24:40,530 remember this because we are going to ask we have a quiz at the end of of the session, Gbps pointing 370 00:24:40,530 --> 00:24:42,290 at that location, Heap's value. 371 00:24:42,600 --> 00:24:49,240 And if I go to Eppy plus eight, it means it's pointing to the first argument SBP plus twelve. 372 00:24:49,240 --> 00:24:51,570 That means is pointing to the second argument. 373 00:24:51,660 --> 00:24:52,080 Why? 374 00:24:52,410 --> 00:24:58,680 Because SBP plus four will be pointing here, SBP plus eight plus 12 here and so on and so forth. 375 00:24:59,430 --> 00:25:00,670 OK, so this is good. 376 00:25:00,690 --> 00:25:01,400 Let's continue. 377 00:25:02,450 --> 00:25:11,720 Now, at the end of the call, which is when the epilogue kicks in, that set of operations happen is 378 00:25:11,720 --> 00:25:12,380 Processo. 379 00:25:12,380 --> 00:25:15,440 Cleaning the variable space is made by a pop operation. 380 00:25:15,470 --> 00:25:19,640 So now this variable, we need to remove it from the top of the stack. 381 00:25:19,650 --> 00:25:21,010 So we have this pop. 382 00:25:21,020 --> 00:25:22,250 So we are going to do a up. 383 00:25:23,030 --> 00:25:25,310 As you can see now, E.S.P points to EBP. 384 00:25:25,340 --> 00:25:31,440 Now, if we do a pop for SBP, so we are going to restore this value, copy it into the Eppy register. 385 00:25:31,700 --> 00:25:34,740 So this means now SBP points back to the base. 386 00:25:35,030 --> 00:25:42,000 OK, so here is where the difference will happen between a C declaration and aesthetical. 387 00:25:42,030 --> 00:25:49,120 OK, here is where the difference will happen between a C declaration and as to the standard currency 388 00:25:49,130 --> 00:25:49,710 declaration. 389 00:25:50,450 --> 00:25:55,790 But before we go and talk about that, just keep in mind that the return instruction here, this return 390 00:25:55,790 --> 00:25:58,820 instruction, what it will be doing, it is going to be. 391 00:26:00,300 --> 00:26:06,390 Copying this value, which is on the top of the stack, which is actually the iPod, the next instruction 392 00:26:06,390 --> 00:26:13,470 or the position of where I want to continue is going to be copying it over into the extended instruction 393 00:26:13,470 --> 00:26:14,020 pointer. 394 00:26:14,040 --> 00:26:19,230 OK, so that now will redirect the execution were back to where it is. 395 00:26:19,860 --> 00:26:20,980 It should continue. 396 00:26:21,050 --> 00:26:25,120 OK, it's going to redirect the instruction back to where it should continue. 397 00:26:25,140 --> 00:26:32,010 So when my when Min called my function one, my function one ended, we need to go back to the position 398 00:26:32,010 --> 00:26:37,910 where I continue after I processed or after we did my function one. 399 00:26:38,070 --> 00:26:43,500 So the return instruction is going to pop that value and it will now continue. 400 00:26:43,530 --> 00:26:45,570 So we since we have here. 401 00:26:47,450 --> 00:26:53,810 Two variables are two arguments on the stack, and since we are talking about the declaration, then 402 00:26:53,810 --> 00:26:57,190 we mentioned the declaration is responsible of cleaning its mess. 403 00:26:57,200 --> 00:27:03,190 So so the top of the values which were pushed are its responsibility. 404 00:27:03,530 --> 00:27:09,900 So that's why now the caller will be cleaning the stacks of puppy puppies. 405 00:27:10,370 --> 00:27:18,920 And now we go back to where we started while if we change this just to see the difference, if we go 406 00:27:18,920 --> 00:27:26,170 back to now, which we have a second and let's say the argument size here is again, just one. 407 00:27:27,140 --> 00:27:28,070 The argument is here. 408 00:27:28,070 --> 00:27:29,390 We need to know how much is it? 409 00:27:29,390 --> 00:27:36,110 But let's assume here we have two integers or two values, which each one of them is four bytes. 410 00:27:36,140 --> 00:27:41,220 So, again, we will be doing great are the eight, which is for both of them. 411 00:27:41,600 --> 00:27:46,690 So what will happen is the return instruction will pop that value from the start. 412 00:27:46,700 --> 00:27:49,010 But now what will happen is. 413 00:27:50,800 --> 00:27:57,940 We have each one of these are 32 bits, OK, in this example, each one of them is 32 bits and we are 414 00:27:57,940 --> 00:28:00,150 going to move those, which is four bytes. 415 00:28:00,310 --> 00:28:03,020 So we are going to move the two of them. 416 00:28:03,040 --> 00:28:06,450 So we are going to move or let's say remove two of them, OK? 417 00:28:06,940 --> 00:28:13,800 And we clean now the stack and now we are back to the caller execution. 418 00:28:14,080 --> 00:28:20,500 OK, so as you can see here, the difference was between the standard call and see declaration is in 419 00:28:20,500 --> 00:28:21,430 standard call. 420 00:28:22,150 --> 00:28:26,460 The call is going to be responsible of cleaning the stack and see declaration. 421 00:28:26,480 --> 00:28:29,230 The caller will be responsible of doing that. 422 00:28:29,530 --> 00:28:31,560 OK, so let's stop here. 423 00:28:31,670 --> 00:28:35,890 Will continue next time from this one code optimization. 424 00:28:36,580 --> 00:28:41,120 We want to have gotten into the code optimization, but at least given an idea about what's happening. 425 00:28:41,410 --> 00:28:43,400 OK, so that's it for this video. 426 00:28:43,450 --> 00:28:51,610 I hope it was clear to you and you understand what we were talking about, about the static frames call 427 00:28:51,610 --> 00:28:58,990 the different call types and how all of this is happening and all of this is going to, uh, working 428 00:28:58,990 --> 00:28:59,550 in the system. 429 00:29:00,880 --> 00:29:01,870 OK, so that's it. 430 00:29:01,930 --> 00:29:02,860 See you in the next video. 431 00:29:02,890 --> 00:29:03,360 Thank you. 432 00:29:03,680 --> 00:29:04,030 Bye.