# Max of Three Numbers

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:00
Hello. This is Dr Miller, and this is Episode 9.1 of Assembly.
00:05
Today, we're going to give an example of how to do
00:07
a three number Max.
00:10
So for this example, what we're going to do is we're going to calculate the max of three numbers.
00:15
I will read the three numbers in via Scan F and we'll get introduced to using an array
00:21
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.
00:28
So I've already created my project here.
00:33
So
00:36
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.
00:44
So percent who
00:49
percent do fine.
00:51
We're called us in one
00:54
e b p plus eight. Totally our first argument, and we'll do the same for the next two.
01:07
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,
01:15
and then three,
01:17
it's gonna be BP
01:19
cost 10 hex.
01:21
So a 12
01:23
and 16
01:25
or eight C and 10
01:29
and then here. We're gonna define what our skin that format is,
01:36
and we're just gonna basically allow it to skin in three integers.
01:42
Got to make sure regional terminate it.
01:46
And then down here in the BSS, we're gonna define an array.
01:53
So the rez means reserve d means d word. And so we're going to reserve three D words,
02:00
and we're gonna be using scan. If so, we gotto say that that's X turn.
02:08
All right,
02:09
So in order to,
02:12
um, scan these in, we basically have to give it three different addresses that we want to use to store our numbers.
02:20
And so
02:23
the 1st 1 that we're gonna use is we're gonna basically load the address of the array,
02:28
and we'll just push that onto the stack. So this is a pointer to that location in memory.
02:34
Now,
02:35
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,
02:52
02:57
and then we'll go ahead and push that address on the stick,
03:01
and then we can add again
03:04
another four to point to the next one.
03:09
03:13
and we'll push that point or onto the stack,
03:15
and then we gotta push our format,
03:23
and then we're gonna call
03:24
scan if
03:28
and then we've got to make sure that we correct the stack. So
03:30
if we count all of the things that we added on here, that is 14 hex. So they got
03:37
for 8 12
03:40
and then 16. Let's see. Did I push them all on?
03:47
Okay,
03:49
so we should and
03:54
looks like 10.
03:58
Oh, go with that.
04:01
All right.
04:02
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.
04:15
And then we can push that onto the stack,
04:18
and then we can move into e x,
04:23
the value of array plus four
04:28
and push that value. So these air de referencing that pointer
04:31
and then getting us the value.
04:33
So those are the numbers that we read being the keyboard
04:36
and then do the last one
04:40
Ex array plus eight.
04:47
And then we're gonna call our function Call
04:51
Max.
04:53
Three is gonna be the name,
04:55
and then we'll go ahead and again correct our stack.
04:58
Um, and
05:00
yes, p
05:04
on the results should be in the X. So we're just gonna call
05:10
print int and call
05:14
print New line.
05:16
So now we got to go and create our function that we're going to do the max of of three numbers.
05:26
So we're gonna call this Max three,
05:31
go ahead and enter our function and allocate some space.
05:36
We're going to save our registers.
05:45
Okay,
05:46
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
05:51
Poppy di, ex pop B C X
05:58
up e
06:00
be x,
06:01
and then we'll use leave because we can.
06:08
But I can tape
06:10
and then do our return.
06:14
So here, say, registers
06:27
from in here, this is gonna be the
06:29
and of our loop.
06:31
Okay, We're end of our function, actually.
06:34
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
06:46
go ahead and load that into a register. So I'm going to move into the A X one.
06:51
Now, this is going to replace that with E B. P.
06:59
Uh, Let's see. Let's double check E v P
07:03
plus eight for the first parameter.
07:08
So that's what it's gonna get actually replaced with.
07:11
But it's a lot easier. Were to type just thes instead of trying to remember what that is.
07:16
So movie be X
07:19
Teoh movie, See Ex
07:24
three
07:25
So now we have inside of our three registers
07:28
the three numbers that they entered and we got via scan If and so now we're going to do
07:38
do our comparison
07:40
So we'll do Go ahead and compare the a X b X
07:46
and then jump above.
07:47
I'll just pick a label. A one.
07:50
So this is
07:53
yaks is bigger. Take the jump.
07:59
Okay.
08:00
And then we'll define a one here,
08:03
so he access bigger. We're gonna move into,
08:07
um,
08:07
our maximum, which see if I
08:11
we need to add that as a
08:13
percent define.
08:16
So this is gonna be a local variable
08:20
and it's going to be e v P minus eight.
08:24
So go ahead and go down. We'll go down before
08:28
that should be what we need to go for it.
08:33
Okay,
08:35
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
08:43
into Max.
08:50
Yea x.
08:52
All right. And then we're gonna jump to the second part. We're going to do another comparison.
09:03
Okay?
09:05
So if the X is not bigger, that means e b X is bigger. So we're gonna move into Max bx
09:13
09:18
So now here is part
09:20
to
09:22
So now we've compared yea x and ebx and we've saved into Max the one that is bigger.
09:30
And so
09:31
what we can do is we can load into yea X the bigger one, which is whatever is stored at
09:37
Max and then we can do a compare of yaks and e c x.
09:43
So again, jump above,
09:46
we'll call it be one.
09:50
So inside of b one,
09:52
we'll use e exes are bigger thing. Some move into
09:56
max Yea x
10:00
Joan to done
10:03
so yea X is bigger. Take jump.
10:09
So it is not bigger than we need to move. You see, ex
10:15
move into Max
10:16
you see ex
10:18
because now it's bigger. And then we need to make sure we do our jump so that we,
10:24
um don't run the next branch. So we have to say jump done.
10:31
All right.
10:33
And then inside. Done. Well, I guess I called it done. I should call it done here.
10:39
Here we go.
10:41
So what we're gonna do is move into yea x
10:45
max. So again, that's a local variable,
10:48
and it has the maximum that we have. And so we're just gonna copy it from our local variable into the
10:54
result that we have for our function.
11:00
And now everything should be all set up appropriately so that it works.
11:07
We'll see. It looks like it didn't fail in the compilation.
11:13
So if I type numbers 123
11:16
or 3 to 1
11:20
or 13 34 to
11:26
and another way to run this if you don't want to do that, you can do echo
11:33
1 200
11:35
And then you can send that output of the Echo Command into the running this command.
11:41
So I'd be like, I typed it into the keyboard,
11:46
but I don't have to
11:48
type it continuously.
11:50
So it's just another way that you can You can run your test.
11:56
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.
12:11
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.
12:26
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.
Up Next