WEBVTT

00:01.020 --> 00:02.489
Hello, this is Dr. Miller, and this

00:02.490 --> 00:03.749
is Episode nine point three of

00:03.750 --> 00:04.750
Assembly.

00:05.610 --> 00:07.139
Today, we're going to convert our

00:07.140 --> 00:09.029
program that had three

00:09.030 --> 00:10.402
inputs and created a max out

00:10.890 --> 00:11.879
of them. And we're going to use

00:11.880 --> 00:13.069
conditional execution.

00:14.040 --> 00:15.089
So here's our problem.

00:15.450 --> 00:17.214
So we are going to write a function.

00:17.670 --> 00:19.529
It'll be given three parameters and

00:19.530 --> 00:20.550
then we'll figure out which one of

00:20.580 --> 00:21.720
those is the maximum.

00:21.930 --> 00:23.189
And then we're going to return that

00:23.190 --> 00:24.299
result and we're going to use

00:24.300 --> 00:25.500
conditional execution.

00:26.100 --> 00:27.449
And then we're when we're done, we

00:27.450 --> 00:29.039
will compare this with the results

00:29.040 --> 00:30.750
that the compiler generates.

00:33.000 --> 00:34.420
So let's go ahead and start.

00:34.440 --> 00:35.440
So if we scroll

00:36.270 --> 00:37.979
down, we can

00:38.160 --> 00:39.809
see our program where we loaded our

00:39.810 --> 00:41.280
numbers. So we've got one, two

00:41.640 --> 00:43.200
and three are our parameters.

00:43.710 --> 00:44.849
And then this is the part where

00:44.850 --> 00:46.880
we're doing all of our comparison.

00:47.160 --> 00:48.570
And so we're going to basically

00:49.050 --> 00:51.030
end up taking out most of this.

00:59.210 --> 01:01.189
All right, so we do our

01:01.190 --> 01:02.190
comparison.

01:07.780 --> 01:09.459
OK, and then.

01:11.460 --> 01:12.749
What we want to do is we're going to

01:12.750 --> 01:14.269
use conditional execution so we

01:14.640 --> 01:16.499
can use another register, so, for

01:16.500 --> 01:17.500
example,

01:18.330 --> 01:19.829
we'll go ahead and use IDEX

01:20.400 --> 01:22.529
so we can do a C move

01:23.040 --> 01:24.689
and then we can do above.

01:24.720 --> 01:25.720
So if this if

01:26.700 --> 01:27.827
X is bigger, I'll put a

01:28.530 --> 01:30.810
comment here is bigger,

01:31.690 --> 01:33.779
then we want to move into,

01:33.780 --> 01:35.129
let's say, idex

01:36.990 --> 01:37.990
x.

01:39.420 --> 01:40.420
All right. So if

01:41.370 --> 01:42.889
if that one is bigger or above.

01:43.140 --> 01:44.099
Right, then we're going to do that,

01:44.100 --> 01:45.149
otherwise we're going to do the

01:45.150 --> 01:46.799
opposite. So we'll do C

01:47.040 --> 01:48.959
move below

01:48.960 --> 01:50.040
or equal.

01:50.430 --> 01:51.655
We're going to X and then

01:52.260 --> 01:53.319
we use SBX.

01:53.730 --> 01:55.349
Now, you could switch the equal on

01:55.350 --> 01:56.279
either one of these.

01:56.280 --> 01:57.897
It really wouldn't make much of a

01:57.930 --> 01:58.930
difference.

01:59.340 --> 02:00.900
So Ebix is

02:01.260 --> 02:02.260
bigger.

02:02.660 --> 02:03.660
All right.

02:03.930 --> 02:05.009
And then we have to do another

02:05.010 --> 02:06.137
comparison to do the X.

02:07.590 --> 02:08.962
So X has our second variable

02:09.539 --> 02:11.009
so we can do compare

02:12.150 --> 02:14.250
X and sorry,

02:14.580 --> 02:16.650
X, the bigger one

02:17.100 --> 02:19.740
from the first two to X.

02:21.870 --> 02:22.870
OK.

02:23.250 --> 02:25.020
And then we can basically say

02:26.250 --> 02:27.300
move

02:28.680 --> 02:29.680
C, move below

02:31.020 --> 02:31.949
or equal to.

02:31.950 --> 02:32.950
So if this one is

02:33.780 --> 02:35.669
below our equal to this one, then we

02:35.670 --> 02:37.889
want to move into X.

02:40.960 --> 02:41.960
OK, and then here

02:42.840 --> 02:44.580
at the end, when we're all done,

02:45.750 --> 02:47.580
we're going to move into

02:47.880 --> 02:49.919
Eddie's ex, which

02:49.920 --> 02:50.990
has our maximum,

02:52.230 --> 02:53.489
we're going to just copy that into

02:53.490 --> 02:54.490
Yak's.

02:56.070 --> 02:57.070
So this should

02:58.110 --> 02:59.727
yield us a program that does what

03:00.060 --> 03:01.319
the previous program does.

03:01.320 --> 03:02.986
But we notice that we are in a lot

03:03.000 --> 03:04.199
less lines of code here.

03:05.760 --> 03:07.379
So let's go ahead and build it.

03:13.000 --> 03:15.130
And then we can enter numbers

03:16.960 --> 03:18.490
and it'll figure out.

03:21.740 --> 03:23.150
Whichever one is the biggest

03:24.230 --> 03:26.169
and so it's interesting,

03:26.420 --> 03:27.420
this works and we

03:28.310 --> 03:30.199
can actually compare this to

03:30.200 --> 03:31.620
what a compiler would generate.

03:31.640 --> 03:32.640
So.

03:34.440 --> 03:35.879
We'll go ahead and look at this, so

03:35.880 --> 03:37.252
I wrote inside of C, I wrote

03:37.770 --> 03:39.340
the basic same program.

03:40.170 --> 03:41.669
So here we have we're returning a

03:41.670 --> 03:43.029
result of an integer.

03:43.050 --> 03:44.459
The function is called naks three.

03:44.460 --> 03:46.077
It takes three arguments A, B and

03:46.320 --> 03:47.849
C here.

03:47.850 --> 03:49.418
I set the result equal to zero I

03:49.710 --> 03:51.659
C of A is greater than B result

03:51.660 --> 03:53.610
is A, otherwise it's B

03:54.090 --> 03:55.707
and then comparing if C is bigger

03:55.830 --> 03:57.419
than the result. And the result here

03:57.420 --> 03:59.086
is C and then return the result at

03:59.310 --> 04:00.310
the end.

04:00.600 --> 04:02.219
And then this is just a machine that

04:02.220 --> 04:03.749
basically does the same things that

04:03.750 --> 04:04.750
we were doing.

04:05.700 --> 04:06.700
And so when we go

04:07.680 --> 04:09.260
ahead and compile that,

04:10.470 --> 04:12.580
so here I'm running G, C, C

04:12.630 --> 04:14.099
minus N thirty two means we're

04:14.100 --> 04:15.521
generating thirty two bit and

04:16.079 --> 04:17.369
then we're going to compile the

04:17.370 --> 04:18.659
program called Maxey.

04:18.870 --> 04:20.009
And then the second command I'm

04:20.010 --> 04:21.719
going to issue is object dump

04:22.170 --> 04:24.089
to show what the assembly is

04:25.110 --> 04:26.459
and the file it's looking at

04:27.030 --> 04:27.989
is eight out out.

04:27.990 --> 04:29.656
It's disassembling it and then I'm

04:29.730 --> 04:31.151
searching for my function Max

04:32.160 --> 04:33.449
and this printing off the first

04:33.450 --> 04:35.130
thirty lines of the function Max.

04:38.070 --> 04:39.779
So if we go through and we look at

04:39.780 --> 04:41.879
this, we can see things like here's

04:41.880 --> 04:43.595
our parameter or result getting set

04:43.860 --> 04:45.779
to zero, it's loading

04:45.780 --> 04:47.054
two of the parameters, EBP

04:47.850 --> 04:49.769
plus a 10 plus 12

04:50.550 --> 04:51.550
doing a jump.

04:52.260 --> 04:53.670
And then it's going to do another

04:54.150 --> 04:56.399
load of the next parameter at 10

04:56.400 --> 04:57.821
and then do a compare of that

04:58.410 --> 04:59.880
with our local variable

05:00.480 --> 05:01.709
and then do another jump.

05:01.830 --> 05:03.449
Right. So we can see that's what

05:03.450 --> 05:04.450
happens.

05:05.790 --> 05:07.049
So then it's interesting that you

05:07.050 --> 05:09.119
can take the program and

05:09.120 --> 05:11.040
you can the minus o

05:11.070 --> 05:12.959
switch will tell the compiler

05:12.960 --> 05:14.870
that you want to do optimizations.

05:15.030 --> 05:16.206
So by default it's doing

05:16.290 --> 05:18.005
optimization zero, which means it's

05:18.060 --> 05:19.079
not optimizing.

05:19.920 --> 05:21.292
And 003 is one of these sort

05:21.750 --> 05:22.926
of maximum optimizations

05:23.610 --> 05:24.539
that we can run.

05:24.540 --> 05:25.649
So we'll go ahead and compile it

05:25.650 --> 05:27.569
with the optimizations and

05:27.570 --> 05:29.579
then again, look for our

05:30.270 --> 05:31.470
function called Max.

05:32.810 --> 05:34.039
And so you can see that our program

05:34.040 --> 05:35.209
all of a sudden becomes a lot

05:35.210 --> 05:37.639
smaller, it actually becomes

05:38.000 --> 05:39.617
similar to the one we just looked

05:39.860 --> 05:40.860
at. So here it again

05:41.870 --> 05:43.519
is loading our parameters into

05:43.520 --> 05:44.520
registers.

05:44.690 --> 05:46.160
It's doing a compare

05:47.180 --> 05:48.180
move. Right.

05:48.210 --> 05:49.489
And then another compare, and it's

05:49.490 --> 05:50.710
actually just using X.

05:50.720 --> 05:52.141
So it's even, you know, using

05:52.640 --> 05:54.820
that as the result.

05:54.830 --> 05:56.349
So it becomes even more compact

05:56.720 --> 05:57.720
than ours.

05:57.890 --> 05:59.479
But the point of ours is to show how

05:59.480 --> 06:00.529
you would do it. And then you can

06:00.530 --> 06:02.119
see that those optimizations when

06:02.120 --> 06:03.120
they're turned on is

06:04.100 --> 06:05.374
what the compiler will do.

06:05.510 --> 06:07.729
So it it sees that these moves

06:07.730 --> 06:09.259
are less expensive

06:09.860 --> 06:11.959
than doing the compares

06:11.960 --> 06:13.819
and the jumps, which is the default

06:13.820 --> 06:14.820
compilation.

06:17.390 --> 06:19.056
So today we looked at our three no

06:19.190 --> 06:20.807
Max, and then we compared that to

06:20.990 --> 06:22.459
the compiler results and learned a

06:22.460 --> 06:23.881
little bit about optimization

06:24.860 --> 06:26.389
and what the compiler would actually

06:26.390 --> 06:27.909
do, which is to use conditional

06:28.040 --> 06:29.040
moves.

06:30.410 --> 06:31.609
So going forward, we're going to

06:31.610 --> 06:32.933
talk about a raise and also

06:33.680 --> 06:35.329
operations that we can do on strings

06:35.330 --> 06:36.439
which are also raise.

06:37.900 --> 06:39.009
So you have questions, you can

06:39.010 --> 06:40.137
e-mail me, Miller, M.J.

06:40.250 --> 06:42.009
at you and KSTU, and you can find me

06:42.010 --> 06:43.509
on Twitter at Milhouse 30.

