Hello, this is Dr. Miller, and this
is Episode five point one of
Assembly.
Today, we're going to talk about
shifting either left and right,
and this is going to be a very
intense and low level and binary
type of episode.
So shifting so a logical
shift is where we take all of the
bits and we either move them to the
left or move them to the right.
And then the last bit that we move
out is going to set our
carry flag.
And then that ends up getting all
the bits that are moved out on the
other side. They're going to be
replaced with zeroes.
So we can see an original here where
we have an A and a nine
and we go ahead and shifted to the
right by three bits.
So then right,
if we look at our bits.
Right. So the top bits here.
Oh, my cursor, once I let them all.
All right, so those bits are going
to get moved over and then in their
place are going to be the zeros,
right? Normally you'd arrange it in
groups of four, but I was just
showing how those bits get shifted.
If we move the other way, we can see
that these bits here.
Sorry.
Come on, highlighter right
there, right, those gates
bits get moved over and then
they get filled in with zeros
and so we can see that it
allows us to move.
And what happens is when you either
shift left or right and if something
is stored in binary, that's
like multiplying and dividing
by two.
If you go one way or the other and
it's actually quicker than if we do
it a different way, like using the
multiplication or the divide
operators that we've looked at thus
far.
All right, so let's start with an
example, so we're going to move into
the numbers, see one, two, three in
Hex, right?
So you should be able to I gave you
the table here.
You should be able to convert that.
Right. If we look up in the table,
we see that see is one one
zero zero one
is zero zero zero
one two is zero zero one
zero and three is
zero zero one one.
So we go ahead and write those
numbers down and then here
we're clearing the Kerry flag.
So that way it is ready for us to
do our operations.
And so then if we shift to the
left, we're going to take all
of these bottom bits and we're going
to move them to the left
and then we'll fill it in with a
zero. And then the number that pops
off the end is going to set our
carry flag so we can see that a
number of the pops off the end if we
shift it by one, is going to be this
one here.
And Seattle set to carry a flag
to a one.
And if we can our number back into
Hex, we can see that this is an
eight. This is a two,
a four and a six.
And I carry if I got up to one and
we can see that at this end.
Right. We added another zero at
the end.
Now, if we shift it back to the
right by one.
We can see that all of these numbers
got shifted over and
we filled the top in with a zero,
and so you can see here we shifted
to the left by one and then shifted
to the right by one.
And we end up with a different
number, right. The one, two, three
get stayed the same, but we
lost that top bit.
Right. So it got put into the
carrier flag and then tipped us to
never return.
Right in here we see that the zero
was what got popped off.
So our flag get set to zero.
And then lastly, we shift to the
right by three, so we
add three zeros here at
the top, right.
So the things that fall off are
going to be this right here,
this zero one one.
Right. So zero one one fell off the
bottom, but the zero was the last
thing to fall off.
And so that's what our flag is
going to get set to.
All right, so what we're going to
do, we're going to do an example, we
read a couple of values from the
user, both the value and the amount
to shift, and then we're going to
shift that value and print off
the result. And we'll also do a
couple of registered dump's
at the same time.
So we can see I'm in my projects
directory.
Well, go ahead and go in there.
Now, for this one, I'm going to want
to print off a couple of prompts,
that way I can
tell the user what we want to do,
so we'll go ahead and define those.
Go ahead and terminate it with a
zero.
As for the amount to shift.
But and make it look nice.
All right, so I got my prompts
here,
so to print those movie X
prompt.
Call the function the printed.
And then read a value from the user.
OK, so now that values and ethics
and I'm going to end up moving that
somewhere else, I'll go ahead and
move that into, let's
say, SBX.
No need to print off my second
prompt.
OK.
Read my number.
And then I'm going to move that
into.
Into X, the reason
I don't just leave it any X is it
doesn't like me to shift with the Al
Register and won't let it.
So we've got to do something
different. So we'll use the X
register
and then I'll go ahead and move in
to X
the value I stored up here, which
was an X, so
I have X and yak's
are the things that I want to do and
then I'll go ahead and dump the
register so we can see what's in
them. So
dump rags
one and we'll do
our shift. So shift right
of X and we'll just use
s.L in order to do the shifting.
Denpa was again,
and then we'll print off our
numbers, so we'll just call print
and.
And print a new line,
and that way we should see our
results in the middle before
we do the shift and then after we do
the shift with our carry flag.
All right at all, it just
complained about my clock being off
so well, we're on our program,
so let's enter the numbers.
So we're going to enter Xerox
A.
And if we want to shift it by
one bit.
So we can see that AA was what we
had and so they got shifted
to the right
by one bit, right.
So it turns out to be five five.
Which is the number eighty five,
which is not helpful when we don't
know what it is.
So if you convert that, you should
be able to figure out what our
number was.
So let's do a number we could easily
do. So let's say
16
and we'll shift it by two or
two squared is four.
So our number should be four
and 16 and hexes one
zero.
And if I shift that by two, it moves
it to bits down.
And so I get the number four
in Hex, which is also for decimal.
So we divide it by two
and we could modify this.
Who don't want to modify the
binary, we can modify
it. So instead of doing a shift,
right, we could do a
shift left.
So now we would be multiplying
instead of dividing.
So if I have the number
two hundred and seven
and I want to multiply it by two, I
can shift it by one.
And so I got four hundred and
fourteen.
So that was our example.
So today we talked today about both
shifting left and right and how we
can kind of see how it works by
using the dump regs
and getting to see where
the Kerry flag gets set
or not get set and how it gets
determined, especially when we
rotate our shift a bunch
of bits out.
Next time we'll talk about
arithmetic shifts in actual rotation
and carry and how that works.
If you have questions, you can
contact me at millirems, at UNC
Edu and you can find me on Twitter
at Milhouse 30.