13 hours 15 minutes
Oh, this is Dr Miller. And this is Episode 6.7 of Assembly.
Today we're gonna talk about function arguments,
so 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 gonna end up having to use the stack.
And so because of that, we're going to by default, we're going to start using anti BP based Stack. We've talked about that in previous lectures.
So we set up our function using our prologue, and then at the end, we're going to restore our function using the epilogue.
And so every time you write a function,
you should use these by default
and then recall when we set up using a prologue, right? We have a B p, which is R extended base pointer.
So the base pointer is gonna point to the old value of e v p. So that's what's on top of the stack.
And then we're gonna have our return address, and that's gonna be a T V P plus four
and then argument one is going to be a T B p plus eight and argument to a city BP plus C argument three is at a BP plus 10 hex,
and then it goes so on and so forth. So 14 18 1 C.
So, for example, right, If we want to copy the data the first parameter into a register ea X, we can use E b P plus eight
if we want to copy the second parameter. So in a Nazem, we have to put a zero if we're using a letter to start, and that's because otherwise that thinks it's a variables. We got zero C in hex.
So that's our second parameter.
So you can try and your own to figure out what is the third parameter.
the best way to learn this is to do an example. So we're gonna do example, while we're going to,
um, read one manager and print it, and then we'll we'll modify it so that it uses to, and then we're gonna use print F, and we're gonna print the values in Hexi Decimal.
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 gonna have
zero X at the beginning so that we know this is a hex,
and then you can look up the print F string. So percent 08
and then in upper case X, so it's gonna print off zero leading digits. So the if we have less than eight digits, it'll print zeros.
We're gonna print up to eight digits. The X means we're gonna print in hex and the upper case X means it's gonna be uppercase.
uppercase Hexi decimal.
And then we'll put a new line it end so that we have that at the end.
Then in our main, we'll go ahead and
read our numbers,
and we're gonna push our number onto the stack. So remember, read int.
The result is in the X, So we're gonna push that on to the top of our stack,
and our function is going to be called print Tex,
I'll do that. And then while I'm here, I'm gonna remember to correct the stack. So I push something on, so I need to pop it off. So I'll just to add yes, p
four Because I pushed on one argument.
So now will come down here into our function.
Here is a function Will do all prologue. Push BP, move!
E v P
Yes, P I'll come down here and do my epilogue while I'm remembering
movie B p e S p pop e v P
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 E X my first parameter. So Movie X,
remember, it's an EVP based act, so the value at E v P
I'll move that N d a x, and I'm gonna call my my printout function.
So the first argument is A formats during the second argument is the value I went to print. So I gotta push my value first, so I'm gonna push the X
and then I will move the X
push. X and I pushed the format.
So I've added to things on top of the stack.
I'm gonna call print F, and then I'm need to correct the stack because I've just printed off something. So add
yes, p eight Because I pushed on two things, each one of them taking up four bites.
Now, if you remember from the print F,
we have to tell it that print of is an external function.
So I have to say X turn
so I've access my first parameter. I have pushed that on and I'm gonna have my format. And now I'm pushing on my format,
calling print F and then correcting this deck
we should have everything lined up.
Alright. Complains about my clock being off, which is fine. So let's go ahead and run this.
So if I put in the number 100
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 100 Hexi Decimal is 64.
Or I could do something like zero x beef.
And again, it's printing in 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
push the X
right. So now I have pushed on a second parameter. So now, instead of doing by four, I need to do by eight.
Right, Because I pushed on to things. I gotta basically pop them off or remove them. And I'm doing it using ad You could do to 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
E v P.
I'll do this 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 stock because I pushed on two things.
My function epilogue should be the same, right? So I haven't changed anything in that.
now, if I run, make
and then run my programs like in Type in 100 Rex
it pushed these on in the reverse order. So Cafe is the 1st 1 that gets printed, so it's argument one and 100 is argument, too, and that's just by the way I pushed him 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 gonna 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 Anne BP based Deck
B Plus eight UBP plus eight?
What's the third parameter of a need? BP based Tech.
So that would be 10 hex or 16 base 10.
If you have questions, you can contact me at Miller MJ at Yoon Kato et you, and you can find me on Twitter at No House 30.