Hello, this is Dr. Miller, and this
is Episode six point seven of
Assembly.
Today, we're going to talk about
function arguments,
function arguments.
These allow us to pass parameters
to a function.
This allows the function to do
different things based on those
parameters.
And it turns out we only have a
limited number of registers.
And so we're going to end up having
to use the stack.
And so because of that, we are going
to by default, we're going to start
using an EVP based stack.
We've talked about that in previous
lectures.
So we set up our function using our
Prolog and then at the end
we're going to restore our function
using the epilog.
And so every time you write a
function, you should use these
by default.
And then recall, when we set up
using a Prolog, right, we have
SBP, which is our extended base
pointer, so the base pointer is
going to point to the old value of
EBP. So that's what's on top of the
stack.
And then we're going to have our
return address and that's going to
be at EBP plus four.
And then argument one is going to be
at EBP plus eight, an
argument two, is it EBP plus C?
Argument three is at EBP
plus 10 Hex.
And then it goes so on
and so forth. So 14, 18
Wannsee,
so for example, right, if
we want to copy the data, the first
parameter into a register X,
we can use SBP plus eight
if we want to copy the second
parameter.
So in a Nazeem, we have to
put a zero if we're using a letter
to start.
And that's because otherwise it
thinks it's a variable.
So we got zero C in hex.
So that's our second parameter.
So you can try on your own to figure
out what is the third parameter.
So the best way to learn
this is to do an example.
So we're going to do example while
we're going to
read one integer and print it and
then we'll we'll modify it so that
it uses two.
And then we're going to use print F
and we're going to print the values
in hexadecimal.
All right, so we have our
I've already created our project.
So I'll go ahead and start entering
it, so the first thing that we're
going to do is we're going to set up
our format.
So our format is going to have
a zero X at the beginning so that we
know this is a hex and
then you can look up the printf
string so percent zero
eight and then an uppercase
X. So this is going to print off
zero leading digits.
So the if we have less
than eight digits, it'll print
zeros.
We're going to print up to eight
digits.
The X means we're going to print in
hex and the uppercase
X means it's going to be uppercase
X uppercase hexadecimal.
And then we'll put a new line at the
end so that we have that at the end.
Then in our main, we'll go ahead
and read our numbers.
And we're going to push our number
onto this.
So remember, read into
the result Wozniak's so we're going
to push that onto the top of our
stack and
our function is going to be called
Pantex.
So I'll do that, and then while I'm
here, I'm going to remember to
correct the stack, so I put
something on, so I need to pop it
off. So I'll just do add
E.S.P four,
because I pushed on one argument.
So now we'll come down here and to
our function
here is a function.
We'll do our Prolog, push BP,
move
EVP,
ESP.
I'll come down here and do my epilog
while I'm remembering move
sbp e.s.p
pop SBP
return.
So now I'm ready to do the main part
of my function.
And so what I'd like to do here is
I'd like to copy into
Yak's my first parameter.
Some vacs
remember it's an EBP based act, so
the Valuate SBP
plus eight,
I'll move that index and now I'm
going to call my my Prenter
function.
So the first argument is the format
during the second argument is the
value I want to print.
So I got to push my value first.
So I'm going to push the X
and then I will
move the X
format, push
X and I push the format.
So I've added two things on top of
the stack.
I'm going to call print F
and then I need to correct the stack
because I've just printed off
something.
So add E.S.P
eight because I pushed on two
things, each one of them taking up
four bytes.
Now, if you remember from the
printf, we have
to tell it the print of as an
external function,
so we have to say extern.
Right.
OK,
so I've accessed my first parameter,
I pushed that on and I'm going to
have my format and now I'm
pushing on my format, calling
print and then correcting the stack
so we should have
everything lined up.
All right, complains about my clock
being off, which is fine, so
let's go ahead and run this.
So if I put in the number one
hundred, it prints me
off at the zero X at
the beginning, it prints off
the leading digits, so I got six
zeros in the front and then one
hundred and hexadecimal is sixty
four.
Or I can do something like zero X,
B
and again it's printing an all
uppercase because I did that with
mine.
So let's suppose that we wanted to
read in two numbers, let's
go ahead and go back in here.
So I read one number,
push it so I can do this again,
call, read
it pushbacks.
All right, so now I've pushed on a
second parameter.
So now instead of doing before,
I need to do by eight.
Right. Because I pushed on two
things. I got to basically pop them
off or remove them.
And I'm doing it using add you
could do two pops.
That would be an equivalent way to
do it.
So here I grab the first parameter.
So now I want to grab my second
parameter.
So my second parameter should be
SBP
plus C.
I'll do this a little different
so I can directly push the format,
because it's just an address.
Call my printer function, and I got
to correct the stack because I
pushed on two things.
Ate
my function, epilogs should be the
same.
So I haven't changed anything in
that.
OK,
now if I run, make
and then run my program so I can
type in one hundred and Zurich's
cafe.
And so they push these on
in the reverse order, so CAFE
is the first one that gets printed.
So that's argument one and
one hundred is argument two.
And that's just by the way, I push
them onto the stack.
So today, we talked about function
arguments, we gave an example, we
ran the example, we modified it so
that it had more than one argument
and we made sure that we corrected
the stack every time.
So looking forward, we're going to
talk about saving registers when you
modify them in a function and we'll
look at some more complicated
functions.
So here's our quiz.
What is the first parameter of an
MVP based Steck?
Would be plus eight,
so EBP plus eight.
What's the third parameter of an MVP
based Steck?
So that would be 10 X or
16 days, 10.
If you have questions, you can
contact me at millirems, at Unica,
Edu, and you can find me on Twitter.
Atencio has 30.