Additionally, we have the operations
like call we can call a function
name, so what call does is it
pushes the address of the next
instruction onto the stack
and then it copies IP to
that new function, location or
address.
And then the reverse of that is the
return, the return is going to
allow us to exit a function
so or return from a function.
So what that does is it pops off the
return address from the stack and
then moves VIP to that new
address.
So we end up always having a call
to a function and then inside the
function when it's done, we'll have
a return.
And so that's how we can write a
program with functions.
Now, you've done this in class.
We have called a lot of different
functions.
So for example, call, read to
call, print and call, print and tell
all of those we are calling a
function and then we assume that
it's going to resume from the
location that we were at before.
We also have the ability to allocate
space using add and subtract.
So, for example, we can say sub hesp
X and then I'll subtract X bytes
from ESP.
So typically when you enter a
function, you're going to do a
subtract on ESP to allocate
some temporary space that we can use
for local variables or buffer's.
And then at the end of the function
we can add back ESP, so
we'll typically add and subtract the
same number of bytes.
And so this will allocate that space
and we'll do that right before we
leave a function.
And then if you ever go through and
you are reverse engineering some
code, you might see that when a
function enters, it might do
something like and ESP and
then you've got a bunch of F's and a
zero.
And this is because it is trying to
make sure that E.S.P is on an even
boundary and this is a security
protection that some operating
systems and some compilers implement
to make it harder to attack
these type of systems.