WEBVTT

00:01.660 --> 00:03.189
Hello, this is Dr. Miller, and this

00:03.190 --> 00:04.689
is Episode eleven point four of

00:04.690 --> 00:05.690
Assembly

00:06.700 --> 00:07.749
today we're going to talk about

00:07.750 --> 00:09.789
branching and using an F

00:09.790 --> 00:11.830
or a while and then shifting.

00:13.030 --> 00:14.030
So branching,

00:15.100 --> 00:17.029
so branching is the branch

00:17.030 --> 00:18.880
construction, it starts with a B

00:19.390 --> 00:21.056
and so you can do a regular branch

00:21.430 --> 00:23.399
or just a jump to an address,

00:23.680 --> 00:25.090
you can do a branch with link.

00:25.780 --> 00:27.039
So that will save the link,

00:27.640 --> 00:28.989
the address of the next location

00:28.990 --> 00:30.429
you're going to go to into the link

00:30.430 --> 00:31.430
register.

00:31.720 --> 00:33.249
And then we also have the branch in

00:33.250 --> 00:34.916
exchange. So it exchanges the link

00:35.140 --> 00:36.729
register in the program counter.

00:37.420 --> 00:39.037
So that will allow you to go back

00:39.340 --> 00:40.420
where you came from.

00:42.570 --> 00:44.219
We also have conditional branches,

00:44.220 --> 00:45.899
just like we had conditional jumps,

00:47.070 --> 00:48.599
and so the ones you'll probably most

00:48.600 --> 00:50.310
often use are equal,

00:50.460 --> 00:52.170
not equal, greater than

00:52.290 --> 00:53.909
greater than or equal to

00:54.420 --> 00:56.579
less than or equal to these

00:56.580 --> 00:58.099
over here on the left hand side

00:58.560 --> 00:59.789
are probably the ones that are most

00:59.790 --> 01:01.409
often used. But there's also

01:02.280 --> 01:04.469
ones, for example, minus or

01:04.470 --> 01:05.470
plus

01:06.330 --> 01:07.380
that we can use.

01:08.430 --> 01:10.409
And so you just have to choose

01:10.650 --> 01:12.169
based on your application, what

01:12.510 --> 01:13.709
type of conditional branch you want

01:13.710 --> 01:14.710
to use.

01:16.160 --> 01:17.299
So here's an example.

01:17.450 --> 01:19.339
So these are the

01:19.340 --> 01:20.689
strings that we are defined at the

01:20.690 --> 01:22.258
top, so we have A and we have B,

01:22.880 --> 01:24.739
so A says not seven and B

01:24.740 --> 01:26.719
says seven exclamation point,

01:27.740 --> 01:28.740
followed by a new line.

01:29.660 --> 01:31.819
And so we can see that we are

01:32.270 --> 01:34.120
calling or branching with

01:34.130 --> 01:35.720
a link, the reed int

01:36.680 --> 01:37.680
function.

01:37.880 --> 01:38.909
And then we are doing

01:39.920 --> 01:41.870
a we do a move

01:41.990 --> 01:43.969
of the number seven into our one and

01:43.970 --> 01:45.244
then we compare our one to

01:45.830 --> 01:46.830
R zero.

01:47.780 --> 01:49.446
And so if they are equal, it jumps

01:49.760 --> 01:51.889
down here to the seven

01:51.890 --> 01:53.089
where it prints off the string

01:53.090 --> 01:54.090
seven.

01:54.410 --> 01:56.539
And then if they're not equal

01:56.540 --> 01:58.206
or any other case, it'll jump over

01:58.280 --> 01:59.420
here to the not seven.

02:00.380 --> 02:01.948
And so here this is implementing

02:02.330 --> 02:03.739
a nice if else.

02:03.920 --> 02:05.390
So if it's seven, do this code

02:05.870 --> 02:06.739
right here.

02:06.740 --> 02:08.779
If it's not seven, do this code down

02:08.780 --> 02:09.780
here.

02:09.889 --> 02:12.169
And then as we've seen before

02:12.890 --> 02:14.569
at the end. Right, we do a branch to

02:14.570 --> 02:16.249
the end for each one of these.

02:16.670 --> 02:17.830
This is a little redundant, right?

02:17.870 --> 02:19.438
Branch to end where end is right

02:19.490 --> 02:20.490
here.

02:20.750 --> 02:22.669
So this isn't necessarily needed,

02:23.360 --> 02:24.589
but it was added in there.

02:24.590 --> 02:25.962
Just for clarity, if you had

02:26.330 --> 02:28.045
additional code that you might want

02:28.190 --> 02:29.449
to do or an additional

02:30.350 --> 02:31.918
comparison, this will give you a

02:32.120 --> 02:33.639
good template in order to start

02:33.650 --> 02:34.650
that.

02:37.110 --> 02:38.969
And then we can have a while loop,

02:39.160 --> 02:40.979
right, so in arm, we don't

02:40.980 --> 02:42.690
have the loop come in like we did in

02:42.930 --> 02:44.729
86 and so

02:44.970 --> 02:46.409
we can see here again, we're reading

02:46.410 --> 02:47.410
a number.

02:47.710 --> 02:49.349
And in this example, we have a Soman

02:49.350 --> 02:50.722
encounter. So the sum starts

02:51.210 --> 02:53.069
off at zero, the

02:53.070 --> 02:54.779
counter starts at the number one

02:54.990 --> 02:56.939
and then the counter, if we remember

02:56.940 --> 02:58.129
branch with the ingredient.

02:58.770 --> 03:00.659
So that's going to read in a number

03:00.660 --> 03:01.660
from the user and

03:02.550 --> 03:03.922
then that'll get stored in R

03:04.440 --> 03:06.360
zero. And so this is where

03:06.390 --> 03:07.449
we're going to end.

03:07.470 --> 03:08.969
So while

03:09.690 --> 03:11.699
R2 is less than R zero,

03:11.940 --> 03:13.169
we're going to

03:14.340 --> 03:16.259
do this code here.

03:16.610 --> 03:18.479
Right. So while our R zero

03:18.480 --> 03:19.480
is bigger.

03:20.640 --> 03:22.109
So when it's all done, then we'll

03:22.110 --> 03:24.210
exit. And so here we go ahead and we

03:24.750 --> 03:25.739
do an addition.

03:25.740 --> 03:27.259
So we're adding our one and our

03:27.630 --> 03:29.345
two together and then we're storing

03:29.520 --> 03:30.892
it in our one and then we're

03:31.470 --> 03:32.699
incrementing R2.

03:32.700 --> 03:34.120
So our two is our counter.

03:34.950 --> 03:36.299
And then when we get down here, we

03:36.300 --> 03:37.669
come back up to the loop and then

03:37.670 --> 03:39.569
and do a comparison of R zero

03:39.570 --> 03:40.570
and R two.

03:41.280 --> 03:42.299
And then when we're all done, our

03:42.300 --> 03:43.990
sum was in R one.

03:44.220 --> 03:46.139
And so we just copy that into R zero

03:46.140 --> 03:47.930
so that we can use the print end

03:47.940 --> 03:49.470
function in order to print it off.

03:50.250 --> 03:51.449
So this is going to go multiple

03:51.450 --> 03:53.018
times until the condition is met

03:53.100 --> 03:54.749
such that it'll exit.

03:54.790 --> 03:56.459
And so that's a implementation of a

03:56.460 --> 03:57.460
while loop.

03:59.190 --> 04:00.190
All right, shifting.

04:02.340 --> 04:04.259
So we have a variety

04:04.260 --> 04:05.260
of different shifts.

04:05.280 --> 04:07.289
We, again, have seen a lot of this

04:07.290 --> 04:08.339
in 86,

04:09.270 --> 04:10.439
but we have sort of a different

04:10.440 --> 04:11.309
notation.

04:11.310 --> 04:13.349
Again, the constant

04:14.520 --> 04:16.550
so we have our destination register.

04:17.160 --> 04:18.898
We have the one holding the operand

04:18.899 --> 04:20.339
or the thing that you want to shift.

04:20.820 --> 04:22.709
And then we have possibly either

04:22.710 --> 04:24.470
a source or a constant.

04:24.480 --> 04:26.519
And so we can shift by logical

04:26.520 --> 04:28.610
shift left by a

04:28.920 --> 04:30.689
another register or a constant.

04:31.860 --> 04:32.938
And we can do a shift.

04:33.450 --> 04:35.399
Right. We can do with arithmetic

04:35.400 --> 04:37.379
shift. So we make sure that we keep

04:37.380 --> 04:38.380
the sign.

04:38.430 --> 04:40.230
So no sign numbers, the

04:40.470 --> 04:41.891
leftmost bit as a one if it's

04:42.390 --> 04:43.390
a negative number.

04:43.710 --> 04:45.339
And so if you do an arithmetic shift

04:45.340 --> 04:47.006
to make sure you keep that so that

04:47.370 --> 04:48.749
the number doesn't go from being

04:48.750 --> 04:50.279
negative to being positive.

04:52.140 --> 04:53.730
And then we have the rotate, so

04:54.090 --> 04:55.462
rotate, right, we can rotate

04:56.040 --> 04:58.019
either by a register or by

04:58.020 --> 04:59.249
a particular amount.

05:00.900 --> 05:02.339
And then we have the barrel shifter,

05:02.940 --> 05:04.606
so the barrel shifter allows us to

05:04.740 --> 05:05.965
shift a particular number

05:06.630 --> 05:07.630
of bits

05:08.580 --> 05:09.990
in another operation.

05:11.010 --> 05:12.389
And so generally with the barrel

05:12.390 --> 05:14.130
shifter, we can either do a shift

05:14.580 --> 05:16.339
or we can do it or rotate.

05:17.010 --> 05:18.239
And again, these are just moving

05:18.240 --> 05:19.769
some bits around so that they

05:20.730 --> 05:21.730
don't change.

05:22.080 --> 05:23.599
And this is basically such that

05:24.000 --> 05:25.889
we can either multiply or divide by

05:26.340 --> 05:27.516
powers of two and we can

05:28.260 --> 05:30.480
do some rotation of bits.

05:31.050 --> 05:33.029
And then if you look back, shifters

05:33.030 --> 05:34.649
are actually used for floating point

05:34.650 --> 05:36.509
arithmetic units and

05:36.510 --> 05:37.510
so on.

05:38.100 --> 05:39.509
The back end floating point might

05:39.510 --> 05:41.274
use this in order to align different

05:41.490 --> 05:42.490
numbers.

05:43.820 --> 05:45.379
So here's an example of a barrel

05:45.380 --> 05:46.605
shifter, so, for example,

05:47.630 --> 05:48.630
we can

05:49.910 --> 05:51.499
move a couple of constants here into

05:51.500 --> 05:52.500
some registers.

05:53.000 --> 05:54.617
And so Barasch after allows us to

05:54.920 --> 05:56.341
say, I'm going to take R zero

05:57.290 --> 05:59.179
and I'm going to do a left shift by

05:59.180 --> 06:00.739
one, and then I'm going to store

06:00.740 --> 06:02.060
that result in R one.

06:03.090 --> 06:04.259
Right, and that's basically

06:04.260 --> 06:05.579
multiplying it by two

06:06.300 --> 06:07.868
or I can take our zero, multiply

06:08.220 --> 06:10.259
it by or shift it by

06:10.470 --> 06:12.185
two, which is due to the two, which

06:12.390 --> 06:14.729
is four, so I can multiply

06:14.730 --> 06:16.679
our zero by four and

06:16.680 --> 06:18.240
then saw the result in our one.

06:19.430 --> 06:20.959
Here's an example where you can do

06:20.960 --> 06:22.136
it with another register

06:22.850 --> 06:24.709
so I can do a left shift of our

06:24.710 --> 06:26.082
nine so we can see our nine,

06:26.540 --> 06:27.980
I moved the number 10 in it.

06:28.550 --> 06:30.589
So I take our zero

06:30.740 --> 06:32.689
and I multiply it by

06:33.200 --> 06:34.729
two to the ten or a thousand twenty

06:34.730 --> 06:36.829
four and then store that result

06:36.830 --> 06:38.299
in R1. So we can see we're doing

06:38.300 --> 06:39.829
both a shift and a move

06:40.490 --> 06:41.779
or we don't have to just do move.

06:41.780 --> 06:42.780
We can do add.

06:42.800 --> 06:44.630
So here we're adding them together.

06:45.140 --> 06:47.420
So our zero plus our two

06:47.450 --> 06:49.429
are two is then left shifted by

06:49.430 --> 06:51.379
three, so two

06:51.380 --> 06:52.609
to the three which is eight.

06:53.120 --> 06:54.120
So that gets

06:54.950 --> 06:56.028
shifted and then added

06:56.810 --> 06:58.040
to our zero.

06:58.070 --> 06:59.638
The result is then stored in our

06:59.690 --> 07:00.690
one.

07:01.200 --> 07:03.079
And here we can do a shift.

07:03.080 --> 07:03.979
Right.

07:03.980 --> 07:05.149
And we can do a subtract.

07:05.150 --> 07:06.179
So R zero minus R two

07:07.010 --> 07:08.929
shifted to the right or divided by

07:09.050 --> 07:10.050
16.

07:11.680 --> 07:12.729
Additionally, we can do some

07:12.730 --> 07:14.829
rotations, so here's an example

07:14.830 --> 07:16.329
of how to load a thirty two bit

07:16.330 --> 07:18.399
constant, we can use a load register

07:19.270 --> 07:20.529
to load a constant in there.

07:20.530 --> 07:22.370
So we have a BBQ

07:23.590 --> 07:25.690
and so we can take that number,

07:25.900 --> 07:27.879
which is in our zero,

07:27.880 --> 07:29.448
and then we can rotate it to the

07:29.710 --> 07:32.019
right by for four bits

07:32.440 --> 07:33.879
and saw that in our one.

07:33.880 --> 07:35.529
And so then the result would be

07:36.040 --> 07:37.989
d.E.A BBK

07:38.190 --> 07:39.190
D. Right.

07:39.210 --> 07:41.199
So all of these bits got

07:41.200 --> 07:43.329
shifted over and then the D wrapped

07:43.330 --> 07:44.330
around.

07:46.040 --> 07:48.049
And then we can also do it for the.

07:50.440 --> 07:52.119
We can rotate to the right eight,

07:52.300 --> 07:53.859
so rotate eight bits, which would be

07:53.860 --> 07:55.690
to hex digits and so the.

07:56.520 --> 07:58.369
The values getting shifted over.

08:02.550 --> 08:04.319
So today, we talked about branching,

08:04.710 --> 08:06.809
I'm looking at a statement

08:06.810 --> 08:08.159
and a wild statement, and then we

08:08.160 --> 08:10.079
looked at shifting and

08:10.080 --> 08:11.080
Barel shifting.

08:13.260 --> 08:14.975
So in future lectures, we will give

08:15.180 --> 08:16.720
an example of the various schifter

08:16.770 --> 08:17.930
and we'll do that in code.

08:18.510 --> 08:19.919
We'll talk about debugging and

08:19.920 --> 08:22.109
memory offsets and then ALM indexing

08:22.110 --> 08:23.110
for RES.

08:24.350 --> 08:26.179
So here's our quiz, so

08:26.180 --> 08:27.699
if we compare R zero and R one,

08:28.100 --> 08:29.539
how would we branch if the first

08:29.540 --> 08:31.039
register is less than the second

08:31.040 --> 08:32.040
register?

08:35.380 --> 08:37.209
So we can say branch less than

08:37.240 --> 08:39.069
other, and this

08:39.070 --> 08:40.178
would be some label that we would

08:40.179 --> 08:41.179
branch to.

08:43.100 --> 08:44.509
If you have questions, you can email

08:44.510 --> 08:46.730
me Miller at you and KSTU

08:46.850 --> 08:48.349
or find me on Twitter at Millhone

08:48.350 --> 08:48.799
Sturdy.

