WEBVTT

00:01.980 --> 00:03.449
Hello, this is Dr. Miller, and this

00:03.450 --> 00:05.459
is Episode six point six of Assembly

00:06.750 --> 00:08.188
today we're going to talk about the

00:08.189 --> 00:09.839
function epilog and then we'll give

00:09.840 --> 00:11.340
our full example and debug it

00:12.330 --> 00:13.429
so the function epilog.

00:14.280 --> 00:15.779
So this is what we do in order to

00:15.780 --> 00:17.495
restore the stack and the registers

00:18.030 --> 00:19.770
to the old versions that they had.

00:19.800 --> 00:21.417
So this is what you do every time

00:21.420 --> 00:22.319
that you're going to exit a

00:22.320 --> 00:23.320
function.

00:23.820 --> 00:25.620
So in this example, we're going to

00:25.650 --> 00:26.650
copy E.S.P back to

00:27.480 --> 00:29.549
EBP because Hesp can be

00:29.550 --> 00:30.869
modified in the middle of our

00:30.870 --> 00:31.870
function.

00:31.950 --> 00:33.665
I remember SBP is our extended base

00:33.840 --> 00:35.549
pointer, so it points to the base

00:36.360 --> 00:38.075
and so it points to the old version

00:38.190 --> 00:39.450
of SBP.

00:39.660 --> 00:41.640
And so then we do a pop SBP

00:42.390 --> 00:43.889
and so this is going to restore the

00:43.890 --> 00:46.079
old version of SBP from a previous

00:46.080 --> 00:48.049
function that we were using.

00:48.630 --> 00:50.429
And so now it is restored back to

00:50.460 --> 00:51.460
where it was.

00:51.870 --> 00:53.369
And if you remember, the top of our

00:53.370 --> 00:55.134
stack should have our return address

00:55.560 --> 00:56.560
on it.

00:58.650 --> 01:00.539
So here's our example as we left

01:00.540 --> 01:02.429
last time, all right, so we

01:02.430 --> 01:03.600
had pushed on these

01:04.290 --> 01:06.119
hard coded values beef, beef

01:06.120 --> 01:08.420
and Cafe Cafe onto the stack.

01:08.430 --> 01:09.569
And so those are on there.

01:10.020 --> 01:11.686
We have our old our return address

01:12.150 --> 01:13.259
of where we're going to go back.

01:13.260 --> 01:14.519
And that's going to be this function

01:14.520 --> 01:15.509
right here.

01:15.510 --> 01:17.510
And we have our old version of EBP.

01:18.690 --> 01:20.209
And so now we're going to we've

01:20.730 --> 01:22.129
executed the body of our function,

01:22.140 --> 01:24.849
which this function just moved 096.

01:25.440 --> 01:27.209
That's our return result.

01:27.600 --> 01:29.579
And then we're going to copy

01:29.580 --> 01:31.219
EBP into E.S.P.

01:31.500 --> 01:32.970
And so we should see no change

01:33.300 --> 01:34.300
there.

01:34.350 --> 01:36.209
But now when we pop SBP, so

01:36.240 --> 01:38.099
hesp points to the top of the stack.

01:38.100 --> 01:40.019
Now SBP gets restored back to

01:40.020 --> 01:41.020
its old version

01:42.840 --> 01:44.939
and then Pop will logically

01:44.940 --> 01:45.940
remove that from there.

01:46.440 --> 01:48.269
And so now our return address should

01:48.270 --> 01:49.400
be on top of the stack.

01:49.650 --> 01:51.149
So when we do return,

01:51.510 --> 01:53.339
it's going to copy that into Yippy

01:53.340 --> 01:54.930
and then jump to that location.

01:55.830 --> 01:57.349
And so if you recall, we called

01:58.050 --> 01:58.829
this function here.

01:58.830 --> 02:00.269
So the return address should be the

02:00.300 --> 02:02.309
the address of this line of code

02:02.310 --> 02:03.310
right here.

02:04.670 --> 02:06.091
So now we're at that line and

02:06.590 --> 02:08.256
then when we do something like ADP

02:08.660 --> 02:10.099
eight, right, the two things that

02:10.100 --> 02:11.479
were on top of the stack that we had

02:11.480 --> 02:12.480
pushed on before are

02:13.400 --> 02:14.989
now logically removed.

02:15.000 --> 02:16.000
So we execute that.

02:16.240 --> 02:18.229
Then the top of our stack is how

02:18.230 --> 02:19.602
it was before we started our

02:19.910 --> 02:20.910
function.

02:21.020 --> 02:22.249
So our function didn't change

02:22.250 --> 02:23.720
anything except as we noted in

02:24.080 --> 02:25.501
our body, it changed yak's to

02:25.940 --> 02:26.990
the value of zero.

02:29.230 --> 02:30.639
So here we're going to debug an

02:30.640 --> 02:32.319
example, and this is the example

02:32.320 --> 02:34.270
that is given right here

02:34.990 --> 02:36.750
so we can see our example.

02:36.790 --> 02:38.407
So we'll go ahead and jump in and

02:38.710 --> 02:39.909
look at what that looks like.

02:44.540 --> 02:45.860
So if we look at the code.

02:49.580 --> 02:51.049
We can see that we have our pushes,

02:51.050 --> 02:52.618
we call our function, we correct

02:53.030 --> 02:54.549
our stack, and then this is the

02:54.560 --> 02:55.939
internals of our function.

03:00.590 --> 03:02.207
So we'll go ahead and use GDB and

03:02.360 --> 03:03.650
we're going to use Jeff on it,

03:04.430 --> 03:06.469
so that's been installed by default.

03:09.850 --> 03:11.439
So I'll go ahead and break in

03:11.830 --> 03:12.939
as an

03:14.020 --> 03:15.279
underscore making.

03:17.100 --> 03:18.120
I'll start running.

03:19.970 --> 03:21.649
And so we'll see here on top of the

03:21.650 --> 03:22.909
stack, it really doesn't matter what

03:22.910 --> 03:23.910
this points to.

03:24.170 --> 03:26.009
You can tell that it says ESPN,

03:26.010 --> 03:27.889
EBP pointing to the same things that

03:27.890 --> 03:29.350
we have an stack.

03:30.380 --> 03:31.699
And so we're going to execute a few

03:31.700 --> 03:33.170
instructions to do things like

03:33.740 --> 03:35.299
push all of the registers and then

03:35.300 --> 03:36.769
we'll see our our pushes here.

03:36.770 --> 03:38.629
So Essi is step instruction.

03:39.530 --> 03:41.569
If I hit enter, it will do the next

03:41.570 --> 03:42.570
instruction.

03:43.400 --> 03:45.169
So here we can see we have our push

03:45.170 --> 03:46.039
beef beef.

03:46.040 --> 03:47.689
So now that should go on top of the

03:47.690 --> 03:48.690
stack.

03:50.510 --> 03:52.225
So now we can see that beef beef is

03:52.250 --> 03:54.014
on top of our stack, now we're going

03:54.020 --> 03:55.339
to Push Cafe Cafe.

03:57.060 --> 03:58.699
So now we can see that our stack

03:58.710 --> 04:00.278
should have Cafe Cafe at the top

04:00.930 --> 04:02.810
and then beef beef is

04:02.820 --> 04:04.050
at plus four

04:06.390 --> 04:07.619
and then it's showing that when

04:07.620 --> 04:08.849
we're going to call this function,

04:08.850 --> 04:10.614
so we're going to call our function.

04:10.860 --> 04:11.987
It has an address here.

04:12.030 --> 04:13.889
So it's got this

04:13.890 --> 04:14.890
zero six eight zero

04:16.050 --> 04:18.360
four eight four SKW.

04:18.570 --> 04:19.829
That's an hour function.

04:20.670 --> 04:22.170
But let's scroll back here.

04:23.550 --> 04:25.259
So this is our function that is

04:25.260 --> 04:26.579
going to call the function,

04:27.210 --> 04:28.827
so the return address is the next

04:28.920 --> 04:30.599
instruction that should be executed

04:30.600 --> 04:31.879
after the call instruction.

04:32.370 --> 04:34.439
So it's going to be this function

04:34.440 --> 04:35.440
right here.

04:36.330 --> 04:37.330
So this address

04:38.160 --> 04:39.989
here, eight zero four eight

04:40.770 --> 04:41.770
four C four should

04:42.660 --> 04:44.309
be the address that gets pushed onto

04:44.310 --> 04:45.569
the stack, because that's the next

04:45.570 --> 04:46.919
instruction that we want to go and

04:46.920 --> 04:47.920
execute.

04:49.940 --> 04:50.940
So I'll step.

04:53.790 --> 04:55.407
So now we can see that eight zero

04:55.680 --> 04:57.839
four eight four C four

04:57.840 --> 04:59.000
is pushed onto the stack.

04:59.010 --> 05:00.627
So that's our return address that

05:00.660 --> 05:02.220
we're going to execute.

05:02.700 --> 05:04.023
We can still see Cafe Cafe.

05:04.350 --> 05:05.990
We can still see beef beef.

05:06.750 --> 05:08.699
Now we enter the Prolog, which is

05:08.700 --> 05:10.069
discussed in the previous lecture.

05:10.080 --> 05:11.549
So we're going to push the old value

05:11.550 --> 05:12.550
of EBP.

05:12.990 --> 05:15.029
So currently EBP has

05:15.060 --> 05:16.889
the value of looks like

05:16.890 --> 05:18.989
four FS D three D

05:18.990 --> 05:20.939
eight. That is our current value of

05:20.940 --> 05:22.439
that. So that's going to get pushed

05:22.440 --> 05:23.440
onto the stack.

05:24.840 --> 05:26.690
So you can see the four FCD three

05:26.700 --> 05:28.529
D eight is now on top of the stack.

05:29.940 --> 05:31.589
And as you can see right now, E.S.P

05:31.590 --> 05:33.149
is pointing to the top of the stack.

05:33.750 --> 05:34.800
We will go ahead and

05:36.000 --> 05:38.310
set the base pointer to

05:39.000 --> 05:40.449
be where Hesp is.

05:41.220 --> 05:42.739
So now we can see that the base

05:43.170 --> 05:44.970
pointer is correctly set to

05:45.000 --> 05:46.709
here and we'll use that in future

05:46.710 --> 05:47.820
lectures when we talk about

05:48.270 --> 05:49.709
parameters and when we talk about

05:49.710 --> 05:50.710
arguments.

05:52.200 --> 05:53.279
So now I'm in the body and my

05:53.280 --> 05:54.599
function, which is just to move

05:54.600 --> 05:55.860
yak's to zero.

05:55.950 --> 05:57.149
Go ahead and step that.

05:58.080 --> 05:59.459
Now we're going to we're at the end

05:59.460 --> 06:00.569
of our function. We're ready to

06:00.570 --> 06:02.809
exit. So we copy back SBP

06:02.820 --> 06:04.529
to Hesp. In this case, they are the

06:04.530 --> 06:05.339
same thing.

06:05.340 --> 06:06.761
But this is how the Prolog is

06:06.990 --> 06:07.990
supposed to work.

06:10.100 --> 06:11.509
All right, so now they're copied to

06:11.780 --> 06:13.519
the stack pointer as this is

06:13.520 --> 06:15.579
pointing to the old version of SBP.

06:15.620 --> 06:17.119
So now this is going to get restored

06:17.120 --> 06:18.120
in TBP.

06:18.830 --> 06:20.450
We can see here that SBP

06:21.020 --> 06:21.979
has a different value.

06:21.980 --> 06:23.419
It's a D three eight.

06:24.920 --> 06:25.990
Instead of date,

06:26.780 --> 06:28.397
so now that you get restored back

06:28.670 --> 06:30.170
to the date

06:31.640 --> 06:33.259
in the stack, we'll take that off.

06:34.830 --> 06:36.629
So now we can see that BP

06:37.110 --> 06:39.149
has d.h in it where it was

06:39.150 --> 06:40.150
before.

06:40.880 --> 06:42.546
And then E.S.P is pointing to this

06:42.800 --> 06:44.629
return address, which is inside of

06:44.630 --> 06:45.630
our main, and so now

06:46.550 --> 06:48.079
when we step, we're going to go back

06:48.080 --> 06:49.790
into our main.

06:53.960 --> 06:55.577
So now we've gone back into Maine

06:55.760 --> 06:56.929
and we're at that instruction that

06:56.930 --> 06:58.869
we saw before at 8:00,

06:59.210 --> 07:00.799
and so that's going to remove our

07:00.800 --> 07:01.927
cafe cafe and beef beef

07:02.720 --> 07:04.059
that are on top of the stack.

07:04.820 --> 07:06.589
So those will get logically removed.

07:08.710 --> 07:10.119
And so this is the where our stack

07:10.120 --> 07:12.220
was before we started executing,

07:12.350 --> 07:14.079
right, so it is

07:14.710 --> 07:16.629
restored back to what it was

07:16.630 --> 07:17.630
previously.

07:20.810 --> 07:22.239
Then when you're done, you can hit C

07:22.240 --> 07:23.661
for continue and it'll run an

07:24.070 --> 07:25.070
exit.

07:28.910 --> 07:30.439
So that was our full example,

07:31.040 --> 07:32.989
so today we talked about the epilog

07:32.990 --> 07:34.670
and then we gave that full example

07:35.030 --> 07:36.589
and showed how to the budget, which

07:36.590 --> 07:37.610
we've done in the past.

07:38.780 --> 07:39.709
Looking forward, we're going to

07:39.710 --> 07:40.729
start talking about function

07:40.730 --> 07:41.959
arguments and those are going to

07:41.960 --> 07:43.381
leverage SBP, which we talked

07:43.880 --> 07:45.105
about in this lecture and

07:45.920 --> 07:47.839
in the previous lecture, and

07:47.840 --> 07:48.840
so understanding how

07:49.790 --> 07:52.009
SBP is set up in the Prolog

07:52.010 --> 07:53.480
and the epilog is important to

07:53.570 --> 07:55.160
understanding arguments.

07:57.010 --> 07:58.869
So the quiz, what is the epilog

07:58.870 --> 07:59.870
consist of?

08:02.350 --> 08:03.350
It consists of ESPE,

08:04.290 --> 08:06.250
EBP, Pop, SBP

08:06.280 --> 08:07.280
and then return.

08:09.110 --> 08:10.309
If you have questions, you can

08:10.550 --> 08:12.069
contact me at Miller, at you in

08:12.470 --> 08:14.185
Canada, Edu, and you can find me on

08:14.300 --> 08:15.709
Twitter at Milhouse 30.

