# Arithmetic Shift

Video Activity

Join over 3 million cybersecurity professionals advancing their career

Sign up with

or

Already have an account? Sign In »

Video Transcription

00:01

Hello. This is Dr Miller, and this is Episode 5.2 of Assembly.

00:06

Today, we're gonna be talking about more arithmetic shifts in including double precision and will give an example.

00:14

So arithmetic shift.

00:17

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.

00:26

And so

00:27

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.

00:43

And so we have shift arithmetic left and shift arithmetic right.

00:48

And then we also have our double precision shifts on. We'll see those in a little bit.

00:53

So double precision.

00:55

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

01:14

or need to fill them in with zeros. You would set source appropriately in either one of these.

01:22

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.

01:38

Alright, so I've logged on.

01:40

And so if we run Gen make 5.2, which is the lesson, it will generate a folder for us

01:47

so I can see all my folders.

01:49

Go ahead and enter

01:53

the current directory.

01:57

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.

02:07

And these air bites

02:15

put my colon in here to make it look nice. Make sure, I know, Terminate it

02:27

again, putting a colon to make it look nice and no terminating that

02:32

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

02:45

call print string

02:49

which will print off the string that has pointed to

02:51

by E X.

02:53

And then we'll go ahead and call

02:55

read int. So this is getting the

03:00

shift amount and then we're going to have to move that somewhere else. It's all go ahead and use E c. X.

03:07

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.

03:16

Now you can print on my second prompt.

03:24

It looks like I spelled prompt her own

03:31

po.

03:34

There we go.

03:37

So move it into yea X. And then that's what I can use to call print string. And then all once again, Cole

03:45

read

03:46

and to get a number from the user.

03:52

All right. And so now this is the part where I'm gonna want to do my shifting

04:00

so I can do shift arithmetic left.

04:05

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.

04:21

I'll go ahead and call dump regs

04:27

so we can see what the registers look like after we do that.

04:31

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

04:42

prompt here

04:46

saying the result.

04:55

No, terminate that.

04:57

Come back down here.

05:00

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

05:10

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

05:20

result, which is my prompt.

05:27

Okay,

05:29

now I got a copy of back into yea X

05:33

and starting ebx currently.

05:43

All right,

05:45

so that should be my program.

05:50

Looks like I typed it right. So now we can go ahead and run it.

05:57

So let's say we're gonna shift by two, which is gonna be by four, right to to the

06:03

two or two squared is for,

06:08

and so we'll start off with an easy numbers list. You just 20

06:13

right? So 20 times four is 80.

06:17

But what if we do

06:19

two and negative 20

06:24

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

06:36

we'll go down to the line where it says shift left and we can change that into a shift. Right?

06:45

Go ahead and build my project.

06:47

Run it. And so now if I do so let's let's do an example of dividing by four

06:54

and we'll do negative 50

06:58

right? And so we're shifting

07:00

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

07:16

and so and then it also works if we do positive number, so due to and 50

07:24

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.

07:43

Later on, we'll learn about floating point and how we could get Mawr exact numbers than this.

07:49

And then we're also going to look at so we can use the additional way of shifting

07:56

so using the double precision

08:01

so

08:05

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.

08:13

So if we got positive numbers, we can just use zero in here and add This is an additional argument.

08:20

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.

08:30

So let's just look at this

08:33

running this one so well, let's type make

08:37

to build our project.

08:39

Go ahead and run it and we'll say, shifting by two and again we'll take the number

08:45

negative 50.

08:46

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.

08:56

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,

09:07

then instead of using E b X set to zero, I consented to either all laughs.

09:16

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

09:22

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.

09:33

So now when I build my project and I go ahead and run it, so if I want to shift by two,

09:39

my number is negative

09:41

50

09:43

but again, I get there Correct number. So it took the bits out of ebx and use those to fill it in. And so

09:50

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

09:56

recommitted arithmetic,

09:58

then we could use a different value in here to get those bits that got shifted into the top.

10:09

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.

10:18

So looking forward, we're going to talk about rotation and how that differs from shifting and rotation with Carrie.

10:26

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

Up Next

Assembly

This Assembly language course will provide background and information related to programming in Assembly: the lowest level programming language which is useful in reverse engineering and malware analysis.

Instructed By

Similar Content