WEBVTT

00:01.350 --> 00:02.969
Hello, this is Dr. Miller, and this

00:02.970 --> 00:04.342
is Episode four point one of

00:04.530 --> 00:05.530
Assembly.

00:07.480 --> 00:08.559
Today, we're going to talk about

00:08.560 --> 00:10.660
control structures and looping

00:11.650 --> 00:12.789
control structures

00:13.690 --> 00:15.013
so control structures allow

00:15.730 --> 00:17.396
us to create a system that is true

00:17.620 --> 00:19.089
and complete, and that means that we

00:19.090 --> 00:20.920
have the ability to do selection

00:21.610 --> 00:23.409
and both high level and assembly

00:23.410 --> 00:25.719
level language contain the ability

00:25.720 --> 00:27.069
in order to do selection.

00:28.210 --> 00:29.210
So how do we do it?

00:29.560 --> 00:31.269
We use a couple of different

00:31.270 --> 00:32.840
commands. One is compare.

00:33.010 --> 00:34.930
So compare takes register one

00:35.320 --> 00:36.969
and register two, and it does

00:36.970 --> 00:37.970
subtraction on them.

00:38.590 --> 00:40.119
And so we take register one minus

00:40.120 --> 00:41.737
register two and then we go ahead

00:41.770 --> 00:43.119
and set the flags.

00:43.600 --> 00:45.579
So the flags say what happened in

00:45.580 --> 00:47.439
the previous operation that

00:47.440 --> 00:48.409
occurred.

00:48.410 --> 00:50.019
And so if the Kerry flag gets set,

00:50.020 --> 00:51.909
that means it had to carry when it

00:51.910 --> 00:52.910
did subtraction.

00:53.200 --> 00:54.940
The overflow flag means that

00:55.240 --> 00:57.004
we didn't have enough room and so we

00:57.070 --> 00:58.750
had to overflow into another

00:58.780 --> 00:59.780
location.

01:00.250 --> 01:02.170
The zero flag means that

01:02.770 --> 01:04.239
the result was zero.

01:04.250 --> 01:06.014
So register one and register two are

01:06.040 --> 01:07.330
equal in that case.

01:08.230 --> 01:09.909
And then our operands, it doesn't

01:09.910 --> 01:11.469
matter the order, but one of them

01:11.470 --> 01:12.549
has to be a register.

01:13.000 --> 01:14.617
So either this one or this one or

01:14.860 --> 01:16.209
both can be a register.

01:16.480 --> 01:18.097
And the other one can be either a

01:18.400 --> 01:19.929
memory address, which we'll talk

01:19.930 --> 01:21.909
about later, or an immediate

01:21.940 --> 01:23.349
or a hardcoded value.

01:26.260 --> 01:28.209
So if register one and register two

01:28.210 --> 01:29.876
are equal, then the zero flag will

01:29.980 --> 01:31.599
get set and we can use that in order

01:31.600 --> 01:33.069
to tell if two things are equal,

01:33.790 --> 01:35.319
if register one is greater than

01:35.320 --> 01:36.986
register two than the zero flag is

01:37.150 --> 01:39.180
zero and the Kerry flag is zero,

01:40.180 --> 01:41.552
if register one is less than

01:41.920 --> 01:43.586
register two than the zero flag is

01:43.840 --> 01:45.114
zero and the Kerry flag is

01:45.790 --> 01:47.540
one, meaning that I carry

01:47.810 --> 01:48.986
a flag set because Borro

01:49.990 --> 01:50.990
had to occur.

01:54.220 --> 01:55.837
So along with conditionals is the

01:56.200 --> 01:57.940
ability to do jump

01:58.120 --> 02:00.520
jumping around or branching

02:00.550 --> 02:02.379
using those flags that

02:02.380 --> 02:04.269
got set and we have two different

02:04.270 --> 02:05.270
kinds of jumps.

02:05.710 --> 02:07.149
So one is called the unconditional

02:07.150 --> 02:08.460
jump jump location.

02:08.470 --> 02:10.089
So whenever that instruction occurs,

02:10.090 --> 02:11.199
it's just going to go somewhere

02:11.200 --> 02:12.200
else.

02:12.460 --> 02:13.809
And then we have the conditional

02:13.810 --> 02:15.909
jump. So only jump when a flag

02:15.910 --> 02:17.527
is set. So if a flag is set, then

02:17.830 --> 02:19.119
you get a jump on the location.

02:19.120 --> 02:20.619
Otherwise you're going to execute

02:20.620 --> 02:21.999
the next instruction that comes

02:22.000 --> 02:23.439
after the current instruction.

02:24.340 --> 02:25.539
And we've got a bunch of different

02:25.540 --> 02:27.339
ones right in each of these relates

02:27.340 --> 02:28.440
to the flags.

02:28.450 --> 02:30.339
So, for example, jump zero,

02:30.640 --> 02:32.061
jump not zero, jump overflow,

02:32.980 --> 02:34.119
jump, not overflow.

02:34.210 --> 02:35.680
And we got sine carry and then

02:36.100 --> 02:37.100
Perati.

02:39.540 --> 02:41.069
So here's a simple example

02:41.400 --> 02:43.319
that just checks to see what

02:43.320 --> 02:44.692
the user did. So if you look

02:45.210 --> 02:46.649
here, we can see that it says call,

02:46.650 --> 02:48.089
read, end. So that's going to read a

02:48.090 --> 02:49.949
number from the user and

02:49.950 --> 02:51.479
then it's going to do a comparison.

02:51.480 --> 02:53.340
Compare Yak's to ten.

02:53.410 --> 02:55.470
Again, we put an immediate in here

02:55.740 --> 02:56.969
and then we have a register.

02:57.630 --> 02:58.889
And so then the flags are going to

02:58.890 --> 03:00.164
get set. And so zero means

03:00.810 --> 03:02.699
that if I took X and I'm minus

03:02.700 --> 03:04.229
10, right. That they're equal.

03:04.800 --> 03:06.809
If we can see that this jump zero

03:07.020 --> 03:08.343
is going to go down here to

03:08.910 --> 03:10.309
this ten label.

03:10.770 --> 03:12.479
So it says if it's zero, it's going

03:12.480 --> 03:14.279
to jump down here and then execute

03:14.280 --> 03:15.659
this instruction down here.

03:16.410 --> 03:17.782
Otherwise, if it's not zero,

03:18.360 --> 03:20.069
it's going to execute this move into

03:20.340 --> 03:22.349
other and then it'll jump

03:22.350 --> 03:23.490
down here to the print.

03:23.640 --> 03:24.419
Notice this.

03:24.420 --> 03:25.869
This is an unconditional jump,

03:25.950 --> 03:27.589
meaning always do that jump.

03:28.590 --> 03:29.849
And up here we have a couple of

03:29.850 --> 03:30.899
strings that were declared.

03:30.900 --> 03:31.769
So we have 10.

03:31.770 --> 03:33.719
You enter 10 or other

03:33.870 --> 03:35.639
incorrect exclamation point.

03:36.710 --> 03:37.729
We can see here that there's a

03:37.730 --> 03:39.289
tenant here that's a new line

03:39.680 --> 03:41.089
and then a zero, right, in order to

03:41.090 --> 03:42.090
end our string.

03:42.110 --> 03:43.309
And so those are things that we have

03:43.310 --> 03:44.310
to do.

03:44.750 --> 03:46.069
So either way, right.

03:46.160 --> 03:47.569
It's going to move either either

03:48.350 --> 03:49.970
the value of other or

03:50.460 --> 03:51.529
the value of ten.

03:51.530 --> 03:52.999
And then they both come down here

03:53.000 --> 03:54.650
into do the print string.

03:55.400 --> 03:57.639
So something to do, move tenant X

03:57.680 --> 03:58.699
and then this is the next

03:58.700 --> 04:00.150
instruction after that.

04:01.130 --> 04:02.796
And so this will allow the user to

04:02.900 --> 04:04.339
enter a number. It'll check if it's

04:04.340 --> 04:06.169
ten or not, and then it'll print

04:06.170 --> 04:07.885
off the result, either you enter 10

04:08.210 --> 04:09.210
or incorrect.

04:10.010 --> 04:12.139
So I think you should go ahead and

04:12.170 --> 04:13.738
use your virtual machine or your

04:14.120 --> 04:15.688
Linux machine and enter the code

04:16.310 --> 04:17.870
and see if you can get it to run.

04:20.529 --> 04:21.529
Looping,

04:22.990 --> 04:24.264
so looping allows us to do

04:24.880 --> 04:26.559
something over and over again, and

04:26.560 --> 04:27.999
there's a specific command called

04:28.000 --> 04:29.274
Loop and so we can loop to

04:30.040 --> 04:31.265
some label which looks at

04:31.990 --> 04:32.990
X, right.

04:33.610 --> 04:35.529
And it jumps if it's not

04:35.530 --> 04:36.519
equal to zero.

04:36.520 --> 04:38.559
So if X is not zero, then it goes

04:38.560 --> 04:39.981
ahead and does a jump, but at

04:40.240 --> 04:42.020
decrements X every time.

04:42.040 --> 04:43.480
So this allows you to

04:43.960 --> 04:45.729
go through multiple times

04:46.300 --> 04:47.949
and in the next lecture will look at

04:47.950 --> 04:49.810
an example using the loop or

04:50.170 --> 04:51.620
one of these two commands.

04:52.240 --> 04:54.189
So we also have loop equal

04:54.190 --> 04:55.190
or loop zero.

04:55.210 --> 04:56.709
These are the same commands, right,

04:56.710 --> 04:58.474
because we have the zero flag and if

04:58.570 --> 05:00.285
it's zero, then that's what we use.

05:00.760 --> 05:02.589
Right. So jumps of X is not as

05:02.650 --> 05:03.875
equal to zero and as zero

05:04.600 --> 05:06.489
flag is one or loop not

05:06.490 --> 05:08.050
equal or loop not zero.

05:11.040 --> 05:12.629
And so here's an example using the

05:12.630 --> 05:13.659
loop command, so here

05:14.550 --> 05:15.550
we have the sort of

05:16.410 --> 05:18.125
formulaic version of that, and then

05:18.330 --> 05:19.949
we could translate that directly

05:19.950 --> 05:21.810
into assembly. So we're moving into

05:22.050 --> 05:23.471
the number zero, we're moving

05:23.970 --> 05:25.800
into X, the number ten,

05:26.190 --> 05:28.529
and then we're going to add X

05:28.530 --> 05:29.309
and X.

05:29.310 --> 05:30.720
So X is ten.

05:31.050 --> 05:32.819
Add that to X, which is zero.

05:32.850 --> 05:34.560
So now X has ten

05:34.920 --> 05:36.089
and then it's going to do loop.

05:36.100 --> 05:38.099
So it's going to check is X

05:38.100 --> 05:39.100
zero, right.

05:39.120 --> 05:40.799
It does a decrement on it checks to

05:40.800 --> 05:41.800
see if at zero.

05:41.940 --> 05:43.679
If it's not it jumps back up here.

05:44.070 --> 05:45.491
Right. Does the add then does

05:45.990 --> 05:46.899
the loop again.

05:46.900 --> 05:48.664
So we do this over and over and over

05:48.810 --> 05:50.669
again until X

05:50.670 --> 05:52.238
is going to be ten and then nine

05:52.860 --> 05:54.624
and then eight and seven, six, five,

05:54.660 --> 05:55.983
four, three, two, one zero.

05:57.090 --> 05:58.380
Then it will exit the loop.

05:59.670 --> 06:01.320
So that's an example of how

06:01.620 --> 06:03.569
the loop command can be used.

06:05.070 --> 06:06.569
So we're going to do a real example.

06:06.570 --> 06:07.709
So we're going to read a number from

06:07.710 --> 06:09.229
the user and kind of on similar

06:09.480 --> 06:10.589
lines, we're going to print all of

06:10.590 --> 06:12.520
the numbers up to that number.

06:13.110 --> 06:14.999
So if the user enters seven, we're

06:15.000 --> 06:15.869
going to print one, two, three,

06:15.870 --> 06:16.919
four, five, six, seven.

06:16.920 --> 06:18.537
And I'll put new lines in just so

06:18.750 --> 06:20.610
that we can kind of see

06:20.850 --> 06:21.850
how it works.

06:24.540 --> 06:26.206
All right, so I've already created

06:26.700 --> 06:27.700
my project.

06:33.220 --> 06:34.209
So the first thing that we're going

06:34.210 --> 06:35.619
to want to do is read the number

06:35.620 --> 06:36.620
from the user.

06:40.660 --> 06:41.934
Now, that number is stored

06:42.820 --> 06:44.409
in yak's, and I'd actually like to

06:44.410 --> 06:45.978
save it somewhere else because I

06:46.120 --> 06:48.190
want to use it in my

06:48.640 --> 06:50.949
Compar or Yak's and my comparison

06:51.250 --> 06:52.867
and I want to be able to print it

06:52.870 --> 06:54.039
off. And the only way to print it

06:54.040 --> 06:55.899
off really easily is the print

06:55.900 --> 06:57.879
int, which requires

06:57.880 --> 06:58.899
the value that we're going to print

06:58.900 --> 07:00.009
to be an X.

07:00.020 --> 07:01.899
And so I'm going to copy

07:01.900 --> 07:03.600
this into SBX.

07:04.870 --> 07:06.459
So now I have a copy of my number

07:06.460 --> 07:07.809
and then I'm going to set Yak's to

07:07.810 --> 07:08.810
zero.

07:12.190 --> 07:13.190
OK.

07:14.590 --> 07:15.717
And then in order to do

07:16.540 --> 07:17.618
my comparison or to be

07:18.370 --> 07:19.689
able to decide when I'm done, I'm

07:19.690 --> 07:21.279
going to go ahead and compare

07:22.210 --> 07:24.130
yak's and SBX.

07:25.030 --> 07:26.619
And so if they're equal, then I'm

07:26.620 --> 07:27.620
done. So I will do

07:28.540 --> 07:29.680
jump equal.

07:30.640 --> 07:32.500
Done now done is a label,

07:32.650 --> 07:34.299
which is what we need for jumping.

07:34.300 --> 07:35.800
So I'll go ahead and down here and

07:36.220 --> 07:37.420
put a label called Done

07:38.950 --> 07:40.930
and all a label is is

07:40.960 --> 07:42.639
a string of characters followed by a

07:42.640 --> 07:44.589
colon to say the instructions

07:44.590 --> 07:45.962
after this, starting on this

07:46.480 --> 07:48.279
line are called that label.

07:50.870 --> 07:52.340
All right, so if I'm not done,

07:53.000 --> 07:54.319
then what I want to do is I want to

07:54.320 --> 07:55.949
print the number that's in X,

07:56.150 --> 07:57.150
so call

07:58.010 --> 08:00.050
print int.

08:01.510 --> 08:02.769
And then I'll print a new line.

08:06.160 --> 08:07.909
So I printed type

08:07.990 --> 08:09.939
two extra key there, I printed

08:09.940 --> 08:11.704
my integer, then I printed off a new

08:11.830 --> 08:12.859
line and now what I'd

08:13.720 --> 08:15.189
like to do. So yeah, this is my

08:15.190 --> 08:16.239
number that I'm printing.

08:16.240 --> 08:17.999
So I want to make that one bigger.

08:18.010 --> 08:20.080
Or I can say increment

08:20.770 --> 08:21.770
yak's.

08:23.780 --> 08:25.519
And then what I want to do is I want

08:25.520 --> 08:26.696
to jump back up here and

08:27.410 --> 08:29.000
so I'm going to say jump

08:31.460 --> 08:33.229
and I'll just call it compare

08:34.429 --> 08:35.960
and I'll put a label here.

08:41.169 --> 08:42.835
So that way to jump back up to the

08:43.000 --> 08:44.859
top now. Yeah, this is one bigger

08:44.860 --> 08:46.428
and so it does a comparison of X

08:46.720 --> 08:48.880
and SBX until they are equal.

08:51.880 --> 08:53.510
And go ahead and write and quit.

09:00.120 --> 09:02.070
So if I enter the number seven

09:03.250 --> 09:05.369
imprint's zero through six,

09:06.840 --> 09:08.339
so brings all the numbers up to that

09:08.340 --> 09:09.960
number, but not including that,

09:10.980 --> 09:12.209
you could change it to do it.

09:12.210 --> 09:13.979
A couple of different versions of

09:13.980 --> 09:14.980
this

09:16.500 --> 09:17.500
Ronke.

09:19.240 --> 09:20.769
So, for example, if you wanted to

09:20.770 --> 09:23.320
start it at the number one,

09:24.100 --> 09:25.779
right, we could go ahead and do that

09:27.160 --> 09:29.289
and we can do a couple of different

09:29.290 --> 09:30.279
versions of this.

09:30.280 --> 09:31.869
Right. So I'll let you try and guess

09:31.870 --> 09:32.870
what they are.

09:33.070 --> 09:34.629
You can try some examples.

09:34.630 --> 09:36.129
So, for example, there is

09:36.460 --> 09:37.460
also a jump above or

09:38.320 --> 09:39.888
jump below, jump greater than or

09:40.090 --> 09:42.059
equal to there's a whole bunch.

09:42.520 --> 09:44.539
And so since I'm comparing X

09:44.620 --> 09:46.719
to SBX, I maybe want to see

09:46.720 --> 09:47.829
if they are.

09:48.680 --> 09:50.080
Let's see. I want to see if

09:50.500 --> 09:51.500
I will try one.

09:52.090 --> 09:53.090
See if that works.

09:55.270 --> 09:57.220
So jump if this one is greater than

09:57.550 --> 09:58.550
so, maybe

09:59.800 --> 10:01.659
that one, see

10:01.660 --> 10:02.660
if that works.

10:04.690 --> 10:05.919
And you can always run it.

10:08.240 --> 10:09.919
And it looks like it did right, so

10:09.920 --> 10:12.049
it kept going until the first

10:12.050 --> 10:13.489
the number we were printing was

10:13.490 --> 10:15.349
greater than instead of greater

10:15.350 --> 10:16.950
than or equal to.

10:17.660 --> 10:18.889
And so we're going from the numbers

10:18.890 --> 10:20.359
from one to seven is what we're

10:20.360 --> 10:21.360
printing off.

10:30.650 --> 10:32.089
All right, so in summary, we talked

10:32.090 --> 10:33.462
about control structures and

10:34.010 --> 10:35.676
looping, and we gave some examples

10:35.840 --> 10:37.789
of how to use both the

10:37.790 --> 10:39.620
loop construct and

10:39.920 --> 10:41.439
how to build our own loop using

10:41.900 --> 10:43.850
the compares and the jumps.

10:45.620 --> 10:46.849
Looking forward, we're going to do a

10:46.850 --> 10:48.799
full on loop example using the loop

10:48.800 --> 10:50.599
command and then we'll talk about

10:50.600 --> 10:52.039
rotation and shifting.

10:54.640 --> 10:55.719
If you have questions, you can

10:55.720 --> 10:57.484
contact me at Miller, MJ, at you and

10:57.490 --> 10:58.715
Kada Edu and you can find

10:59.380 --> 11:01.210
me on Twitter at Milhouse 30.

