Hello, this is Dr. Miller, and this
is Episode six point six of Assembly
today we're going to talk about the
function epilog and then we'll give
our full example and debug it
so the function epilog.
So this is what we do in order to
restore the stack and the 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 E.S.P back to
EBP because Hesp can be
modified in the middle of our
function.
I remember SBP is our extended base
pointer, so it points to the base
and so it points to the old version
of SBP.
And so then we do a pop SBP
and so this is going to restore the
old version of SBP 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
stack 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
and Cafe Cafe onto the stack.
And so those are on there.
We have our old our return address
of where we're going to go back.
And that's going to be this function
right here.
And we have our old version of EBP.
And so now we're going to we've
executed the body of our function,
which this function just moved 096.
That's our return result.
And then we're going to copy
EBP into E.S.P.
And so we should see no change
there.
But now when we pop SBP, so
hesp points to the top of the stack.
Now SBP gets restored back to
its old version
and then Pop will logically
remove that from there.
And so now our return address should
be on top of the stack.
So when we do return,
it's going to copy that into Yippy
and then jump to that location.
And so if you recall, we called
this function here.
So the return address 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 ADP
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.
Then 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 yak's 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 GDB and
we're going to use Jeff on it,
so that's been installed by default.
So I'll go ahead and break in
as an
underscore making.
I'll start running.
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,
EBP pointing to the same things that
we have an stack.
And so we're going to execute a few
instructions to do things like
push all of the registers and then
we'll see our our pushes here.
So Essi 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 going
to 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 this
zero six eight zero
four eight four SKW.
That's an hour function.
But let's scroll back here.
So this is our function that is
going to call the function,
so the return address is 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, eight zero four eight
four 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 I'll step.
So now we can see that eight zero
four eight four 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 Prolog, which is
discussed in the previous lecture.
So we're going to push the old value
of EBP.
So currently EBP has
the value of looks like
four FS 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 FCD three
D eight is now on top of the stack.
And as you can see right now, E.S.P
is pointing to the top of the stack.
We will go ahead and
set the base pointer to
be where Hesp is.
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 and my
function, which is just to move
yak's to zero.
Go ahead and step that.
Now we're going to we're at the end
of our function. We're ready to
exit. So we copy back SBP
to Hesp. In this case, they are the
same thing.
But this is how the Prolog is
supposed to work.
All right, so now they're copied to
the stack pointer as this is
pointing to the old version of SBP.
So now this is going to get restored
in TBP.
We can see here that SBP
has a different value.
It's a D three eight.
Instead of date,
so now that you get restored back
to the date
in the stack, we'll take that off.
So now we can see that BP
has d.h in it where it was
before.
And then E.S.P is pointing to this
return address, which is inside of
our main, and so now
when we step, we're going to go back
into our main.
So now we've gone back into Maine
and we're at that instruction that
we saw before at 8:00,
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 the where our stack
was before we started executing,
right, so it is
restored back to what it was
previously.
Then when you're done, you can hit C
for continue and it'll run an
exit.
So that was our full example,
so today we talked about the epilog
and then we gave that full example
and showed how to the budget, which
we've done in the past.
Looking forward, we're going to
start talking about function
arguments and those are going to
leverage SBP, which we talked
about in this lecture and
in the previous lecture, and
so understanding how
SBP is set up in the Prolog
and the epilog is important to
understanding arguments.
So the quiz, what is the epilog
consist of?
It consists of ESPE,
EBP, Pop, SBP
and then return.
If you have questions, you can
contact me at Miller, at you in
Canada, Edu, and you can find me on
Twitter at Milhouse 30.