### Video Transcription

00:00

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

00:08

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.

00:18

So shifting,

00:20

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,

00:25

And then the last bit that we move out is going to set our carry flag,

00:30

and then that ends up getting all the bits that are moved out on the other side. They're going to be replaced zeros,

00:37

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.

00:44

So then

00:46

right, if we look at our bits, right, So the top bits here,

00:52

my cursor wants violate them all.

00:55

All right, so those bits are going to get moved over, and then in their place, you're gonna be the zeros.

01:00

I don't normally you'd arrange it in groups of four, but I was just showing how those bits get shifted.

01:06

If we move the other way. We can see that

01:08

these bits here.

01:11

Sorry.

01:14

Come on. I later

01:15

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

01:34

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.

01:48

All right, so let's start with an example. So we're gonna move into a X, the number C 123 in hex.

01:55

Right. So you should be able, Teoh. I gave you the table here. You should be able to convert that.

02:00

All right, if we look up in the table, we see that, See? Is 1100

02:05

one is 00012 is 0010 and three is

02:12

0011

02:14

So we go ahead and write those numbers down.

02:16

And then here were clearing the carry flag so that way it is ready for us to do our operations.

02:23

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

02:36

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.

02:45

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,

02:52

and our careful, I got set toe one, and we can see the at this end, right? We added another zero at the end.

02:59

Now, if we shifted back to the right by one,

03:04

we can see that all of these numbers got shifted over and we filled the top in with a zero.

03:09

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,

03:17

but we lost that top bit, right? So I got put into the carry flag and then to bust to never return

03:23

right in here. We see that the zero was what got popped off so are careful and gets set to zero.

03:32

And then lastly, we shift to the right by three. So we add three zeros here at the top,

03:38

right? So the things that fall off are gonna be this right here. This 011

03:45

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.

03:55

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.

04:02

And then we're gonna shift that value and print off the result. And we'll also do a couple of registered dumps

04:09

at the same time

04:12

so we can see I'm in my projects directory.

04:19

Go ahead and go in there.

04:26

Now for this one. I'm going to want to print off a couple of prompts.

04:30

That way I can tell the user what we want to do.

04:33

So we'll go ahead and define those.

04:45

Go ahead and terminated with a zero.

04:54

As for the amount to shift,

04:59

put a colon, make it look nice.

05:02

All right. So I got my prompts here.

05:06

So to print those movie X prompt

05:12

call the function to print it

05:17

and then read a value from the user.

05:21

Okay, so now that values in the A X and I'm gonna end up moving that somewhere else, I'll go ahead and

05:28

move that into laissez ebx.

05:33

I need to print off my second prompt.

05:43

Okay,

05:45

Read my number,

05:50

and then I'm gonna move that into

05:57

into easy x.

05:59

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

06:10

and I'll go ahead and move into the X

06:14

the value I stored up here, which was an e b X.

06:16

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

06:25

dump rags one,

06:29

we'll do our shift. So shift right of yea X and we'll just you see l in order to do the shifting

06:39

dumper registers again

06:42

and then we'll print off our number. So just call print and

06:47

and print a new line

06:50

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,

07:01

all right at all. It just complained about my clock being off,

07:06

so we'll run our program. So let's enter the numbers so we can enter zero x a.

07:15

And if we want to shift it by one bit

07:20

so we can see that a A was what we had. And so that got shifted

07:25

to the

07:27

right by one bit. Right? So it turns out to be 55

07:33

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

07:45

16

07:46

and we'll shift it by two or two. Squared is four.

07:50

So our numbers should be four, right and 16 in hex is 10

07:57

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,

08:05

and we could modify this

08:07

hope.

08:09

I don't want to modify the binary.

08:11

We could modify it. So instead of doing a shift right, we could do a

08:16

shift left. So now we would be multiplying instead of dividing.

08:24

So if I have the number 207

08:28

and I want to multiply by two, Aiken shifted by one. And so I got 414

08:37

so that was our example.

08:39

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

08:48

and getting to see where the carry flag gets set or not get set and how it gets determined, especially when we

08:54

rotate our shift a bunch of bits out.

08:58

Next time we'll talk about arithmetic shifts and actual rotation and carry. And how that works.

09:05

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.

### Up Next

### Assembly

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

### Instructed By

Matthew Miller

Instructor