Hello, this is Dr. Miller, and this
is Episode 12 11 of Assembly.
Today, we're going to talk about
conditional execution and we're
going to do an example,
so conditional execution example,
so what's our problem to be solved?
So we're going to write a program
that generates a random number and
then we're going to try and guess
the number and we're going to use
conditional execution inside of
our program.
All right, so at the top, we'll have
a couple of messages so
big.
So know it's too big,
small.
And then.
All right,
and then we'll have one more
message.
It says to ask them for a number.
From zero to 20,
that's a given and you can change
this range if you want.
But you don't have to.
All right, so a couple
of things that we're going to do,
so we're going to use the
time function as Rand and then Rand.
So the time function tells us what
time it is.
According to the processor,
the RAND initializes our
random number generator and then the
RAND function will
give us a random number.
So we'll just go ahead and call
those. So we just call time
as Rand
Rand and then
here we're going to do a little bit
of math. So what I'd like to do is I
would like to get a number
between zero and 20, but
this is actually just giving me an
absolutely random number.
And so there's a formula that says C
is equal to a
mod B,
and that's the same as C
is equal to A
minus B.
Time's
A divided by B,
so this is just a bunch of math so
that we can end up doing mod,
so I'm going to go ahead and
do the math.
You don't have to try and replicate
it, but this is just
taking some well known formulas and
converting them into assembly.
So we're going to move into our one
and here you can change your number.
So I'm using the number twenty
and then I'm using you div
storing the result in our three
hour zero hour one
and I'll put some comments in here.
A divided like
A divided by
B
and then more R four
as a result.
Are one
in or three.
This is B times
A, divided by B,
all right, and then we do
sub R
eight R
zero hour for.
So our aid has
our number.
So this is our random number that
we're going to have,
and then here we're going to start
off and we will use move into our
nine,
the number zero.
OK, so
then we have our loop.
Here is the top of our loop.
At the bottom, we're going to do a
branch, not equal
top,
so we're going to use a couple of
different things and so you'll get
to see some of the different
conditional executions that we can
have.
So, first of all, ask the user for
our number load register
our zero, our
message, asking
them to enter a number.
Well, then print off
and then we'll call,
read it.
All right, so now our zero has our
number that we are looking
at and
we want to compare it so our
eight has our random number that we
chose up here.
So we're going to compare
our zero.
Our eight.
OK.
I'll put a comment just so we see
it, r zero
is our number
or eight
as our random number.
So what we can do is we're going
to load into
our zero a different message
based on what they do.
So we can do load, register
and then add E q to
say loaded if it's equal
or zero,
the same message.
Right. So it says that there are the
same.
We could also do Lord register
greater than.
It's too big a
lower register, less
than.
Small, and then
I'm going to put this up here,
because they're kind of along the
same lines, what I'm basically
going to do is set a flag
to tell myself, oh, by the way,
we are done.
So we'll just do a
move.
Equal
are nine
the number one.
And so that's going to say our nine
starts at zero, saying we're not
done.
And then we set it to one to say
that we are done.
So then down here, well,
first of all, we'll call our printf.
And then.
We will.
Compare our
nine to the number one,
so if it's not equal to the number
one, then it'll jump back up here to
the top, print off our message,
read the number from the user,
and then compare our number
with the random number that we were
given.
All right, let's see what we did.
So I think one of the things that we
forgot to do is move
into our
zero, the number zero
for our time function to tell us to
give it the current time.
So now we get to enter a number,
so let's say if I'm picking
just terribly, I can say
one, two, three, four,
five, or I could
start in the middle. Ten, ten
is too small. 15,
15 is too small.
18 is too big.
17 just
right.
And each time you run it, it should
give you a different number.
So let's again do ten.
Ten is too small.
15.
Fifteen is too big, 13.
Twelve, all
of and.
So we can see inside of here that we
had our conditional execution that
allowed us to do four
different instructions based on
whatever flags that we cared
about for equal
greater than or less than
sorry, we didn't have a bunch of
comparison jumps.
So today we solve the problem of
guessing a number using conditional
execution.
Looking forward, we're going to do
conditional execution versus
branching and then we'll talk about
Indianness and we'll talk
about it. Box.
So if you have questions, you can
contact me, Miller, MJM, you,
Ankita, Edu, and you can find me on
Twitter at Milhouse 30.