WEBVTT

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

00:02.490 --> 00:03.862
is Episode nine point two of

00:03.870 --> 00:04.870
Assembly.

00:05.670 --> 00:06.839
Today, we're going to talk about

00:06.840 --> 00:09.179
pipelines, the order of execution

00:09.180 --> 00:10.739
and conditional execution.

00:12.020 --> 00:13.020
Pipelines.

00:13.750 --> 00:15.699
So pipelines are what allows a

00:15.700 --> 00:17.349
multiple instructions to be in

00:17.350 --> 00:19.239
different stages of processing

00:19.240 --> 00:20.429
all at the same time,

00:21.190 --> 00:22.989
and this is important because things

00:22.990 --> 00:24.729
like fetching and storing are going

00:24.730 --> 00:25.860
to be much slower.

00:26.380 --> 00:28.209
So RAM is several

00:28.210 --> 00:29.709
orders of magnitude slower than the

00:29.710 --> 00:31.179
processor and the hard drive in

00:31.180 --> 00:32.529
network or even slower than that.

00:33.070 --> 00:34.700
So here are some example times.

00:34.720 --> 00:36.337
So, for example, a register takes

00:36.430 --> 00:38.559
point four nanoseconds to access,

00:39.100 --> 00:40.374
whereas RAM takes over 100

00:40.900 --> 00:41.900
nanoseconds.

00:42.250 --> 00:44.260
So we're going to have well over 100

00:44.440 --> 00:46.899
clock cycles before RAM

00:46.900 --> 00:48.009
is going to be able to access

00:48.010 --> 00:48.969
something.

00:48.970 --> 00:50.259
And then even if you pick some of

00:50.290 --> 00:51.417
the fastest SSD and Vme

00:52.930 --> 00:55.029
SSD, those are twenty

00:55.030 --> 00:56.659
five thousand nanoseconds.

00:56.680 --> 00:58.539
And so, again, orders of

00:58.540 --> 00:59.949
magnitude difference in all of

00:59.950 --> 01:00.909
these.

01:00.910 --> 01:02.799
And so pipelines help us to

01:02.830 --> 01:04.989
optimize our processors.

01:06.660 --> 01:07.889
And you can see that this is

01:07.890 --> 01:09.659
something that we've had processors

01:09.660 --> 01:11.029
have done for quite some time,

01:11.520 --> 01:13.229
so, for example, with Penny Amphoras

01:13.230 --> 01:14.639
and Penny LMDs, the number of

01:14.640 --> 01:16.180
pipeline stages they have are

01:16.200 --> 01:17.200
between 20

01:18.060 --> 01:19.619
and 30. And so it's something that

01:19.620 --> 01:21.569
processors have been doing for quite

01:21.570 --> 01:22.570
some time.

01:23.550 --> 01:25.199
So here's what happens when we have

01:25.200 --> 01:26.879
sort of normal execution and we

01:26.880 --> 01:27.900
don't have a pipeline.

01:28.110 --> 01:29.384
So we can kind of see here

01:30.000 --> 01:31.199
in the diagram we fetch an

01:31.200 --> 01:32.915
instruction, then the next cycle we

01:33.060 --> 01:34.530
decode it, then we execute it,

01:34.980 --> 01:36.120
and then we write the data.

01:36.600 --> 01:38.219
Then we fetch our next instruction,

01:38.220 --> 01:40.139
decode, execute and

01:40.140 --> 01:42.209
then write back either data

01:42.210 --> 01:43.210
or Flagg's.

01:44.300 --> 01:45.770
And so when we have something like

01:46.280 --> 01:48.229
pipelining, we can see here that

01:48.590 --> 01:49.639
while we're fetching one

01:49.640 --> 01:51.469
instruction, we do that

01:51.470 --> 01:52.909
and then we will decode that

01:52.910 --> 01:54.409
instruction at the same time, will

01:54.410 --> 01:55.849
fetch the next instruction.

01:56.540 --> 01:58.069
And then while we are fetching this

01:58.070 --> 02:00.019
instruction, we're decoding

02:00.050 --> 02:01.519
this instruction. We're executing

02:01.520 --> 02:02.520
this one.

02:02.600 --> 02:04.489
And so, as we can see, if we

02:04.490 --> 02:05.960
look at this eight nanoseconds

02:06.170 --> 02:07.700
versus eight nanoseconds,

02:08.180 --> 02:09.319
we can see that here.

02:09.320 --> 02:11.089
We got two instructions done.

02:11.360 --> 02:12.977
And here we ended up getting four

02:13.220 --> 02:14.509
or five, depending on if you're at

02:14.510 --> 02:16.078
eight, eight nanoseconds or nine

02:16.460 --> 02:17.460
nanoseconds.

02:18.140 --> 02:19.430
And so we can see that

02:20.570 --> 02:22.069
there is a benefit to that where

02:22.070 --> 02:23.589
we're getting more instructions

02:23.720 --> 02:25.043
executed in the same amount

02:25.580 --> 02:27.350
of time if we add pipelines.

02:29.630 --> 02:31.399
And so the thing with branching

02:31.670 --> 02:33.229
or jumping is the case.

02:33.350 --> 02:34.400
We may have

02:35.240 --> 02:37.279
the this will cause the processor

02:37.280 --> 02:38.569
to flush its pipeline.

02:38.570 --> 02:40.520
So as it's going, if this

02:41.180 --> 02:43.069
this instruction does

02:43.070 --> 02:44.197
a branch, then the next

02:44.990 --> 02:46.069
instruction or all these

02:46.070 --> 02:47.638
instructions in the pipeline are

02:47.960 --> 02:49.489
going to have to be gotten rid of

02:49.490 --> 02:51.180
because they are not correct.

02:52.310 --> 02:53.809
So what we have is we have what

02:53.810 --> 02:55.159
modern processors do and they

02:55.160 --> 02:56.679
speculate about which branch is

02:56.750 --> 02:58.639
going to be taken and then they'll

02:58.640 --> 02:59.640
actually

03:00.530 --> 03:01.909
change the order of execution

03:01.910 --> 03:02.910
additionally.

03:03.030 --> 03:04.309
So there are a couple of things that

03:04.310 --> 03:05.370
our processor is doing.

03:06.140 --> 03:07.909
So for speculative execution,

03:07.910 --> 03:09.229
basically we have two different

03:09.230 --> 03:10.230
types.

03:10.280 --> 03:11.989
We have the Eger where we basically

03:11.990 --> 03:13.607
try and do both branches and then

03:13.880 --> 03:15.644
whichever one is wrong, then we undo

03:15.860 --> 03:17.599
all of those changes or don't commit

03:17.600 --> 03:18.600
those.

03:18.830 --> 03:20.300
And then we also have predictive

03:20.600 --> 03:22.819
predictive is basically

03:22.820 --> 03:24.139
the processor has a

03:24.800 --> 03:26.466
processor within it that is trying

03:26.870 --> 03:28.309
to predict which branch has taken

03:28.310 --> 03:29.310
the most often.

03:29.540 --> 03:31.279
And then it will go ahead and

03:31.610 --> 03:32.688
do that prediction and

03:33.650 --> 03:35.569
guess and then if it's wrong,

03:35.570 --> 03:36.619
it has to backtrack.

03:36.620 --> 03:38.449
But if it's right, then it

03:38.450 --> 03:40.879
just goes through the pipeline and

03:40.880 --> 03:42.319
it speeds up our execution

03:44.270 --> 03:45.599
out of order execution.

03:46.460 --> 03:48.019
So one of the things the processors

03:48.020 --> 03:49.098
also do is if we think

03:49.970 --> 03:51.439
about just in order processor

03:51.440 --> 03:52.849
refection instruction, if it's

03:52.850 --> 03:54.409
available or if it's in

03:55.340 --> 03:57.259
on the CPU, then we continue.

03:57.260 --> 03:59.240
Otherwise, we basically have to wait

03:59.720 --> 04:00.994
until we can retrieve that

04:01.580 --> 04:02.749
data from RAM.

04:02.970 --> 04:04.587
And so this fetch can take a long

04:05.120 --> 04:06.949
time and then we go

04:06.950 --> 04:08.419
ahead and execute it and then store

04:08.420 --> 04:09.420
the results.

04:10.070 --> 04:11.589
But what happens is with modern

04:11.810 --> 04:13.759
processors, what they do

04:13.760 --> 04:15.379
is they determine which instructions

04:15.380 --> 04:16.769
depend on which other ones.

04:16.790 --> 04:18.015
And so if they keep track

04:18.680 --> 04:20.239
of things like what instructions

04:20.240 --> 04:21.857
it's already fetched, what stores

04:21.920 --> 04:23.929
it's done, the results of each

04:23.930 --> 04:25.759
instruction and then dependencies

04:26.210 --> 04:27.949
as to which instruction is dependent

04:27.950 --> 04:29.929
on another one, then basically

04:29.930 --> 04:32.150
we can do out of order execution.

04:32.630 --> 04:33.679
And so without out of order

04:33.680 --> 04:35.059
execution, we go ahead and fetch an

04:35.060 --> 04:36.060
instruction.

04:36.110 --> 04:37.678
We add it to the queue of things

04:37.760 --> 04:39.529
that can be executed

04:40.490 --> 04:42.470
and then you check the dependencies.

04:42.650 --> 04:44.779
And so if one register depends

04:44.780 --> 04:46.369
on another one, then that

04:46.370 --> 04:47.599
instruction is going to have to

04:47.600 --> 04:48.800
wait. But if two

04:49.700 --> 04:51.319
instructions are independent of each

04:51.320 --> 04:52.789
other, then basically you can do

04:52.790 --> 04:54.689
either one and either either order.

04:55.580 --> 04:56.929
And so if you are waiting on

04:56.930 --> 04:58.399
dependencies when those are finally

04:58.400 --> 04:59.968
met or when all the instructions

05:00.470 --> 05:02.000
that precede this instruction

05:02.510 --> 05:04.339
that it requires of met, then we

05:04.340 --> 05:05.420
go ahead and continue.

05:06.470 --> 05:07.470
And then we can

05:08.420 --> 05:10.340
execute instore the instructions.

05:11.510 --> 05:13.010
So conditional instructions.

05:14.480 --> 05:15.829
So with conditional instructions,

05:15.830 --> 05:17.269
they basically have the ability to

05:17.270 --> 05:18.889
execute an instruction based on

05:18.890 --> 05:20.809
flags, so either do the

05:20.810 --> 05:22.579
instruction or not, depending on

05:22.580 --> 05:23.689
which flags are set,

05:24.410 --> 05:25.831
so often we do something like

05:25.910 --> 05:27.625
compare and then we might move some

05:27.740 --> 05:29.689
data depending

05:29.690 --> 05:31.460
on the result of that comparison.

05:31.620 --> 05:33.335
And we've done this using jumps, so

05:33.620 --> 05:34.819
compares and jumps.

05:35.510 --> 05:37.579
But basically we are able to

05:37.580 --> 05:39.109
reduce branching and increase

05:39.110 --> 05:40.579
efficiency. If we can

05:40.940 --> 05:42.709
go ahead and do this without having

05:42.710 --> 05:43.710
to do

05:44.630 --> 05:46.296
jumps, which again are going to be

05:46.400 --> 05:48.379
inefficient as far as our processor.

05:51.570 --> 05:52.720
So a conditional move.

05:53.220 --> 05:54.569
So this is going to check the state

05:54.570 --> 05:55.795
of the E flags or the set

05:56.550 --> 05:57.539
of flags that we have on the

05:57.540 --> 05:58.716
processor, and it's only

05:59.550 --> 06:00.579
going to do the move.

06:00.630 --> 06:02.549
So the C part stands for conditional

06:02.550 --> 06:03.959
and then move and then we have the

06:03.960 --> 06:05.939
condition and so it'll only

06:05.940 --> 06:07.350
move it. The flag is set.

06:07.650 --> 06:09.360
And again, we have the typical

06:09.690 --> 06:11.307
we have our mnemonic, we have the

06:11.310 --> 06:12.779
destination, we have the source.

06:13.290 --> 06:14.711
And then this key part is the

06:14.910 --> 06:15.910
condition.

06:16.050 --> 06:18.419
So here are a couple of examples.

06:18.420 --> 06:20.339
There's a whole list here, but we

06:20.340 --> 06:21.957
have conditional move above, some

06:22.350 --> 06:23.279
move if above.

06:23.280 --> 06:24.358
So if we did a compare

06:25.740 --> 06:27.720
and then the

06:27.990 --> 06:29.558
part on the left is greater than

06:29.910 --> 06:31.674
the part on the right, then we'll go

06:31.800 --> 06:33.479
ahead and actually do the move from

06:33.480 --> 06:34.709
the source to the destination.

06:34.710 --> 06:36.209
Otherwise we won't actually do that.

06:36.900 --> 06:38.309
Or you can do something like move

06:38.310 --> 06:39.633
greater than or equal to or

06:40.230 --> 06:41.455
move less than again, any

06:42.270 --> 06:43.936
of these will only get executed if

06:43.980 --> 06:45.809
the correct flag is set for

06:45.810 --> 06:47.069
that condition, just like we did

06:47.070 --> 06:48.070
with jumps.

06:49.790 --> 06:51.211
So here are some examples, so

06:51.740 --> 06:53.860
we're comparing yak's and SBX

06:53.870 --> 06:55.489
and then we can say conditional move

06:55.490 --> 06:57.209
above EDC.

06:58.010 --> 06:59.676
And so we can see that if this one

06:59.930 --> 07:01.519
is greater than this one, then we're

07:01.520 --> 07:03.500
going to copy its value into ITCs.

07:04.040 --> 07:05.029
And then you can see in this

07:05.030 --> 07:06.879
example, we're doing vice versa.

07:06.890 --> 07:08.749
So we have above and then below are

07:08.750 --> 07:09.750
equal.

07:09.980 --> 07:11.419
So then if this one is below or

07:11.420 --> 07:13.339
equal, then we copy SBX into

07:13.340 --> 07:14.712
IDEX and that's only if that

07:15.290 --> 07:16.830
is is true.

07:17.390 --> 07:19.309
So with these set

07:19.310 --> 07:21.709
of three instructions, this compare

07:21.710 --> 07:23.150
and then these two moves, one of

07:23.180 --> 07:24.559
these moves will get executed and

07:24.560 --> 07:25.659
the other one will not.

07:26.390 --> 07:27.859
And then here's another compare and

07:27.860 --> 07:29.419
then maybe a below are equal.

07:29.870 --> 07:31.309
So those are some examples of

07:31.310 --> 07:32.310
conditional moves.

07:33.640 --> 07:35.810
So today, we talked about pipelines,

07:35.830 --> 07:37.349
the order of execution and then

07:37.660 --> 07:38.919
conditional execution.

07:40.330 --> 07:41.679
Looking forward, we're going to take

07:41.680 --> 07:43.346
our max of three numbers and we're

07:43.420 --> 07:44.469
going to go ahead and use

07:44.470 --> 07:46.059
conditional execution for that.

07:47.390 --> 07:49.160
So here's our quiz wire, but

07:49.370 --> 07:51.019
jumps bad for processers.

07:53.630 --> 07:55.189
Because they cause a pipeline to be

07:55.190 --> 07:57.110
flushed, which will take more time.

07:58.730 --> 08:00.151
How do you copy data based on

08:00.230 --> 08:01.230
Flagg's?

08:03.710 --> 08:05.629
Use conditional instructions, for

08:05.630 --> 08:06.920
example, the seed move

08:07.820 --> 08:08.820
and then some condition.

08:10.190 --> 08:11.479
If you have questions, you can

08:11.480 --> 08:13.459
contact me at Miller, MJ,

08:13.460 --> 08:15.019
at you and Keda, Edu, and you can

08:15.020 --> 08:16.880
find me on Twitter at now has 30.

