Hello, this is Dr. Miller, and this
is Episode six point three of
assembly
today we're going to start using the
stack and then we're going to have
an example of calling the function
printf.
Using the stick.
So when we have a stack, one of the
things that we're going to want to
do is we're going to want to either
push things on or pop
things off.
And so every time that you push
something on, it modifies
ESP and
when you're done, you need to pop it
back off.
So an example would be push yak's,
push SBX and then we can pop them
in the reverse order.
So that's one way to restore
the stack back to its original
state, which is what we need to do
all the time.
The other thing that we could do is
we could push on two registers or
register twice and then we can just
add E.S.P to correct
the amount that we put on there.
If we don't do this, then our
program is going to crash and we'll
show an example of that in a little
while.
So, for example, you might want to
save some data when you enter
a function and we'll talk about how
to create functions in the next
lecture.
So when you enter your function, you
might want to push registers.
So, for example, you exceed
S.I and then when
you're done, you need to pop them
off in the reverse order.
So the last one that you pushed is
the first one that you pop.
So you can see we did
X, X and
then X.
And so when we create functions,
we're going to want to enter.
We're going to save our our
registers. And when we exit, we're
going to restore them back in
the reverse order that we had.
So printf so
printf is a C function that has
a variable amount of arguments,
which means that you can have zero
one, two, three, four, 10, 20,
whatever the number of arguments
that you want is.
And we have this thing called a
format string.
So format string has these
characters and then that gets
replaced with the actual variables
X, Y and C.
So we're going to do is we're going
to call a print function
and we've got to push our arguments
in the reverse order because
the first thing on is the last
thing that we have in there.
So we'll go ahead and start our
project here.
So I've already built the project.
So the first thing that we're going
to do is we're going to declare a
couple variables and then we're
going to define our format string.
So I'm going to try and do the same
format string that we had before.
I'm just typing that in.
The present day means that it's
going to be a integer.
The sea is a character,
and then the 10 is our new line and
the zero terminates our stream, and
then we'll declare a couple
variables. So X
has 10 Y.
As 20.
Then I'll show some examples here,
so we have to push on our arguments
in the reverse order, so I'm going
to push my character first.
So I can define a character, so that
is the character a right, so that
would put it onto the stack.
Now I could use
a register to put that in, but
we'll just show pushing an immediate
or just pushing a hard coded value
onto the stack
here. We want to get the value of
what? Why?
And I'm just using X because it's
convenient.
And we'll do X
here.
All right, so I've put them
on in the reverse order, so I got my
character is the first thing that I
put on because it's the last thing
in here.
Then I got Y and then I got
X, and now I need to put on my
format string
and for the format, we
want the address of it.
We don't want the actual data at it.
So we're getting the address and
then we'll push that on
to our stack.
So now we're ready to call our
function.
So the function is called printf,
but it doesn't exist in our normal
spot, it exists in
the C library, so we have to tell it
that we have an
external function called print
F,
and then we can see here that we
pushed on one to three,
four items onto the stack.
And so when we're done, we have to
correct a stack.
So the easiest way to do that is to
add to E.S.P
the number of 16
or you could do 10 in hex.
Either one is fine.
So 16, base 10.
Or we can do list 10
X.
So we built our project,
so now we can go ahead and run it.
So print it off the data that we
thought it would print off.
So let's go back in.
And let's look at if we forgot to
correct the stats, I'll just comment
on this line.
So now when I build my project.
It causes it to crash
because there are pointers
that are saved on the stack that if
we don't restore the stock back to
its original, then it's going to
have problems.
And even if I if I did this and I
let's say only corrected it by
12 instead of 16
because I forgot about one of my
arguments that I put on there.
I'll clean my project and
run it.
It also caused it to crash,
so one of the keys to understanding
here is that you want to make sure
that you put the correct
amount back on solidary 16
and decimal.
Now, when we run our function,
it works fine and it doesn't crash
in a segmentation fault.
This means that the program tried to
access a region of memory that it
wasn't allowed to access.
All right, so today we looked at
using the stack by calling
our function that was called printf.
So here's a couple of quick
questions when exiting a function,
what operation to perform
on E.S.P.
So generally, we add E.S.P
and then some amount, and that's the
amount that we allocated when we
were entering the function or
before we were calling a function.
And then how do we save registers?
So we push the register, so
we looked at earlier, you push them
and then you would pop them, so
push X or push Ebix, whatever
the register that you want to save
is.
Looking forward, we're going to look
at calling functions and then even
creating what we call recursive
functions.
If you have questions, you can
contact me at Milang, at you,
Ankita, Edu, and you can find me on
Twitter at Milhouse 30.