Hello, this is Dr. Miller, and this
is Episode eleven point four of
Assembly
today we're going to talk about
branching and using an F
or a while and then shifting.
So branching,
so branching is the branch
construction, it starts with a B
and so you can do a regular branch
or just a jump to an address,
you can do a branch with link.
So that will save the link,
the address of the next location
you're going to go to into the link
register.
And then we also have the branch in
exchange. So it exchanges the link
register in the program counter.
So that will allow you to go back
where you came from.
We also have conditional branches,
just like we had conditional jumps,
and so the ones you'll probably most
often use are equal,
not equal, greater than
greater than or equal to
less than or equal to these
over here on the left hand side
are probably the ones that are most
often used. But there's also
ones, for example, minus or
plus
that we can use.
And so you just have to choose
based on your application, what
type of conditional branch you want
to use.
So here's an example.
So these are the
strings that we are defined at the
top, so we have A and we have B,
so A says not seven and B
says seven exclamation point,
followed by a new line.
And so we can see that we are
calling or branching with
a link, the reed int
function.
And then we are doing
a we do a move
of the number seven into our one and
then we compare our one to
R zero.
And so if they are equal, it jumps
down here to the seven
where it prints off the string
seven.
And then if they're not equal
or any other case, it'll jump over
here to the not seven.
And so here this is implementing
a nice if else.
So if it's seven, do this code
right here.
If it's not seven, do this code down
here.
And then as we've seen before
at the end. Right, we do a branch to
the end for each one of these.
This is a little redundant, right?
Branch to end where end is right
here.
So this isn't necessarily needed,
but it was added in there.
Just for clarity, if you had
additional code that you might want
to do or an additional
comparison, this will give you a
good template in order to start
that.
And then we can have a while loop,
right, so in arm, we don't
have the loop come in like we did in
86 and so
we can see here again, we're reading
a number.
And in this example, we have a Soman
encounter. So the sum starts
off at zero, the
counter starts at the number one
and then the counter, if we remember
branch with the ingredient.
So that's going to read in a number
from the user and
then that'll get stored in R
zero. And so this is where
we're going to end.
So while
R2 is less than R zero,
we're going to
do this code here.
Right. So while our R zero
is bigger.
So when it's all done, then we'll
exit. And so here we go ahead and we
do an addition.
So we're adding our one and our
two together and then we're storing
it in our one and then we're
incrementing R2.
So our two is our counter.
And then when we get down here, we
come back up to the loop and then
and do a comparison of R zero
and R two.
And then when we're all done, our
sum was in R one.
And so we just copy that into R zero
so that we can use the print end
function in order to print it off.
So this is going to go multiple
times until the condition is met
such that it'll exit.
And so that's a implementation of a
while loop.
All right, shifting.
So we have a variety
of different shifts.
We, again, have seen a lot of this
in 86,
but we have sort of a different
notation.
Again, the constant
so we have our destination register.
We have the one holding the operand
or the thing that you want to shift.
And then we have possibly either
a source or a constant.
And so we can shift by logical
shift left by a
another register or a constant.
And we can do a shift.
Right. We can do with arithmetic
shift. So we make sure that we keep
the sign.
So no sign numbers, the
leftmost bit as a one if it's
a negative number.
And so if you do an arithmetic shift
to make sure you keep that so that
the number doesn't go from being
negative to being positive.
And then we have the rotate, so
rotate, right, we can rotate
either by a register or by
a particular amount.
And then we have the barrel shifter,
so the barrel shifter allows us to
shift a particular number
of bits
in another operation.
And so generally with the barrel
shifter, we can either do a shift
or we can do it or rotate.
And again, these are just moving
some bits around so that they
don't change.
And this is basically such that
we can either multiply or divide by
powers of two and we can
do some rotation of bits.
And then if you look back, shifters
are actually used for floating point
arithmetic units and
so on.
The back end floating point might
use this in order to align different
numbers.
So here's an example of a barrel
shifter, so, for example,
we can
move a couple of constants here into
some registers.
And so Barasch after allows us to
say, I'm going to take R zero
and I'm going to do a left shift by
one, and then I'm going to store
that result in R one.
Right, and that's basically
multiplying it by two
or I can take our zero, multiply
it by or shift it by
two, which is due to the two, which
is four, so I can multiply
our zero by four and
then saw the result in our one.
Here's an example where you can do
it with another register
so I can do a left shift of our
nine so we can see our nine,
I moved the number 10 in it.
So I take our zero
and I multiply it by
two to the ten or a thousand twenty
four and then store that result
in R1. So we can see we're doing
both a shift and a move
or we don't have to just do move.
We can do add.
So here we're adding them together.
So our zero plus our two
are two is then left shifted by
three, so two
to the three which is eight.
So that gets
shifted and then added
to our zero.
The result is then stored in our
one.
And here we can do a shift.
Right.
And we can do a subtract.
So R zero minus R two
shifted to the right or divided by
16.
Additionally, we can do some
rotations, so here's an example
of how to load a thirty two bit
constant, we can use a load register
to load a constant in there.
So we have a BBQ
and so we can take that number,
which is in our zero,
and then we can rotate it to the
right by for four bits
and saw that in our one.
And so then the result would be
d.E.A BBK
D. Right.
So all of these bits got
shifted over and then the D wrapped
around.
And then we can also do it for the.
We can rotate to the right eight,
so rotate eight bits, which would be
to hex digits and so the.
The values getting shifted over.
So today, we talked about branching,
I'm looking at a statement
and a wild statement, and then we
looked at shifting and
Barel shifting.
So in future lectures, we will give
an example of the various schifter
and we'll do that in code.
We'll talk about debugging and
memory offsets and then ALM indexing
for RES.
So here's our quiz, so
if we compare R zero and R one,
how would we branch if the first
register is less than the second
register?
So we can say branch less than
other, and this
would be some label that we would
branch to.
If you have questions, you can email
me Miller at you and KSTU
or find me on Twitter at Millhone
Sturdy.