Hello, this is Dr. Miller, and this
is Episode nine point three 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'll 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 we've got one, two
and three are our parameters.
And then this is the part where
we're doing all of our comparison.
And so we're going to basically
end up taking out most of this.
All right, so we do our
comparison.
OK, and then.
What we want to do is we're going to
use conditional execution so we
can use another register, so, for
example,
we'll go ahead and use IDEX
so we can do a C move
and then we can do above.
So if this if
X is bigger, I'll put a
comment here is bigger,
then we want to move into,
let's say, idex
x.
All right. So if
if that one is bigger or above.
Right, then we're going to do that,
otherwise we're going to do the
opposite. So we'll do C
move below
or equal.
We're going to X and then
we use SBX.
Now, you could switch the equal on
either one of these.
It really wouldn't make much of a
difference.
So Ebix is
bigger.
All right.
And then we have to do another
comparison to do the X.
So X has our second variable
so we can do compare
X and sorry,
X, the bigger one
from the first two to X.
OK.
And then we can basically say
move
C, move below
or equal to.
So if this one is
below our equal to this one, then we
want to move into X.
OK, and then here
at the end, when we're all done,
we're going to move into
Eddie's ex, which
has our maximum,
we're going to just copy that into
Yak's.
So this should
yield us a program that does what
the previous program does.
But we notice that we are in a lot
less lines of code here.
So let's go ahead and build it.
And then we can enter numbers
and it'll figure out.
Whichever one is the biggest
and so it's interesting,
this works and we
can actually compare this to
what a compiler would generate.
So.
We'll go ahead and look at this, so
I wrote inside of C, I wrote
the basic same program.
So here we have we're returning a
result of an integer.
The function is called naks three.
It takes three arguments A, B and
C here.
I set the result equal to zero I
C of A is greater than B result
is A, otherwise it's B
and then comparing if C is bigger
than the result. And the result here
is C and then return the result at
the end.
And then this is just a machine that
basically does the same things that
we were doing.
And so when we go
ahead and compile that,
so here I'm running G, C, C
minus N thirty two means we're
generating thirty two bit and
then we're going to compile the
program called Maxey.
And then the second command I'm
going to issue is object dump
to show what the assembly is
and the file it's looking at
is eight out out.
It's disassembling it and then I'm
searching for my function Max
and this printing off the first
thirty lines of the function Max.
So if we go through and we look at
this, we can see things like here's
our parameter or result getting set
to zero, it's loading
two of the parameters, EBP
plus a 10 plus 12
doing a jump.
And then it's going to do another
load of the next parameter at 10
and then do a compare of 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 o
switch will tell the compiler
that you want to do optimizations.
So by default it's doing
optimization zero, which means it's
not optimizing.
And 003 is one of these sort
of maximum optimizations
that we can run.
So we'll go ahead and compile it
with the optimizations and
then again, look for our
function called Max.
And so you can see that our program
all of a 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 X.
So it's even, you know, using
that as the result.
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 optimizations when
they're turned on is
what the compiler will do.
So it it sees that these moves
are less expensive
than doing the compares
and the jumps, which is the default
compilation.
So today we looked at our three no
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 that we can do on strings
which are also raise.
So you have questions, you can
e-mail me, Miller, M.J.
at you and KSTU, and you can find me
on Twitter at Milhouse 30.