13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 9.1 of Assembly.
Today, we're going to give an example of how to do
a three number Max.
So for this example, what we're going to do is we're going to calculate the max of three numbers.
I will read the three numbers in via Scan F and we'll get introduced to using an array
and then we're gonna return the result in the A X and we're gonna try and use some local variables for our program.
So I've already created my project here.
in order to make our life easier, we're going to use some percent defines in order to define the variables that we're going to use inside of our function.
So percent who
percent do fine.
We're called us in one
e b p plus eight. Totally our first argument, and we'll do the same for the next two.
I do zero C because it's a letter, and so you got to start of the zero. If it's gonna be in hex,
and then three,
it's gonna be BP
cost 10 hex.
So a 12
or eight C and 10
and then here. We're gonna define what our skin that format is,
and we're just gonna basically allow it to skin in three integers.
Got to make sure regional terminate it.
And then down here in the BSS, we're gonna define an array.
So the rez means reserve d means d word. And so we're going to reserve three D words,
and we're gonna be using scan. If so, we gotto say that that's X turn.
So in order to,
um, scan these in, we basically have to give it three different addresses that we want to use to store our numbers.
the 1st 1 that we're gonna use is we're gonna basically load the address of the array,
and we'll just push that onto the stack. So this is a pointer to that location in memory.
the each one of these d words takes up four bites. So to the address, the next one, we're gonna add four bites. So on e x four,
ever get they based address,
and then we'll go ahead and push that address on the stick,
and then we can add again
another four to point to the next one.
He added for added for more,
and we'll push that point or onto the stack,
and then we gotta push our format,
and then we're gonna call
and then we've got to make sure that we correct the stack. So
if we count all of the things that we added on here, that is 14 hex. So they got
for 8 12
and then 16. Let's see. Did I push them all on?
so we should and
looks like 10.
Oh, go with that.
And now we want to, um, go ahead and push those items onto our stack so we can move into the X, the Value Store, that array.
And then we can push that onto the stack,
and then we can move into e x,
the value of array plus four
and push that value. So these air de referencing that pointer
and then getting us the value.
So those are the numbers that we read being the keyboard
and then do the last one
Ex array plus eight.
And then we're gonna call our function Call
Three is gonna be the name,
and then we'll go ahead and again correct our stack.
on the results should be in the X. So we're just gonna call
print int and call
print New line.
So now we got to go and create our function that we're going to do the max of of three numbers.
So we're gonna call this Max three,
go ahead and enter our function and allocate some space.
We're going to save our registers.
We're not going to push the a X because we're actually going to use that in their function. At the end of our function, we're gonna go ahead and say
Poppy di, ex pop B C X
and then we'll use leave because we can.
But I can tape
and then do our return.
So here, say, registers
from in here, this is gonna be the
and of our loop.
Okay, We're end of our function, actually.
So what we want to do is we're gonna use those percent defines that we define, so we could type this all out and say a BP plus eight. But it's probably just easier to set this up and then
go ahead and load that into a register. So I'm going to move into the A X one.
Now, this is going to replace that with E B. P.
Uh, Let's see. Let's double check E v P
plus eight for the first parameter.
So that's what it's gonna get actually replaced with.
But it's a lot easier. Were to type just thes instead of trying to remember what that is.
So movie be X
Teoh movie, See Ex
So now we have inside of our three registers
the three numbers that they entered and we got via scan If and so now we're going to do
do our comparison
So we'll do Go ahead and compare the a X b X
and then jump above.
I'll just pick a label. A one.
So this is
yaks is bigger. Take the jump.
And then we'll define a one here,
so he access bigger. We're gonna move into,
our maximum, which see if I
we need to add that as a
So this is gonna be a local variable
and it's going to be e v P minus eight.
So go ahead and go down. We'll go down before
that should be what we need to go for it.
so we'll do a comparison of yea x and E v x and then if that's true, we're gonna move. So if e x is bigger, then we're gonna move
All right. And then we're gonna jump to the second part. We're going to do another comparison.
So if the X is not bigger, that means e b X is bigger. So we're gonna move into Max bx
and then here will also jump to part two.
So now here is part
So now we've compared yea x and ebx and we've saved into Max the one that is bigger.
what we can do is we can load into yea X the bigger one, which is whatever is stored at
Max and then we can do a compare of yaks and e c x.
So again, jump above,
we'll call it be one.
So inside of b one,
we'll use e exes are bigger thing. Some move into
max Yea x
Joan to done
so yea X is bigger. Take jump.
So it is not bigger than we need to move. You see, ex
move into Max
you see ex
because now it's bigger. And then we need to make sure we do our jump so that we,
um don't run the next branch. So we have to say jump done.
And then inside. Done. Well, I guess I called it done. I should call it done here.
Here we go.
So what we're gonna do is move into yea x
max. So again, that's a local variable,
and it has the maximum that we have. And so we're just gonna copy it from our local variable into the
result that we have for our function.
And now everything should be all set up appropriately so that it works.
We'll see. It looks like it didn't fail in the compilation.
So if I type numbers 123
or 3 to 1
or 13 34 to
and another way to run this if you don't want to do that, you can do echo
And then you can send that output of the Echo Command into the running this command.
So I'd be like, I typed it into the keyboard,
but I don't have to
type it continuously.
So it's just another way that you can You can run your test.
So today we showed a sort of more complicated function. Example. We use some local variables, used three parameters for this example, and we were able to compute the maximum of three different numbers that were inputted by the user.
So in the future, we're going to talk about conditional execution or the notion that we can maybe not used jumps in order to do. Some of those operations will talk about a raise and then some strings and string operations that are associated with them.
If you have questions, you can email me Miller MJ at you and Kate I e. To you. Or you can find me on Twitter at Milhouse 30.