Hello. This is Dr Miller, and this is Episode 9.3 of Assembly.
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.
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.
And then we're when we're done, we will compare this with the results that the compiler generates.
So let's go ahead and start. So
if we scroll down, we can see our program where we loaded our numbers. So they got 12 and three are parameters.
And then this is the part where we're doing all of our comparison. And so we're gonna basically
end up taking out most of this.
so we do our comparison,
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
so we can do a c move
and then we can do above. So if this
if e x is bigger, I'll put a comment here
Then we want to move into let's say GDX
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.
And then we have to do another comparison to do the e c x ccx has our second variable so we can do compare
e c X and sorry e d x
the bigger one from the 1st 2 to e c x.
Okay, and then we can basically say, um, move,
below or equal to. So
if this one is below are equal to this one, then we want to move into e d x
and then here at the end. When we're all done,
we're going to move into E D X, which has our maximum.
We're gonna just copy that into the X.
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.
So let's go ahead and build it.
And then we can enter numbers
and it will figure out
whichever one is the biggest.
it's interesting that this works and we can actually compare this to what a compiler regenerate. So
we'll go ahead and look at this. So I wrote inside of See, I wrote this basic same program.
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.
Here I set. The result equals zero. I see if a is greater than be result is a otherwise it's b
and then comparing IFC is bigger than the result, and the result here is see and then return the result at the end.
And then this is just a main that basically does the same things that we were doing.
when we go ahead and compile that,
so here I am running GCC minus and 32 means we're generating 32 bit.
And then we're going to compile the program called Maxie, and then the second command I'm an issue is object dump
to show what the assembly is.
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.
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.
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
and then do another jump right so we can see that's what happens.
So then it's interesting that you can take the program
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,
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
and so you can see that our program all the sudden becomes a lot smaller.
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,
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
is what the compiler reduce. So it it sees that thes see moves are less expensive
than doing the compares in the jumps, which is the default compilation.
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.
So going forward, we're going to talk about a raise. And also operations weaken due on strings which are also a raise.
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.