Hello, this is Dr. Miller, and this
is Episode four point one of
Assembly.
Today, we're going to talk about
control structures and looping
control structures
so control structures allow
us to create a system that is true
and complete, and that means that we
have the ability to do selection
and both high level and assembly
level language contain the ability
in order to do selection.
So how do we do it?
We use a couple of different
commands. One is compare.
So compare takes register one
and register two, and it does
subtraction on them.
And so we take register one minus
register two and then we go ahead
and set the flags.
So the flags say what happened in
the previous operation that
occurred.
And so if the Kerry flag gets set,
that means it had to carry when it
did subtraction.
The overflow flag means that
we didn't have enough room and so we
had to overflow into another
location.
The zero flag means that
the result was zero.
So register one and register two are
equal in that case.
And then our operands, it doesn't
matter the order, but one of them
has to be a register.
So either this one or this one or
both can be a register.
And the other one can be either a
memory address, which we'll talk
about later, or an immediate
or a hardcoded value.
So if register one and register two
are equal, then the zero flag will
get set and we can use that in order
to tell if two things are equal,
if register one is greater than
register two than the zero flag is
zero and the Kerry flag is zero,
if register one is less than
register two than the zero flag is
zero and the Kerry flag is
one, meaning that I carry
a flag set because Borro
had to occur.
So along with conditionals is the
ability to do jump
jumping around or branching
using those flags that
got set and we have two different
kinds of jumps.
So one is called the unconditional
jump jump location.
So whenever that instruction occurs,
it's just going to go somewhere
else.
And then we have the conditional
jump. So only jump when a flag
is set. So if a flag is set, then
you get a jump on the location.
Otherwise you're going to execute
the next instruction that comes
after the current instruction.
And we've got a bunch of different
ones right in each of these relates
to the flags.
So, for example, jump zero,
jump not zero, jump overflow,
jump, not overflow.
And we got sine carry and then
Perati.
So here's a simple example
that just checks to see what
the user did. So if you look
here, we can see that it says call,
read, end. So that's going to read a
number from the user and
then it's going to do a comparison.
Compare Yak's to ten.
Again, we put an immediate in here
and then we have a register.
And so then the flags are going to
get set. And so zero means
that if I took X and I'm minus
10, right. That they're equal.
If we can see that this jump zero
is going to go down here to
this ten label.
So it says if it's zero, it's going
to jump down here and then execute
this instruction down here.
Otherwise, if it's not zero,
it's going to execute this move into
other and then it'll jump
down here to the print.
Notice this.
This is an unconditional jump,
meaning always do that jump.
And up here we have a couple of
strings that were declared.
So we have 10.
You enter 10 or other
incorrect exclamation point.
We can see here that there's a
tenant here that's a new line
and then a zero, right, in order to
end our string.
And so those are things that we have
to do.
So either way, right.
It's going to move either either
the value of other or
the value of ten.
And then they both come down here
into do the print string.
So something to do, move tenant X
and then this is the next
instruction after that.
And so this will allow the user to
enter a number. It'll check if it's
ten or not, and then it'll print
off the result, either you enter 10
or incorrect.
So I think you should go ahead and
use your virtual machine or your
Linux machine and enter the code
and see if you can get it to run.
Looping,
so looping allows us to do
something over and over again, and
there's a specific command called
Loop and so we can loop to
some label which looks at
X, right.
And it jumps if it's not
equal to zero.
So if X is not zero, then it goes
ahead and does a jump, but at
decrements X every time.
So this allows you to
go through multiple times
and in the next lecture will look at
an example using the loop or
one of these two commands.
So we also have loop equal
or loop zero.
These are the same commands, right,
because we have the zero flag and if
it's zero, then that's what we use.
Right. So jumps of X is not as
equal to zero and as zero
flag is one or loop not
equal or loop not zero.
And so here's an example using the
loop command, so here
we have the sort of
formulaic version of that, and then
we could translate that directly
into assembly. So we're moving into
the number zero, we're moving
into X, the number ten,
and then we're going to add X
and X.
So X is ten.
Add that to X, which is zero.
So now X has ten
and then it's going to do loop.
So it's going to check is X
zero, right.
It does a decrement on it checks to
see if at zero.
If it's not it jumps back up here.
Right. Does the add then does
the loop again.
So we do this over and over and over
again until X
is going to be ten and then nine
and then eight and seven, six, five,
four, three, two, one zero.
Then it will exit the loop.
So that's an example of how
the loop command can be used.
So we're going to do a real example.
So we're going to read a number from
the user and kind of on similar
lines, we're going to print all of
the numbers up to that number.
So if the user enters seven, we're
going to print one, two, three,
four, five, six, seven.
And I'll put new lines in just so
that we can kind of see
how it works.
All right, so I've already created
my project.
So the first thing that we're going
to want to do is read the number
from the user.
Now, that number is stored
in yak's, and I'd actually like to
save it somewhere else because I
want to use it in my
Compar or Yak's and my comparison
and I want to be able to print it
off. And the only way to print it
off really easily is the print
int, which requires
the value that we're going to print
to be an X.
And so I'm going to copy
this into SBX.
So now I have a copy of my number
and then I'm going to set Yak's to
zero.
OK.
And then in order to do
my comparison or to be
able to decide when I'm done, I'm
going to go ahead and compare
yak's and SBX.
And so if they're equal, then I'm
done. So I will do
jump equal.
Done now done is a label,
which is what we need for jumping.
So I'll go ahead and down here and
put a label called Done
and all a label is is
a string of characters followed by a
colon to say the instructions
after this, starting on this
line are called that label.
All right, so if I'm not done,
then what I want to do is I want to
print the number that's in X,
so call
print int.
And then I'll print a new line.
So I printed type
two extra key there, I printed
my integer, then I printed off a new
line and now what I'd
like to do. So yeah, this is my
number that I'm printing.
So I want to make that one bigger.
Or I can say increment
yak's.
And then what I want to do is I want
to jump back up here and
so I'm going to say jump
and I'll just call it compare
and I'll put a label here.
So that way to jump back up to the
top now. Yeah, this is one bigger
and so it does a comparison of X
and SBX until they are equal.
And go ahead and write and quit.
So if I enter the number seven
imprint's zero through six,
so brings all the numbers up to that
number, but not including that,
you could change it to do it.
A couple of different versions of
this
Ronke.
So, for example, if you wanted to
start it at the number one,
right, we could go ahead and do that
and we can do a couple of different
versions of this.
Right. So I'll let you try and guess
what they are.
You can try some examples.
So, for example, there is
also a jump above or
jump below, jump greater than or
equal to there's a whole bunch.
And so since I'm comparing X
to SBX, I maybe want to see
if they are.
Let's see. I want to see if
I will try one.
See if that works.
So jump if this one is greater than
so, maybe
that one, see
if that works.
And you can always run it.
And it looks like it did right, so
it kept going until the first
the number we were printing was
greater than instead of greater
than or equal to.
And so we're going from the numbers
from one to seven is what we're
printing off.
All right, so in summary, we talked
about control structures and
looping, and we gave some examples
of how to use both the
loop construct and
how to build our own loop using
the compares and the jumps.
Looking forward, we're going to do a
full on loop example using the loop
command and then we'll talk about
rotation and shifting.
If you have questions, you can
contact me at Miller, MJ, at you and
Kada Edu and you can find
me on Twitter at Milhouse 30.