Hello. This is Dr Miller, and this is Episode 6.6 of Assembly.
Today we're gonna talk about the function epilogue, and then we'll give our full example into buggit.
So the function epilogue.
So this is what we do in order to restore the stack and registers to the old versions that they had. So this is what you do every time that you're going to exit a function.
So in this example, we're going to copy, Yes, p back to e BP because SP can be modified in the middle of our function.
Remember, EVP is our extended base pointer, so it points to the base.
And so it points to the old version of EVP. And so then we do a pop e v p.
And so this is going to restore the old version of E V P from a previous function that we were using.
And so now it is restored back to where it was.
And if you remember, the top of our stacks should have our return address on it.
So here's our example as we left last time.
All right, So we had pushed on these
hard coded values Beef, beef in Cafe Cafe onto the stack. And so those air on there we have our old are return address of where we're going to go back, and that's gonna be this function right here.
Then we have our old version of E B. P.
And so now we're going to
We've executed the body of our function, which this function just move zero in t a X
that's our were returned result. And then we're going to copy E v P in tsp, and so we should see no change there.
But now when we pop pbp So yes, pea points of the top of the stack Now e v p gets restored back to its old version
and then pop will logically remove that from there.
So now our return address should be on top of the stack. So when we do return, it's gonna copy that in T i p and then jump to that location.
And so, if you recall, we called dysfunction here. So the return of just should be the the address of this line of code right here.
So now we're at that line, and then when we do something like add yes, B eight, right? The two things that were on top of the stack that we had pushed on before are now logically removed. So we execute that on the top of our stack is how it was before we started our function.
So our function didn't change anything. Except as we noted in our body, it changed the X to the value of zero.
So here we're going to debug an example. And this is the example that is given right here
so we can see our example. So we'll go ahead and jump in and look at what that looks like.
So if we look at the code,
we can see that we have our pushes. We call our function, we correct our stack, and then this is the internals of our function.
So we'll go ahead and use DDB and we're gonna use Jeff on it.
So that's been installed by default.
So I go ahead and break in. Essam
and so we'll see here on top of the stack. It really doesn't matter what this points to. You can tell that it says ESPN Eby Pierre pointing to the same thing. So we haven't CBP Base Tech.
And so we're gonna execute a few instructions to do things like push all of the registers and then we'll see our pushes here. So s I is step instruction.
If I hit enter, it will do the next instruction.
So here we can see we have our push. Beef, beef. So now that should go on top of the stack.
So now we can see that beef beef is on top of our stack. Now we're gonna push Cafe Cafe.
So now we can see that our stack should have Cafe cafe at the top, and then
beef beef is at plus four
and then it's showing that when we're going to call this function, So we're going to call our function. It has an address here. So it's got
80484 cf. That's an R function,
but let's scroll back here.
So this is our function that is going to call the function.
So the return addresses the next instruction that should be executed after the call instruction.
So it's going to be this function right here.
So this address here 80 for eight
for C four should be the address that gets pushed onto the stack because that's the next instruction that we want to go and execute.
So now we can see that 80484 C four is pushed onto the stack. So that's our return address that we're going to execute.
We can still see Cafe Cafe. We can still see beef beef.
Now we enter the prologue, which was discussed in the previous lecture. So we're gonna push the old value of E B. P.
So currently, e BP has thieve value of looks like four efs d three d eight. That is our current value of that. So that's going to get pushed onto the stack
so you can see the four efs d three. D eight is now on top of the stack.
And as you can see right now, yes, P is pointing to the top of the stack.
set the base pointer to
So now we can see that the base pointer is correctly set to here, and we'll use that in future lectures. When we talk about
parameters and when we talk about arguments.
So now I'm in the body of my function, which is just to move, yea x 20 Go ahead and step that
now we're going to where? At the end of our function, we're ready to exit. So we copy Backe, BP, TSP. In this case, they are the same thing. But this is how the prologue is supposed to work.
All right, so now they're copied. So the the stack pointer is this is pointing. Told the old version, TV BP. So now this is going to get restored in TVP.
All right, we can see here that e v P
has a different value. It's a d 38
So now that should get restored back to the D eight
and the stack will take that off.
So now we can see that BP has d eight in it where it was before,
and then Yes, P is pointing to this return address, which is inside of our main.
And so now when we step, we're gonna go back into our main.
So now we've gone back into Maine, and we're at that instruction that we saw before. Add yes, p eight And so that's going to remove our cafe, cafe and beef beef that are on top of the stack
so those will get logically removed.
And so this is where our stack was before we started executing, right? So
it is respond restored back to what it was previously.
Then when you're done, you can hit, see for continue noodle running exit.
So that was our full example.
So today we talked about the epilogue and then we gave that full example and showed how to debug it, which we've done in the past.
Looking forward, we're going to start talking about function arguments and those air going to leverage CBP, which we talked about in this lecture and in the previous lecture
and so understanding how e. V p is set up in the prologue and epilogue is important to understanding arguments.
So the quiz, What is the epilogue consist of?
It consists of movie s P E, V, p. Poppy, BP, and then return.
If you have questions, you can contact me at Miller MJ at you and Keita et you and confined me on Twitter at Milhouse 30