Hello. This is Dr Miller, and this is Episode 5.1 of Assembly.
Today, we're going to talk, talk about shifting either left and right. And this is gonna be a very intense and low level and binary type of episode.
so 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 zeros,
so we can see an original here where we have an A in a nine, and we go ahead and shifted to the right by three bits.
right, if we look at our bits, right, So the top bits here,
my cursor wants violate them all.
All right, so those bits are going to get moved over, and then in their place, you're gonna be the zeros.
I don't 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
right there. Right. Those gets bits, get moved over, and then they get filled in with zeros, and so we can see that it will 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 gonna move into a X, the number C 123 in hex.
Right. So you should be able, Teoh. I gave you the table here. You should be able to convert that.
All right, if we look up in the table, we see that, See? Is 1100
one is 00012 is 0010 and three is
So we go ahead and write those numbers down.
And then here were clearing the carry flag so that way it is ready for us to do our operations.
And so then if we shift to the left, we're gonna take all of these bottom bits and we're gonna 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 you can see the number that pops off the end. If we shifted by, one is gonna be this one here, and Salata said that carry flag to a one.
And if we convert our number back into Hex, we can see that this is an eight. This is a to a four and a six,
and our careful, I got set toe one, and we can see the at this end, right? We added another zero at the end.
Now, if we shifted 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 for the right by one. And we end up with a different number, right? The 123 gets stayed the same,
but we lost that top bit, right? So I got put into the carry flag and then to bust to never return
right in here. We see that the zero was what got popped off so are careful and gets 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 gonna be this right here. This 011
Right. So 011 fell off the bottom, But the zero was the last thing to fall off until that's what our care flag is going to get set to.
All right, So what we're gonna do, we're gonna do an example. We read a couple of values from the user. Both the value and the amount to shift.
And then we're gonna shift that value and print off the result. And we'll also do a couple of registered dumps
so we can see I'm in my projects directory.
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 terminated with a zero.
As for the amount to shift,
put a colon, make it look nice.
All right. So I got my prompts here.
So to print those movie X prompt
call the function to print it
and then read a value from the user.
Okay, so now that values in the A X and I'm gonna end up moving that somewhere else, I'll go ahead and
move that into laissez ebx.
I need to print off my second prompt.
and then I'm gonna move that into
The reason I don't just leave it any exes? It doesn't like me to shift with the A. L register. It won't let it. So we've got to do something different. So we'll use the E c X register
and I'll go ahead and move into the X
the value I stored up here, which was an e b X.
So I have easy X and yaks are the things that I want to do and then I'll go ahead and dump the registers so we can see what's in them. So
we'll do our shift. So shift right of yea X and we'll just you see l in order to do the shifting
dumper registers again
and then we'll print off our number. So just call print and
and print a new line
and the way we should see our results in the middle before we do the shift. And then after we do the shift with their care flag,
all right at all. It just complained about my clock being off,
so we'll run our program. So let's enter the numbers so we can enter zero x a.
And if we want to shift it by one bit
so we can see that a A was what we had. And so that got shifted
right by one bit. Right? So it turns out to be 55
which is the number 85 which is not helpful when we don't know what a is. So if you convert that, you should be able to figure out what our number was, let's do a number we could easily do. So let's say
and we'll shift it by two or two. Squared is four.
So our numbers should be four, right and 16 in hex is 10
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 foreign decimal. So we divided by two,
and we could modify this
I don't want to modify the binary.
We could 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 207
and I want to multiply by two, Aiken shifted by one. And so I got 414
so that was our example.
So today we talked about both shifting left and right and how we could kind of see how it works by using the dump regs
and getting to see where the carry 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 and actual rotation and carry. And how that works.
If you have questions, you can contact me at Miller M J u N K D i e d u and you can find me on Twitter at Milhouse 30.