Transcript for:
Understanding Assembly Language Prologue

all right so you guys probably seen the following lines of code in front of every function written in assembly uh keep in mind that I'm using AT&T syntax so the lines go as follows it goes something like this it says push this register called EBP and move this register I mean move ESP to E VP so let's go at this line by line and try to try to see what exactly it does let's use our function add as as the example so add if you didn't if you didn't watch the previous videos it's just a function that takes two arguments and adds them together and returns their sum so this is what ad does here but our main function is the one that actually calls ahe which is up here and AD is called on this line here so we know that the arguments of a function are pushed onto its stack in order from right to left so its arguments so this is ADD stack add stack and the arguments are going to to get pushed on from right to left so and we also know that this thing called the return address is also pushed on I explain what this does in my previous video but anyway so after that happens we know that since there's this register called ESP which always points to the top of the stack it's going to be pointing to the return address here which is at the top of the stack so keep in mind that in memory in my drawing here these are high memory addresses and these are low memory addresses so anyway I explained that in another video as why that's important anyway so ESP is pointing to the top of the stack always always always always so let's go at this line this code line by line let's see what it see what it's trying to tell us so first we get this com we get this line that says push EBP so pushing EBP means push EBP onto the stack onto add stack since we're just ignore this C++ code here let's say we were writing add in assembly and we know that these lines are always always seem to come up when a function is written in assembly so we say push EBP and this value of EBP is the value that EBP has before our function really does anything so so we'll kind of we'll call it we won't call it EBP but we'll call it old EBP since it's really an old value it doesn't have anything to do with our function it's just the value that it had before it even entered our function so since we push this Value Old DBP onto the stack we know that ESP is got to keep pointing to the top of the stack so esp's got moved down so ESP won't be up there anymore es will actually be down here now let me draw it in red ESP will be down here now pointing at this old EBP spot so that's what happens on this line right here this line so let's move on to the next line so in this next line what happens is this line of code is basically telling us all right make EBP EBP point to whatever ESP is pointing to that's what it's saying it's saying move the value that ESP is pointing to into EBP or in other words simpler words make EBP point to the same thing that ESP is pointing to all right so what does that do so we H we know that we got this other register called EBP so EBP is actually going to point to the same thing that ESP is pointing to so and that's it that's all this thing called the prologue this is called the prologue this here this whole thing here is called the prologue so wait a sec this thing there is going to be called the prologue and it's it's it might not seem very important now but the prologue is a very important part of code especially well functions if you're going to run a function you're going to use the stack so yeah that's basically all the prologue is doing it does that it makes ESP and EBP point to the same thing and it pushes ebp's value it's old value the one that that we really didn't do anything to onto the stack and all right I I'll hopefully touch on what exactly why exactly this is important in the next video