# Shift Example

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Required fields are marked with an *
or Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:01
Oh, this is Dr Miller. And this is Episode 11.5 of Assembly.
00:06
Today, we're going to do an example that's going to use a loop and use some shifting we've talked about previously.
00:14
So here's our arms shift example. So here's our problem to be solved.
00:17
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.
00:29
Alright, so I've created the,
00:33
00:38
Go ahead and go into our directory.
00:44
All right. So for this example, we're gonna use print F. So at the top, we're going to just find our format.
00:52
Remember, this uses the s
00:54
esque easy,
00:57
so to put a zero to end. And this is what our outputs gonna look like. So we're gonna say to
01:02
to the 1% de so to the number is equal to
01:07
percent de,
01:10
they will put a
01:11
flash. And so we have a new line,
01:15
so that will be our format.
01:23
All right.
01:25
Save our link register
01:32
so we can use it.
01:33
All right. So from the user, what we want to do
01:36
because we want to branch with link and call. Read
01:42
it.
01:44
So now
01:46
or zero has and
01:49
and that's a maximum we want to go,
01:52
and I'm going to use registers are 789 in order to hold guys because our zero through 12
02:00
we're going to use for print F So
02:04
start off our nine gets the number one, and this is gonna be our
02:09
constant to shift,
02:13
so it
02:15
it will have
02:15
the number one in and then won't be able to.
02:19
I'm do our shift. From there.
02:23
Our eight is gonna be our max,
02:28
So we're gonna copy in into their,
02:34
and then
02:37
our seven is gonna have our counter. So we're going to start that at the
02:42
number zero.
02:45
All right. And then we're gonna have our loops, a lot of the top of our loop.
02:49
Some point down here, we're gonna say brands to top
02:54
on, we'll probably have ah
02:57
done label down here so that we can
03:00
exit our loop.
03:01
So at the top, we're gonna do our comparison. We're gonna compare r seven and r eight
03:08
and then we will branch when it is greater than or equal to.
03:15
And that's when we're gonna exit, so we'll do our comparison. And then
03:20
when
03:21
our seven is greater than or equal to our eight, then we'll go ahead and exit.
03:27
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
03:36
zero and then one and then two and then three all the way up until we get to the end.
03:42
So
03:43
at the end, we're gonna do a left shift
03:46
logical shift left
03:46
on our seven,
03:49
and the number we want to shift is gonna be our nine.
03:53
And then where we're going to store it
03:57
well, say are zero for the time,
04:00
just so we have it,
04:01
all right?
04:02
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
04:12
to be the last argument that's pushed on for print F.
04:15
So at some point down here, I'm going to say a branch with Link
04:19
who
04:19
04:21
print F
04:25
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
04:35
format. So we get the pointer and put that on there
04:39
and then the number that we want to prints of D
04:44
we're going to use
04:46
if these are one, and we'll move our seven into that
04:51
and then our to
04:56
then get the last thing, which is the number to the power that we have. And so right now that's in our zero.
05:02
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
05:11
remember an appropriate order. So the way print off works is that use argument 01 and two,
05:16
um, from these different registers.
05:21
Then after we're done, we need Teoh increment. Our counters add are seven
05:30
and we need to add one each time. So are seven starts at zero and then goes 12345678
05:35
And then we're shifting by that amount here
05:45
again using our barrel shifter.
05:48
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
05:55
and or eight which has our maximum.
05:57
I'm gonna go through and it should print them out.
06:09
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
06:15
when we can try with different numbers. So
06:18
powers of two there or powers of three are up to three.
06:26
We can do probably about 30
06:29
and get nice, positive numbers. But if we go toe
06:31
32
06:33
we get into a negative number, right? Because we pushed into the sign value.
06:38
And I think if we do 33
06:41
our last one ends up being zero because we've shifted so much that we don't have anything left,
06:46
so you could probably do Ah, 100.
06:49
And we get lots and lots of zeroes.
06:56
All right, so today we did an example program and we used a wild loop.
07:00
And then we ended up using a barrel shifter in order to shift our amount in a single move. Command.
07:08
Looking forward, we'll talk about debugging and memory offsets and arm indexing.
07:14
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.
Up Next