WEBVTT

00:01.190 --> 00:02.719
Hello, this is Dr. Miller, and this

00:02.720 --> 00:04.429
is Episode seven point three of

00:04.430 --> 00:05.430
Assembly

00:06.320 --> 00:07.549
today we're going to talk about

00:07.550 --> 00:09.069
enter and leave and then useful

00:09.410 --> 00:10.410
macro's,

00:11.360 --> 00:12.360
enter and leave.

00:13.310 --> 00:14.976
So enter and leave are done during

00:15.380 --> 00:16.819
the prolog and the epilog.

00:17.240 --> 00:18.979
And that is because doing the prolog

00:18.980 --> 00:20.209
and the epilog is a little bit

00:20.210 --> 00:21.649
tedious. You have to remember two

00:21.650 --> 00:23.149
instructions, do them in the right

00:23.150 --> 00:24.150
order.

00:24.290 --> 00:25.549
And so they enter and leave

00:25.550 --> 00:27.265
instructions, sort of fill that gap

00:27.320 --> 00:28.759
and make it a little bit easier for

00:28.760 --> 00:29.760
programmers.

00:30.770 --> 00:32.929
So, for example, enter zero zero

00:32.930 --> 00:34.939
enters a function and creates

00:34.940 --> 00:36.139
what we call a static frame.

00:37.040 --> 00:38.389
And so here we can see the

00:38.390 --> 00:39.713
definitions where the first

00:40.310 --> 00:41.878
operand is the amount of dynamic

00:42.380 --> 00:43.610
stores that you want.

00:44.150 --> 00:45.829
And then the second one talks about

00:45.830 --> 00:47.569
lexical nesting.

00:48.740 --> 00:50.719
So typically we only see either

00:50.720 --> 00:52.519
enter zero zero or enter with.

00:52.520 --> 00:54.200
The first argument is how many

00:55.100 --> 00:56.689
local variables you'd like to have.

00:57.170 --> 00:58.591
So it might be four or eight,

00:58.880 --> 01:00.409
depending on how much space you want

01:00.410 --> 01:01.410
to allocate.

01:01.790 --> 01:03.469
And so enter zero zero does the

01:03.470 --> 01:05.479
following. It does push up and

01:05.480 --> 01:07.129
then move SBP VSP.

01:09.590 --> 01:10.839
Whereas leave is what we use to

01:11.330 --> 01:12.751
function. So at this does the

01:13.040 --> 01:14.265
epilog, so leave does the

01:14.900 --> 01:17.029
following, it'll say move Hesp

01:17.030 --> 01:18.920
EBP and then Popi BP.

01:21.220 --> 01:22.929
Now, what are the pros and cons, so

01:22.930 --> 01:24.694
the pro is that it's easier to write

01:24.730 --> 01:26.740
code if you use enter and leave,

01:27.190 --> 01:29.229
you have two instructions instead of

01:29.230 --> 01:30.230
four.

01:30.580 --> 01:32.379
Now, the downside is that this is

01:32.380 --> 01:34.209
actually slower than doing it

01:34.210 --> 01:35.409
by hand. If you know what you're

01:35.410 --> 01:36.849
doing and you know what

01:37.240 --> 01:39.400
instructions are faster than

01:39.700 --> 01:40.959
what other instructions.

01:41.620 --> 01:42.969
So let's look at an example.

01:43.690 --> 01:45.405
So previously we had done a example

01:45.790 --> 01:46.819
called FUX and inside

01:47.680 --> 01:49.800
of it we would use

01:49.810 --> 01:51.459
push and then popped.

01:51.460 --> 01:52.881
And this is an aversion using

01:53.560 --> 01:54.560
C.

01:54.890 --> 01:56.680
And so what we can see is that

01:56.920 --> 01:59.079
in C it'll do

01:59.080 --> 02:00.639
instead of doing enter, it'll do

02:00.640 --> 02:03.040
push EBP, Move SBP, PSP,

02:03.550 --> 02:05.019
but then at the end it does the

02:05.020 --> 02:06.020
leave instruction.

02:06.370 --> 02:07.840
And this is because using this

02:08.080 --> 02:10.059
instruction is faster than enter

02:10.330 --> 02:12.249
and using this instruction is faster

02:12.250 --> 02:14.389
than doing it by hand.

02:15.130 --> 02:16.479
And so what you'll see is you'll see

02:16.480 --> 02:18.009
a compiler will make a better

02:18.010 --> 02:19.480
optimization than what maybe a

02:19.600 --> 02:21.639
programmer might do by hand.

02:22.240 --> 02:23.979
And so this is one of the examples

02:23.980 --> 02:25.499
that we'll see where people who

02:25.540 --> 02:27.059
write compilers are writing for

02:27.400 --> 02:28.779
the code that will go as fast as

02:28.780 --> 02:30.054
possible and use as little

02:30.610 --> 02:31.569
space as possible.

02:31.570 --> 02:33.190
And so they'll make those decisions.

02:35.530 --> 02:37.147
So we do have some macros that we

02:37.330 --> 02:38.330
can use, so no one

02:39.160 --> 02:40.919
is we can talk about the stack size.

02:40.960 --> 02:42.129
So if you go to the Nazeem

02:42.130 --> 02:43.599
documents, you can see the different

02:43.600 --> 02:44.678
types of stack size so

02:45.460 --> 02:47.770
you can use small, flat,

02:48.400 --> 02:50.559
large. So there are different sizes

02:50.560 --> 02:51.560
that we can use.

02:53.440 --> 02:55.139
And then we've talked about Mack,

02:55.600 --> 02:56.889
we've talked about arguments to

02:56.890 --> 02:58.654
functions previously, and so instead

02:58.960 --> 03:00.639
of defining those by an offset,

03:01.060 --> 03:03.249
you can use basically a shortcut.

03:03.250 --> 03:05.409
So every time I see X, it will do,

03:05.770 --> 03:08.199
for example, EBP minus

03:08.200 --> 03:09.200
or plus eight.

03:09.430 --> 03:11.589
And every time I see why it might do

03:11.590 --> 03:13.770
plus 12 and Z plus 16.

03:14.380 --> 03:15.489
And so this will do all those

03:15.490 --> 03:16.810
calculations for you.

03:18.430 --> 03:20.019
On the flip side, you can also use

03:20.020 --> 03:21.669
macros for local variables,

03:22.090 --> 03:23.079
although it's a little bit more

03:23.080 --> 03:24.080
complicated.

03:24.190 --> 03:25.954
So you can see here that it's got to

03:26.050 --> 03:27.716
push some context and perhaps some

03:28.000 --> 03:29.379
context at the beginning, in the

03:29.380 --> 03:30.249
end.

03:30.250 --> 03:31.779
And this is so that the

03:32.140 --> 03:33.699
assembler knows what you're doing.

03:34.270 --> 03:35.889
And then here it's creating a

03:35.890 --> 03:37.959
variable inside of the assembler

03:37.960 --> 03:39.039
that we're never going to see.

03:39.400 --> 03:40.749
But this variable says how many

03:40.750 --> 03:42.280
local variables do we have?

03:42.910 --> 03:44.320
And then when we define each one of

03:44.350 --> 03:45.869
these local variables, it'll add,

03:45.880 --> 03:47.469
so, for example, this will add four

03:47.470 --> 03:49.029
bytes and this will add four bytes.

03:50.410 --> 03:51.699
So when it gets down to the end,

03:51.700 --> 03:53.415
it'll actually just say enter eight

03:53.590 --> 03:55.809
comma zero if you disassemble

03:55.810 --> 03:56.679
it.

03:56.680 --> 03:58.297
And so then you can just use this

03:58.870 --> 04:00.429
instead of having to keep track.

04:00.430 --> 04:02.649
Of which offset did I use for

04:03.490 --> 04:05.589
X and which one did I use for Y

04:05.590 --> 04:06.939
or some or I.

04:07.120 --> 04:08.199
And in the future we're going to

04:08.200 --> 04:09.699
look at an example showing that.

04:14.330 --> 04:15.996
So today we talked about enter and

04:16.040 --> 04:17.449
leave, and then we looked at some

04:17.450 --> 04:19.099
macros that you can use in order to

04:19.100 --> 04:20.389
make your life easier.

04:21.649 --> 04:22.849
So looking forward, we'll give you

04:22.850 --> 04:24.565
an example of using enter and leave

04:24.740 --> 04:26.329
and arguments and convert it from

04:26.330 --> 04:27.529
our previous example.

04:28.280 --> 04:29.509
And then also we're going to talk

04:29.510 --> 04:30.829
about floating point numbers.

04:32.480 --> 04:34.339
So here's our quiz, what is the

04:34.340 --> 04:36.620
equivalent of enter zero zero

04:36.650 --> 04:38.540
as far as low level instructions?

04:40.590 --> 04:42.550
And that's pushing EBP Move SBP

04:42.560 --> 04:43.560
E.S.P.

04:44.470 --> 04:45.940
What's the equivalent of leave?

04:48.460 --> 04:50.380
That is Move E.S.P, EBP,

04:50.710 --> 04:51.710
Pop, EBP.

04:53.710 --> 04:54.669
If you have questions, you can

04:54.670 --> 04:56.799
contact me via email

04:56.800 --> 04:58.417
at Miller, M.G., at U.A.E to you,

04:58.720 --> 05:00.099
and you can find me on Twitter at

05:00.100 --> 05:00.910
Milhouse 30.

