Hello, this is Dr. Miller, and this
is Episode eleven point five of
Assembly
today we're going to do an example
that's going to use a loop and use
some shifting that we've talked
about previously.
So here's our armed shift example,
so here's our problem to be solved,
so we're going to do is we're going
to read a number from the user and
then we're going to print off the
powers of two from the number zero
two and minus one.
All right, so I've created the.
Project already.
Go ahead and go into our directory.
All right, so for this example,
we're going to use printf so at the
top, we're going to define our
format.
Remember, this uses the S.
S Z,
so to put a zero at the end and
this is what our output is going to
look like. So we're going to say to
to the D.
So to the number is equal to
present D
and we'll put a
flash. And so we have a new line.
So that'll be our format.
All right,
save our link register
so we can use it.
All right, so from the user, what we
want to do is we
want to branch link
and call read.
And so now
our zero has an.
And that's the maximum we want to go
and then I'm going to use registers
are seven, eight and nine in
order to hold this because our
zero through want to
we're going to use for printf.
So
start off our nine gets the
number one and this is going to be
our
constant to shift.
So it'll have
the number one in it and then we'll
be able to do
our shift from there.
Our eight was going to be Amax,
so we're going to copy it into
their.
And then
R7 is going to have our counter,
so we're going to start that at the
numbers zero.
All right. And then we're going to
have our loop's a lot of the top of
our loop.
Some point down here.
We're going to say Brandes to top.
And we'll probably have a
done label down here so that we can
exit our loop,
so at the top we're going to do our
comparison.
We're going to compare our seven
or eight,
and then we will branch
when it is greater than
or equal to.
And that's when we're going to exit.
So we'll do our comparison.
And then when
our seven is greater than or equal
to our eight, then we'll go ahead
and exit.
All right, and so here we're going
to use our barrel shifta, so we have
our nine, which has the number one
on it, and we're just going to
basically shift those bits over.
So we're shifted over zero
and then one and then two and then
three all the way up until we get to
the end.
So at the end, we're going
to do a left shift,
logical shift left on
our seven.
And the number we want to shift is
going to be our nine.
And then where we're going to store
it, we'll say
are zero for the time
just so we have it.
All right.
And then what we need to do.
So now our zero has the result that
we want to print.
But at the end of the day, right.
We actually probably want this
to be the last argument that's
pushed on for printf.
So at some point down here, I'm
going to say Brant's with link
to begin to think
printf.
And the first argument in our zero
has to be our format, so we'll do
a register or zero
and then our format is.
Format, so we get the pointer
and put that on there
and then the number that we
want to print, so
we're going to use
if these are one and
we'll move our seven into that.
And then hour two,
then get the last thing,
which is the number to the power
that we have, and so right now
that's in our zero.
So it's easiest if you start with
where are you going to go and then
build up that way you can remember
all the things that you need to
remember in the appropriate order.
So the way print works is that you
use argument zero, one and two
from these different registers.
And then after we're done, we need
to increment our counter.
So add our seven.
And we need to add one each time,
so our seven starts at zero and
then goes one, two, three, four,
five, six, seven, eight, and then we
are shifting by that amount here.
Again, using our barrel shifta.
And then when we're all done, we
branch back up to the top again, do
our comparison of our seven, which
has our number, and our eight
which has our maximum, and
going to go through and it should
print them out.
And so here we printed off two to
the zero to two, the one all the
way up to two to the nine, because
we entered the number 10
and we can try with different
numbers. So powers
of two, there are powers
of three,
up to three.
We can do probably about 30
and get a nice positive numbers, but
if we go to 32,
we get into a negative number,
right, because we pushed into the
sign value.
And I think if we do thirty three,
our last one ends up being zero
because we've shifted so much that
we don't have anything left.
So you could probably do a 100
and we'd get lots and lots of zeros.
All right, so today we did an
example program and we used a while
loop and then we ended up
using a barrel shifter in order
to shift our amount
in a single move command.
Looking forward, we'll talk about
debugging and memory offsets and
indexing.
If you have questions, you can email
me Milham, you incredible
and you can find me on Twitter at my
house 30.