Hello, this is Dr. Miller, and this
is Episode seven point three of
Assembly
today we're going to talk about
enter and leave and then useful
macro's,
enter and leave.
So enter and leave are done during
the prolog and the epilog.
And that is because doing the prolog
and the epilog is a little bit
tedious. You have to remember two
instructions, do them in the right
order.
And so they enter and leave
instructions, sort of fill that gap
and make it a little bit easier for
programmers.
So, for example, enter zero zero
enters a function and creates
what we call a static frame.
And so here we can see the
definitions where the first
operand is the amount of dynamic
stores that you want.
And then the second one talks about
lexical nesting.
So typically we only see either
enter zero zero or enter with.
The first argument is how many
local variables you'd like to have.
So it might be four or eight,
depending on how much space you want
to allocate.
And so enter zero zero does the
following. It does push up and
then move SBP VSP.
Whereas leave is what we use to
function. So at this does the
epilog, so leave does the
following, it'll say move Hesp
EBP and then Popi BP.
Now, what are the pros and cons, so
the pro is that it's easier to write
code if you use enter and leave,
you have two instructions instead of
four.
Now, the downside is that this is
actually slower than doing it
by hand. If you know what you're
doing and you know what
instructions are faster than
what other instructions.
So let's look at an example.
So previously we had done a example
called FUX and inside
of it we would use
push and then popped.
And this is an aversion using
C.
And so what we can see is that
in C it'll do
instead of doing enter, it'll do
push EBP, Move SBP, PSP,
but then at the end it does the
leave instruction.
And this is because using this
instruction is faster than enter
and using this instruction is faster
than doing it by hand.
And so what you'll see is you'll see
a compiler will make a better
optimization than what maybe a
programmer might do by hand.
And so this is one of the examples
that we'll see where people who
write compilers are writing for
the code that will go as fast as
possible and use as little
space as possible.
And so they'll make those decisions.
So we do have some macros that we
can use, so no one
is we can talk about the stack size.
So if you go to the Nazeem
documents, you can see the different
types of stack size so
you can use small, flat,
large. So there are different sizes
that we can use.
And then we've talked about Mack,
we've talked about arguments to
functions previously, and so instead
of defining those by an offset,
you can use basically a shortcut.
So every time I see X, it will do,
for example, EBP minus
or plus eight.
And every time I see why it might do
plus 12 and Z plus 16.
And so this will do all those
calculations for you.
On the flip side, you can also use
macros for local variables,
although it's a little bit more
complicated.
So you can see here that it's got to
push some context and perhaps some
context at the beginning, in the
end.
And this is so that the
assembler knows what you're doing.
And then here it's creating a
variable inside of the assembler
that we're never going to see.
But this variable says how many
local variables do we have?
And then when we define each one of
these local variables, it'll add,
so, for example, this will add four
bytes and this will add four bytes.
So when it gets down to the end,
it'll actually just say enter eight
comma zero if you disassemble
it.
And so then you can just use this
instead of having to keep track.
Of which offset did I use for
X and which one did I use for Y
or some or I.
And in the future we're going to
look at an example showing that.
So today we talked about enter and
leave, and then we looked at some
macros that you can use in order to
make your life easier.
So looking forward, we'll give you
an example of using enter and leave
and arguments and convert it from
our previous example.
And then also we're going to talk
about floating point numbers.
So here's our quiz, what is the
equivalent of enter zero zero
as far as low level instructions?
And that's pushing EBP Move SBP
E.S.P.
What's the equivalent of leave?
That is Move E.S.P, EBP,
Pop, EBP.
If you have questions, you can
contact me via email
at Miller, M.G., at U.A.E to you,
and you can find me on Twitter at
Milhouse 30.