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
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.
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 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.
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
and then 16. Let's see. Did I push them all on?
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
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.
and then do our return.
So here, say, registers
from in here, this is gonna be the
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 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
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.
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 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.
we'll use e exes are bigger thing. Some move into
so yea X is bigger. Take jump.
So it is not bigger than we need to move. 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.
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
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,
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.