Hello, this is Dr. Miller, and this
is Episode twelve point ten of
Assembling.
Today, we're going to talk about
conditional execution with flags
and instructions,
armed conditional execution.
So what is conditional execution do
so it allows an instruction to be
executed only when a condition is
met and then it's
based on when flags are set.
So we have compares or tests
or even add addition and
multiplication.
And then we have conditional
instructions and we have the IT
block.
So here's an instruction ad
with without Carrie, right?
And so if we have the
AC on it or add s
ads, then
the conditional flags are going to
be set in the
system.
And so that way, when we go
to execute another instruction, we
can check and see if any of the
flags have been set.
So here's an example
so we can see that we have
currently in the CPS
are we have the zero flag
and they carry a flag and then
we're going to basically
clear our flags with the MSR
so we can see that the flags got
carried.
And then when I coded this, I said
move into our zero negative
one.
But when you go into buggier, it's
due doesn't move and invert.
So when you invert the number zero,
you end up with negative one.
And so that's going to copy negative
one into our zero.
All right, so we don't have any
flags yet, and then we have to do
an ad as our zero
zero one, so our zero
has negative one and if we add one,
it'll end up having the number zero.
And so then the zero flag is going
to get set.
So we've got zero.
And then additionally, we also had a
carry flag set.
So now our zero has the number
one in it or zero in it.
And if we do add again the
number one, we'll end up with the
number one in our zero.
And you can see that the flags
got taken away.
So these instructions
like ads end up modifying
our flags, just like our
test in our compare instructions
did.
So we have several different flags
that we can look at.
So we've got the flag for negative.
So the result was negative
zero. Did the result yield
zero? So again, when we added one
to negative one, our result was
zero.
Was there a Kerry or see?
And that is was the result greater
than thirty two bits.
So did it take more than thirty two
bits to store.
And then we have four overflow
and then it's a result over thirty
one bits. So if a signed result gets
over thirty one bits then it's going
to overflow into the same flag
and then mess things up.
And so that's something that we can
also test. And we're dealing with
numbers that are positive or
negative or signed.
So instructions we have conditionals
that we can optionally add to a
lot of different instructions
and these conditionals specify
which flags we care about so
we can use the conditionals
and then only execute instructions
when the correct set of flags are
set.
And then the reason that
we have this is one reason is that
we can run faster than branches.
So if we think about it when we do
a branch instruction, we have to
flush the pipeline that
they have on the processor.
And this flushing can take
approximately three cycles.
And so if you can avoid doing a
branch, then you can save some
cycles when you are programing
for it. And so that's why
conditional execution was was
created. It makes it a lot faster
than doing the branches and we don't
have to flush our pipeline.
We can just keep executing.
And then we have a variety of
different conditionals and flags,
so typically you'll see the suffix
like E Q or
not equal or not equal,
or if you're looking at something
that's negative or positive
and typically the ones most
programmers are going to use are
less than greater than equal to
less than greater than
less than equal to then you
probably won't use the Al.
So here we again, move negative
one into our zero, we add
and set the flags are zero, are
zero, the number one.
And so we have some additional
qualifications on our move
instructions so we can say move
and then EKU or move,
not equal.
And so here we have the zero
flag set. So
it says Take and Z.
So the Z flag was set when we do
this movie. Q because
when we added one to negative one,
the result was zero.
But if we look at the next one,
we can see that we do move not
equal. So thus command is not
going to get executed.
So one is not going to get the value
of our two.
And then here we do another ad where
we set it so we can see when
we executed this, our one
had the value of one.
So this instruction got executed
and this one did not.
And it just depends on how you
program it as to which instructions
would get executed or not get
executed.
So here's another example.
So we basically,
if the user types in
the number 10, the result is going
to be 20.
If they type open a number greater
than 10 will get one hundred.
And every time something less than
10, we're going to get zero.
And so this program goes through
and reads the number that the
user print user enters.
So calls read int we jump
into this function called bar and
then we basically do a compare of
our zero and the number 10
and then we can have three different
instructions based on
this. Right. So move equal, move
less then move greater then.
So only one of these instructions
because you can't be
any two of them, only one
of these is going to get executed.
So the program counter will just go
over each one and it'll check the
flags and say, OK, I did the compare
and then it'll look at the flags and
decide which one of
these it's going to actually
execute.
And so when you run this, then it'll
either do one of those three
things.
So today, we talked about
conditional execution,
what flags were that we could use
and then instructions and adding the
different the additional suffixes
to make constructions be
conditional.
So in the future, we're going to
talk about a conditional execution
example and then we're also going to
talk about it blocks.
So if we move are
one, the number one move are two,
the number eight, compare our one
to our two.
How would we move one
into our zero if the value
is negative?
So go back and look at your sheet
and see if you can figure that out.
So move my
or for negative or
zero, the number one.
If you have questions, you can email
me, Miller, M.J., Unclutter
Edu, and you can find me on Twitter
at my house 30.