Video Transcription

00:01

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

00:06

Today we're gonna talk about branching and using an if or a while and then shifting.

00:13

So branching.

00:15

So branching is the branch instruction. It starts with a B,

00:19

and so you can do a regular branch or just a jump to an address. You can do a branch with Link,

00:25

so that will save the link. The address for the next location. You're gonna go to into the link register,

00:31

and then we also have the branch in exchange. So it exchanges the link register in the program counter, so that will allow you to go back where you came from.

00:42

We also have conditional branches, just like we had conditional jumps.

00:47

And so the ones you'll probably most often use are equal, not equal, greater than greater than or equal to less than or equal to.

00:56

He's over here on the left hand side are probably the ones that are most often used. But there's also,

01:00

um, ones, for example, minus or plus, um, that we can use.

01:07

And so you just have to choose based on your application. What type of conditional brains you want to use.

01:15

So here's an example. So

01:18

these air the strings that were defined at the top. So we have a and we have be so a says not seven and B, says seven exclamation point.

01:26

I followed by a new line

01:29

and so we can see that we are calling or branching with a link the read int

01:36

ah function.

01:37

And then we are doing a We do a move of the number seven in tow are one, and then we compare our one toe are zero

01:47

and so if they are equal it jumps down here to the

01:51

seven where it prints off the string seven.

01:53

And then if they're not equal or any other case, it will jump over here to the not seven.

02:00

And so here. This is implementing a nice if else. So. If it's seven, do this code right here. If it's not seven, do this code down here

02:08

and then, as we've seen before at the end, right, we do a branch to the end for each one of these was this is a little redundant right branch to end where end is right here,

02:20

so this isn't necessarily needed,

02:23

but it was added in there just for clarity. If you had additional code that you might want to do or an additional

02:30

comparison, this will give you a good template in order to start that.

02:37

And then we can have ah, while loop. Right. So, in arm, we don't have the loop command like we did in X 86.

02:44

And so we can see here again. We're reading a number. I'm and in this example we have a summon encounter. So the sun starts off at zero. The counter starts at the number one and then the counter where if we remember branch with ingredient,

02:58

So that's going to read in a number from the user,

03:01

and then that'll get stored in R zero. And so this is where we're going to end. So while our two is less than r zero, we're going to,

03:13

um, do this code here, right? So while r r zero is bigger,

03:20

so when it's all done, then we'll exit. And so here we go ahead, and we

03:23

do in addition. So we're adding R one and r two together, and then we're storing it in our one

03:30

and then we're implementing our twos are too is our counter.

03:34

And then when we get down here, we come back up to loop and then and do a comparison of r zero and are too.

03:40

And then we're all done, are some was in our one. And so we just copy that into our zero so that we can use the print into function in order printed off.

03:50

So this is gonna go multiple times until the condition is met such that it'll exit. So that's a implementation of a while loop.

03:59

All right, shifting.

04:01

So we have, Ah,

04:02

a variety of different shifts. We again have seen a lot of this in x 86

04:08

but we have sort of a different notation again, the constant.

04:14

So we have our destination register,

04:16

we have the one holding the operandi or to the thing that you want to shift.

04:19

And then we have possibly either a source or a constant, and so we can shift by logical shift left

04:27

by a another register or constant.

04:30

Um, and we can do a shift right. We can do with arithmetic shift. So we make sure that we keep the sign. Remember, sign numbers. The left most bid is a one if it's a negative number. And so if you do an arithmetic shift to make sure you keep that s so that the number doesn't go from being negative to being positive

04:51

and then we have the rotate, so rotate right. We can rotate either by a register or by a particular amount,

05:00

and then we have the barrel shifter.

05:01

So the barrel shifter allows us to shift a particular number of bits,

05:06

Um, in another operation.

05:11

And so, generally with the barrel shifter, we can either do a shift or weaken Jewish or are rotate. And again, these are just moving some bits around so that they don't change.

05:21

And this is basically that such that weekend either multiply or divide by powers of two,

05:27

and we can do some rotation of bits.

05:30

And then, if you look, barrel shifters actually used for floating point arithmetic units

05:35

and so

05:36

on the back end Floating point might use this in order to align different numbers.

05:43

So here's an example of a barrel shifter.

05:46

So, for example, we can move a couple constants here into some registers,

05:51

and so bear a shifter allows us to say I'm gonna take are zero, and I'm going to do a left shift by one. And then I'm gonna store that result in our one

06:02

right and that's basically multiplying it by two.

06:05

Or I can take our zero multiply by or shifted by two, which is to do the two, which is four.

06:13

So I can multiply our zero by four and then store the result in our one.

06:18

Here's an example where you can do it with another register so I can do a left shift of our nine Cygan cr nine. I moved the number 10 in it.

06:28

So I take are zero and I multiply it by

06:32

to to the 10 or 1000 24 then store that result in our one so we can see we're doing both a shift and a move

06:40

where we don't have to just do, move, weaken, do add. So here we're adding them together.

06:45

So are zero plus R two r two is in left, shifted by three

06:49

So two to the three, which is eight.

06:53

So that gets, um shifted and then had added to our zero The result is in stored in our one

07:00

and here we can do a shift right and we can do a subtract so r zero minus are too shifted to the right or divided by 16.

07:11

Additionally, we could do some rotations. So here's an example of how toe load a 32 bit constant we can use the load register,

07:17

um, toe load a constant in there. So we have a B, B, C, C, D d.

07:23

And so we can take that number, which is in R zero, and we can rotate it to the right by 44 bits and so that in our one and so then the result would be D A B B C c d. Right. So

07:39

all of these bits got shifted over and then the d wrapped around

07:46

and then we can also do it for the, um

07:49

we can rotate to the right eight. So rotate eight bits, which would be to hex digits. And so there

07:56

the values getting shifted over.

08:01

So today we talked about branching, um, looking at a if statement in a wild statement. And then we looked at

08:09

shifting and barrel shifting.

08:13

So in future lectures, we will give an example of the barrel shifter we'll do that in code.

08:18

We'll talk about debugging and memory offsets and then arm indexing for raise.

08:24

So here's our quiz.

08:24

So if we compare our Zeron are one, how would we branch if the first register is less than the second register?

08:35

So we can say branch less than other?

08:37

And this would be some label that we had. Brain Djetou.

08:43

If you have questions, you can email me Miller MJ at you and Kate I e d you or find me on Twitter at No House 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

Similar Content