Hello, this is Dr. Miller, and this
is Episode nine point one of
assembly today we're
going to give an example of how to
do a three no,
Max.
So for this example, what we're
going to do is we're going to
calculate the max of three numbers,
will read the three numbers in
Bioscan F, and we'll
get introduced to using an array
and then we're going to return the
result in X and we're going to try
and use some local variables for
our program.
So I've already created my project
here.
So
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.
So do fine.
And we're going to call this in one
SBP plus eight,
so that'll be our first argument
and we'll do the same for the next
two.
I do zero, see, because it's
a letter, and so you've got to start
with a zero if it's going to be in
Hex
and then three
is going to be BP
plus 10 hex.
So we got eight, 12
and 16 or
eight, C and 10.
And then here we're going to define
what our SCANA format is.
And we're just going to basically
allow it to scan in three
integers.
Got to make sure we will terminate
it.
And then down here in the best,
we're going to find an array.
So the rest means reserve,
D means D word, and so we're going
to reserve three D words
and we're going to be using ganef.
And so we got to say that that's
extern.
All right, so in
order to
scan these in, we basically have to
give it three different addresses
that we want to use to store
our numbers.
And so
the first one that we're going to
use is we're going to 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.
Now, the
each one of these D words takes up
four bytes. So to the address, the
next one, we're going to add four
bytes.
So add X
for.
So here we get the base address
and then we'll go ahead and push
that address onto the stack
and then we can add again
another four to point to the next
one.
So we added four, added four more,
and we'll push that pointer onto the
stack and
then we got to push our format.
And then we're going to call
Scantest
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
Hecks. So we've got four,
eight, 12.
And then 16, I
see, did I push them all on?
OK, so we should
add.
Looks like 10.
Oh, go with that.
All right.
And now we want to
go ahead and push those items
onto our stack so
we can move in to the values,
store that array,
and then we can push that onto the
stack.
And then we can move into X
the value of Arae
plus four
and push that value.
So these are D referencing that
pointer and then getting us
the value.
So those are the numbers that we
read via the keyboard and then
do the last one. You've
got x
ray plus eight.
And then we're going to call our
function to call
Max three
is going to be the name and then
we'll go ahead and again correct our
stack and
E.S.P.
And then the result should be
an X, so we're just going to call
Sprint and and
call
brand new line.
So now we've got to go and create
our function that we're going to do
the max of three numbers.
So we're going to call this Max
three.
Go ahead and enter our function and
allocate some space.
We're going to save our registers.
OK, we're not going to
push X because we're actually going
to use that in our function at the
end of our function, we're going to
go ahead and say Pop,
MDX, Pop,
DCX,
Pop to
be X
and then we'll use leave
because we can
I can type and
then do our return.
So here say
registers.
And here, this is going to be the
end of our Loopt.
OK, our end of our function,
actually,
so what we want to do is we're going
to use those percent defines that we
define. So we could type this all
out and say EBP 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
X one.
Now, this is going to replace that
with SBP.
Policy Doublecheck
SBP
plus eight for the first parameter,
so that's what it's going to get
actually replaced with,
but it's a lot easier to type just
these instead of trying to
remember what that is.
So move SBX
to move
DCX
three.
So now we have inside of our three
registers the three numbers
that they entered and we got bioscan
eugh. And so now we're going to do.
Do our comparison, so
we'll do go ahead and compare
the CBX
and then jump above
and I'll just pick a label, a one.
So this is
yak's is bigger.
Take the jump.
OK, and then we'll define
a one here.
So he is bigger.
We're going to move into.
Our maximum, which
let's see if we need to add
that as a
percent define.
So this is going to be a local
variable
and it's going to be SBP
minus eight.
So go ahead and go down.
We'll go down for
it. Should be what we need to go for
it.
OK, so
we'll do a comparison of X and SBX,
and then if that's true, we're going
to move. So if X is bigger,
then we're going to move
into Max.
Yikes.
All right, and then we're going to
jump to the second part, so we're
going to do another comparison.
OK, so
if Vioxx is not bigger, that
means SBX is bigger,
so we're going to move into
max Vioxx
and then here we'll also jump to
part two.
So now here is part
two.
So now we've compared yak's
and SBX and we've saved into
Max, the one that is bigger.
And so what we can do
is we can load into yak's
the bigger one, which is whatever is
stored at Max,
and then we can do a compare Vioxx
and DCX.
So again, jump above,
I'll call it B one.
So instead of B one,
we'll use X as our bigger thing.
Some move into
max eaa x
shown to done
so X is
bigger.
Take the jump.
So if it's not bigger than we need
to move DCX.
Move into Max DCX
because now it's bigger and then we
need to make sure we do our jumps so
that we don't
run the next branch, so we have to
say jump down.
All right,
and then inside, done well,
I guess I called it done, I should
call it done here.
Here we go.
So what we're going to do is move
into Yak's Max.
So, again, that's our local variable
and it has the maximum that we have.
And so we're just going to 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.
Looks like it didn't
fail in the compilation,
if I type numbers one, two, three
or three to one
or one, three,
three, four, two.
And another way to run this, if you
don't want to do that, you can do
echo.
One to
hundred,
and then you can send that output
of the Echo command into the running
this command, so it'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, we use
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 use jumps in order to
do some of those operations.
We'll talk about a raise and
then some strings and string
operations that are associated with
them.
If you have questions, you can email
me Milgrim, Jet, you, Ankita, Aidoo,
or you can find me on Twitter at
Milhouse 30.