# Conditional Move Max Example

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.3 of Assembly.
00:05
Today, we're going to convert our program that had three inputs and created a max out of them, and we're going to use conditional execution.
00:13
So here's our problem. So we are going to write a function. It will be given three parameters, and then we'll figure out which one of those is the maximum, and then we're going to return that result, and we're going to use conditional execution.
00:26
And then we're when we're done, we will compare this with the results that the compiler generates.
00:32
So let's go ahead and start. So
00:35
if we scroll down, we can see our program where we loaded our numbers. So they got 12 and three are parameters.
00:43
And then this is the part where we're doing all of our comparison. And so we're gonna basically
00:49
end up taking out most of this.
00:59
All right,
01:00
so we do our comparison,
01:07
okay. And then
01:11
what we want to do is we're gonna use conditional execution so we can use another register. So, for example, um, we'll go ahead and use e d. X
01:19
so we can do a c move
01:23
and then we can do above. So if this
01:26
if e x is bigger, I'll put a comment here
01:29
is bigger
01:30
Then we want to move into let's say GDX
01:36
e X.
01:38
All right, so
01:40
if if that one is bigger or above right then we're gonna do that otherwise we're going to the opposite so we'll do see move below, are equal will go into E d X and then we use ebx. Now you could have switched the equal on either one of these Really wouldn't make much of a difference.
01:59
E b x is bigger,
02:01
all right?
02:02
And then we have to do another comparison to do the e c x ccx has our second variable so we can do compare
02:12
e c X and sorry e d x
02:15
the bigger one from the 1st 2 to e c x.
02:21
Okay, and then we can basically say, um, move,
02:28
see, move
02:30
below or equal to. So
02:31
if this one is below are equal to this one, then we want to move into e d x
02:38
e c X,
02:39
then came
02:42
and then here at the end. When we're all done,
02:45
we're going to move into E D X, which has our maximum.
02:52
We're gonna just copy that into the X.
02:55
So
02:57
this should yield us a program that does what the previous program does. But we noticed that we're in a lot less lines of code here.
03:05
So let's go ahead and build it.
03:12
And then we can enter numbers
03:15
and it will figure out
03:21
whichever one is the biggest.
03:23
And so
03:24
it's interesting that this works and we can actually compare this to what a compiler regenerate. So
03:34
we'll go ahead and look at this. So I wrote inside of See, I wrote this basic same program.
03:39
So here we have, we're returning a result of an imager. The function is called max three. It takes three arguments A, B and C.
03:46
Here I set. The result equals zero. I see if a is greater than be result is a otherwise it's b
03:53
and then comparing IFC is bigger than the result, and the result here is see and then return the result at the end.
04:00
And then this is just a main that basically does the same things that we were doing.
04:04
And so
04:06
when we go ahead and compile that,
04:10
so here I am running GCC minus and 32 means we're generating 32 bit.
04:15
And then we're going to compile the program called Maxie, and then the second command I'm an issue is object dump
04:21
to show what the assembly is.
04:24
Um, in the file it's looking at is a doubt out, its disassembling it. And then I'm searching for my function, Max and this printing off the 1st 30 lines of the function. Max.
04:38
So if we go through and we look at this, we can see things like here is our parameter a result getting set to zero. It's loading two of the parameters E v P plus eight and plus 12.
04:49
I'm doing a jump, and then it's going to do another load of the next parameter a 10 and then do a compare that with our local variable
05:00
and then do another jump right so we can see that's what happens.
05:04
So then it's interesting that you can take the program
05:08
and you can. The minus Oh, switch will tell the compiler that you want to do optimization, so by default it's doing optimization zero, which means it's not optimizing,
05:18
and 03 is one of these sort of maximum optimization is that we can run, so we'll go ahead and the pilot with the optimization. And then again look for our function called Max
05:31
and so you can see that our program all the sudden becomes a lot smaller.
05:35
It actually becomes similar to the one we just looked at. So here it again is loading our parameters into registers. It's doing a compare move, right and then another compare, and it's actually just using a AK, so it's even,
05:51
you know, using that as the results. So it becomes even more compact than ours. But the point of ours is to show how you would do it, and then you can see that those optimization is when they're turned on
06:02
is what the compiler reduce. So it it sees that thes see moves are less expensive
06:09
than doing the compares in the jumps, which is the default compilation.
06:16
So today we looked at our three number max, and then we compared that to the compiler results and learned a little bit about optimization and what the compiler would actually do, which is to use conditional moves.
06:30
So going forward, we're going to talk about a raise. And also operations weaken due on strings which are also a raise.
06:36
So have questions. You can email me, Miller, MJ at you and Kate I e to you. And you can find me on Twitter at Milhouse 30.
Up Next