Hello, this is Dr. Miller, and this
is Episode four point two of
Assembly.
Today, we're going to be talking
about the if translation
looping and then an
example of the if and
loop altogether.
So the if in the loop,
so from our template, we have the
example that we can do
a COMPAR on,
for example, one register and
another one, and then
in order to jump
so we can jump over some code.
And that is the code in here for the
negation.
So we can say jump zero label,
eh? So if they're equal, it'll
jump down here and then do the code
when they're equal
and then it'll continue on through
the end label.
But if they're not zero then we
can execute some code where they
are not equal and then we can
jump to the end.
And so that's an example.
If you have a high level language,
you can implement an if statement
or and if else statement
using this template here.
Additionally, we have the loop
construct, so we talked about
that in the previous lecture where a
decrements X
and then it checks to see is that
zero, if it's not zero, will
jump back to the top or the labeled
that's listed inside of the loop.
And then we'll give an example here
of a bad loop
of something that students often do
when they are doing coding.
So let's do that example.
So I've already created a directory.
So I'm going to go ahead and edit
that and let's just do a really
simple program, so, for example,
we're going to call read
it
to read a number from the user,
and then I'm going to copy that into
X so I can do a loop on that number.
So remember, the result of Reden is
an exact copy that X,
and then I'll create a label for the
top of my loop.
So I can go back
now, what I'd like to do is just
print off this number
so I can copy that back into.
Into the variable
where we can print something so X
and I'll copy the value of X.
So what my loop counter is at this
time and then I'll call
print int
followed by
print new line, sort of print
a new line at the end.
So if we run our program, when we
type the number 10 in,
it'll print off, so I type
the number 10 it printed ten, nine,
eight, seven, six, five, four,
three, two, one.
What happens if you do
zero?
So as you can see, and I had control
to kill that, so
what happened is it did a decrement
on our register,
right? So it does a decrement of X,
so if it's zero, the number becomes
negative one.
Well, it doesn't actually know that
that's a negative number.
It just thinks that it's a really
big number.
And so it'll basically loop.
So as we can see, when we ran this
program, it was looping forever
because it went into negative
numbers and
see if I.
So it went to it started off at
negative one, negative two, negative
three, right. And just kept going
down. So it just kept
decreasing that negative number
forever until I hit control.
So if you're writing a loop, you
want to make sure one of the things
you don't do is start off with the
number zero, because then the
loop will decrement and it will
basically loop forever, or
at least it'll loop for a really
long time, which is probably
not what you intended.
All right, so let's do another
example, so we're going to read
the numbers from the user and then
we're going to print prompts for
each of the inputs and print prompts
for the outputs and then
add an additional case that we can
stop if they enter the No.
Negative one.
So let's create a new project.
So I'm in the projects directory.
I should see my new project here.
All right, so the first thing that I
want to do is I want to print a
prompt for the user to enter
something so.
I need some prompt text, so
I'm going to call this.
Print string method to print our
string,
but we don't have a string, so we
need to create one.
If you recall, we can put in the
data section our prompt.
And we can call it whatever we want,
but I'm going to call it prompt
and I'll say
enter the number.
I like putting a colon because it
kind of shows where they should type
something,
I'm going to have a couple other
problems, so we'll get to those as
we as we get in there.
So we move our prompt into X,
we call print string.
So not prints that string from the
user. And then we want to read
something from the user.
So we'll call, read
it.
And then again, we're going to use X
for our
loop. So I'm going to copy
into X the value of
X. So this is how many numbers I
want to read. So if they take five,
I want to read in five
different numbers.
Additionally, I'm going to use SBX
for the the sum
of all my numbers, so I'm going to
add up all the numbers that they
have typed.
So I'll go ahead and zero that up
and then I'll have the top of my
loop.
And at some point here, I'll have.
Looping back to the top.
So now what I want to do is I want
to read a number from
the user.
So, again, I want to print another
prompt, asking them to enter the
number.
So let's go up here.
We'll add another prompt.
I call it enter.
Again, putting a call in to make it
look nice
and putting a zero to make sure we
terminated, if we don't, we'll get a
bunch of garbage.
So that would probably not be good.
Again, to print off a string,
we move into our
prompt,
which is actually called enter.
And I'll put those put into
a prompt
make sure enters
a reserved word so.
OK, so we
put in our prompt TJX call
Prince. So now Prince it, and now we
want to read the number from the
user.
So we call Reid in Seattle, Reid, a
number from the user,
and then we we're we're going to
want to add that eventually to
SBX, right.
And the result of Reid is going to
have our value in X.
And so we go ahead and add that to
our current sum, which is an Ebix.
But we also wanted to have the
ability to say if they entered the
No. Negative one, then
we want to exit the loop right away.
So I'll do a compare.
And then.
Jump off at zero to done
so, Dunn is going to be something
that's going to come down here
and this is where I'm going to go
ahead and print off the sun.
So down here, I'm going to again
print another string, so.
I'll call my shrink some.
So now I got to go to find my
variable called some at the top.
Printing off on my total,
again, putting in zero to make sure
I terminate it.
OK, so
I print the string and then SBX
should have my thumb and so to print
and I got to move that back into X.
And then print into print, whatever
is in the X
and print new line
will print a new line at the end.
So that's my program.
Well, make sure it compiles
here.
I didn't have any airs,
and so now when I run, it
says how many numbers you want and
I'll say four signs
or four numbers,
add them up.
That should be 10.
If I say I want 100 numbers
and I type 10, 20,
30 negative
one
negative one tells it that you want
to stop. And so I've got the numbers
going to 30.
Now, one thing I'll point out, so
when we get later on
and you're going to end up calling
functions like print
int or print F,
some of those will actually modify
X,
and so that will break your loop.
So you go along, you're writing
something you call print F,
all of a sudden your loop will stop
working. And that's because print F
modified X,
we'll get to that later.
But it's just a helpful hint
to know that sometimes
things will change in registers get
modified and X is one
of them that can get modified and
you don't realize it.
And then all of a sudden your code
doesn't work anymore.
All right, so today we showed an
example of doing some loops
and if statement and then kind of
putting them together together in
one large example,
looking forward, we're going to talk
about doing shifts and rotations.
If you have questions, you can
contact me at Miller, M.J., at you
and Kada Edu and on Twitter
at Milhouse 30.