13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 11.4 of Assembly.
Today we're gonna talk about branching and using an if or a while and then shifting.
So branching is the branch instruction. It starts with a B,
and so you can do a regular branch or just a jump to an address. You can do a branch with Link,
so that will save the link. The address for the next location. You're gonna go to into the link register,
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.
We also have conditional branches, just like we had conditional jumps.
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.
He's over here on the left hand side are probably the ones that are most often used. But there's also,
um, ones, for example, minus or plus, um, that we can use.
And so you just have to choose based on your application. What type of conditional brains you want to use.
So here's an example. So
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.
I followed by a new line
and so we can see that we are calling or branching with a link the read int
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
and so if they are equal it jumps down here to the
seven where it prints off the string seven.
And then if they're not equal or any other case, it will jump over here to the not seven.
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
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,
so this isn't necessarily needed,
but it was added in there just for clarity. If you had additional code that you might want to do or an additional
comparison, this will give you a good template in order to start that.
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.
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,
So that's going to read in a number from the user,
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,
um, do this code here, right? So while r r zero is bigger,
so when it's all done, then we'll exit. And so here we go ahead, and we
do in addition. So we're adding R one and r two together, and then we're storing it in our one
and then we're implementing our twos are too is our counter.
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.
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.
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.
All right, shifting.
So we have, Ah,
a variety of different shifts. We again have seen a lot of this in x 86
but we have sort of a different notation again, the constant.
So we have our destination register,
we have the one holding the operandi or to the thing that you want to shift.
And then we have possibly either a source or a constant, and so we can shift by logical shift left
by a another register or constant.
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
and then we have the rotate, so rotate right. We can rotate either by a register or by a particular amount,
and then we have the barrel shifter.
So the barrel shifter allows us to shift a particular number of bits,
Um, in another operation.
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.
And this is basically that such that weekend either multiply or divide by powers of two,
and we can do some rotation of bits.
And then, if you look, barrel shifters actually used for floating point arithmetic units
on the back end Floating point might use this in order to align different numbers.
So here's an example of a barrel shifter.
So, for example, we can move a couple constants here into some registers,
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
right and that's basically multiplying it by two.
Or I can take our zero multiply by or shifted by two, which is to do the two, which is four.
So I can multiply our zero by four and then store the result in our one.
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.
So I take are zero and I multiply it by
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
where we don't have to just do, move, weaken, do add. So here we're adding them together.
So are zero plus R two r two is in left, shifted by three
So two to the three, which is eight.
So that gets, um shifted and then had added to our zero The result is in stored in our one
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.
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,
um, toe load a constant in there. So we have a B, B, C, C, D d.
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
all of these bits got shifted over and then the d wrapped around
and then we can also do it for the, um
we can rotate to the right eight. So rotate eight bits, which would be to hex digits. And so there
the values getting shifted over.
So today we talked about branching, um, looking at a if statement in a wild statement. And then we looked at
shifting and barrel shifting.
So in future lectures, we will give an example of the barrel shifter we'll do that in code.
We'll talk about debugging and memory offsets and then arm indexing for raise.
So here's our quiz.
So if we compare our Zeron are one, how would we branch if the first register is less than the second register?
So we can say branch less than other?
And this would be some label that we had. Brain Djetou.
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.
How to Use GDB (BSWJ)
In this part of the Breaking Stuff with Joe (BSWJ) series, Joe Perry demonstrates how ...
Certificate of Completion Offered
How to Use IDA (BSWJ)
The IDA is used throughout the IT and cybersecurity industries by exploit developers, vulnerability analysts, ...
Certificate of Completion Offered