13 hours 15 minutes
Oh, this is Dr Miller. And this is Episode 11.5 of Assembly.
Today, we're going to do an example that's going to use a loop and use some shifting we've talked about previously.
So here's our arms shift example. So here's our problem to be solved.
So what we're gonna do is we're going to read a number from the user, and then we're gonna print off the powers of two from the number zero to N minus one.
Alright, so I've created the,
um, project already.
Go ahead and go into our directory.
All right. So for this example, we're gonna use print F. So at the top, we're going to just find our format.
Remember, this uses the s
so to put a zero to end. And this is what our outputs gonna look like. So we're gonna say to
to the 1% de so to the number is equal to
they will put a
flash. And so we have a new line,
so that will be our format.
Save our link register
so we can use it.
All right. So from the user, what we want to do
because we want to branch with link and call. Read
or zero has and
and that's a maximum we want to go,
and I'm going to use registers are 789 in order to hold guys because our zero through 12
we're going to use for print F So
start off our nine gets the number one, and this is gonna be our
constant to shift,
it will have
the number one in and then won't be able to.
I'm do our shift. From there.
Our eight is gonna be our max,
So we're gonna copy in into their,
our seven is gonna have our counter. So we're going to start that at the
All right. And then we're gonna have our loops, a lot of the top of our loop.
Some point down here, we're gonna say brands to top
on, we'll probably have ah
done label down here so that we can
exit our loop.
So at the top, we're gonna do our comparison. We're gonna compare r seven and r eight
and then we will branch when it is greater than or equal to.
And that's when we're gonna exit, so we'll do our comparison. And then
our seven is greater than or equal to our eight, then we'll go ahead and exit.
All right, so here, we're going to use our barrel shifter. So we have our nine, which has the number one in it, and we're just gonna basically shift those bits over. So it shifted over
zero and then one and then two and then three all the way up until we get to the end.
at the end, we're gonna do a left shift
logical shift left
on our seven,
and the number we want to shift is gonna be our nine.
And then where we're going to store it
well, say are zero for the time,
just so we have it,
And then what we need to do so now are zero has the result that we went 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 print F.
So at some point down here, I'm going to say a branch with Link
and the first argument in R zero has to be our formats and we'll do the register are 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 prints of D
we're going to use
if these are one, and we'll move our seven into that
and then our to
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 you're gonna go and then build up that way, you can remember all the things that you need to
remember an appropriate order. So the way print off works is that use argument 01 and two,
um, from these different registers.
Then after we're done, we need Teoh increment. Our counters add are seven
and we need to add one each time. So are seven starts at zero and then goes 12345678
And then we're shifting by that amount here
again using our barrel shifter.
And then when we're all done, we were branch back up to the top again do our comparison of our seven, which has a number
and or eight which has our maximum.
I'm gonna go through and it should print them out.
And so here we printed off to to the 0 to 2, the one all the way up to two to the nine because we entered the number 10
when we can try with different numbers. So
powers of two there or powers of three are up to three.
We can do probably about 30
and get nice, positive numbers. But if we go toe
we get into a negative number, right? Because we pushed into the sign value.
And I think if we do 33
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 Ah, 100.
And we get lots and lots of zeroes.
All right, so today we did an example program and we used a wild 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 arm indexing.
If you have questions you can email me Miller M J u NK that you to you and you can find me on Twitter at no house 30.