Hello. This is Dr Miller, and this is Episode 5.2 of Assembly.
Today, we're gonna be talking about more arithmetic shifts in including double precision and will give an example.
So arithmetic shift.
So last time we learned about the normal shift where we just shift the bits and we don't care about if the thing is assigned number or not.
we they invented the arithmetic shift or signed shift. And so it's like the logical shift, except for we make sure that we keep track of the sign so that our number doesn't change when we divide by two or multiplied by two or power off.
And so we have shift arithmetic left and shift arithmetic right.
And then we also have our double precision shifts on. We'll see those in a little bit.
So double precision.
So the double precision shift we can use S h l d. To denote that we're going to do that. And we basically have a destination where we have the bits that are gonna get shifted, the source and then how much count? And the sources used to fill in the bits. And so if you need to fill them in with ones
or need to fill them in with zeros. You would set source appropriately in either one of these.
So let's do an example so well, prompt the user for a couple numbers and then we'll get the amount to shift in. Them will shift it and then we'll print off the result and we'll do some testing with both positive and negative numbers.
Alright, so I've logged on.
And so if we run Gen make 5.2, which is the lesson, it will generate a folder for us
so I can see all my folders.
the current directory.
So what I want to do is I'm gonna get a couple of different prompts for the user again so that they know what we're doing.
put my colon in here to make it look nice. Make sure, I know, Terminate it
again, putting a colon to make it look nice and no terminating that
when we get down here in our main. So in here we'll go ahead and we have to put our prompt in t a x once again
which will print off the string that has pointed to
And then we'll go ahead and call
read int. So this is getting the
shift amount and then we're going to have to move that somewhere else. It's all go ahead and use E c. X.
And remember they called to read int is going to read a value and then store it in e X and sly. Copy it from E X into easy X.
Now you can print on my second prompt.
It looks like I spelled prompt her own
So move it into yea X. And then that's what I can use to call print string. And then all once again, Cole
and to get a number from the user.
All right. And so now this is the part where I'm gonna want to do my shifting
so I can do shift arithmetic left.
See the A X is our number, and then the amount we want to move it will have stored in. We started Annecy X, which remember, is broken down into C X and then CXS further broken down into C l and C H. And so we'll just use the low bits in C L.
I'll go ahead and call dump regs
so we can see what the registers look like after we do that.
And then I want to print my results. So my result is in E X, But I want to print a string saying the results. So let's go ahead and put a
Come back down here.
So since I'm gonna print it, I'll go ahead and I'm gonna have to use the a X in order to print that string. And so I'm just gonna copy
my value into E B X. And so you have it Stay saved someone go to print. It'll be there and want to move it back in D A X so movie a x
result, which is my prompt.
now I got a copy of back into yea X
and starting ebx currently.
so that should be my program.
Looks like I typed it right. So now we can go ahead and run it.
So let's say we're gonna shift by two, which is gonna be by four, right to to the
two or two squared is for,
and so we'll start off with an easy numbers list. You just 20
right? So 20 times four is 80.
right, so we can see that our sign got kept right? So when we did our shift, the sign is the same now. Multiplication or it is going to shift it over. One way we want to do division. We can go ahead and add it. This
we'll go down to the line where it says shift left and we can change that into a shift. Right?
Go ahead and build my project.
Run it. And so now if I do so let's let's do an example of dividing by four
and we'll do negative 50
right? And so we're shifting
normally. When we shift in this direction, right? These efs. If we use a regular shift, those are not going to get filled back in with EFS. And so we're doing the arithmetic shift. It says if that bit is a one than when you do this shift, you fill it in with ones
and so and then it also works if we do positive number, so due to and 50
and then we can see when we divided by 4 50 divided by four is 12.5. We got it rounded up to 13. Or round it down to 12 right? And so those are just some issues you have to take into account. All right, if you're doing a whole number division, so we just lopping stuff off and we're not actually keeping track of it.
Later on, we'll learn about floating point and how we could get Mawr exact numbers than this.
And then we're also going to look at so we can use the additional way of shifting
so using the double precision
and this takes so we'll still use E X. But then we'll want to move something into another register. So let's say we use EBX.
So if we got positive numbers, we can just use zero in here and add This is an additional argument.
And so it'll shift E X by C. L. And then when it goes to fill in the bits, it's going to use E b X.
So let's just look at this
running this one so well, let's type make
to build our project.
Go ahead and run it and we'll say, shifting by two and again we'll take the number
So because our number was negative when it did the shift, a used EBX and I had set it to zero. So it filled in zeros of the top, so the top two bits got set to zero.
So then all the sudden our number got changed from a negative number into a positive number. So if I was doing this shift and I knew I had a negative number,
then instead of using E b X set to zero, I consented to either all laughs.
So that's one way or another way to get all f's and something that's a lot simpler for a person to type
is I could use just negative one, all right, And if you know two's complement, you'd be able to figure out what negative one would be. It would be all laughs.
So now when I build my project and I go ahead and run it, so if I want to shift by two,
my number is negative
but again, I get there Correct number. So it took the bits out of ebx and use those to fill it in. And so
if we had other values that we wanted to do shifts on and we knew that we wanted a certain number of bits and we weren't actually doing
then we could use a different value in here to get those bits that got shifted into the top.
All right, so today we learned about the arithmetic shift and then even doing a double precision one. And then we went through an example of each one of those.
So looking forward, we're going to talk about rotation and how that differs from shifting and rotation with Carrie.
If you have questions, you can contact me at Miller MJ at you and Kate, I e you and you can find me on Twitter at Milhouse 30