Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 12.10 of Assembly.
Today we're gonna talk about conditional execution with flags and instructions.
Arm conditional execution.
So what is conditional execution? Dio. So it allows an instruction to be executed on Lee when a condition is met,
and then it's based on when flags are set. So we have compares or tests or even add addition and multiplication.
And then we have conditional instructions and we have the i t block.
So here's an instruction ad with without carry, right?
And so if we have the s on it or add dot s ads, then the conditional flags they're going to be set in the, um,
And so that way, when we goto execute another instruction, we can check and see if any of the flags have been set.
So here's an example.
So we can see that we have, um, currently in the CPS are we have the zero flag and the carry flag,
and then we're going to basically clear our flags with the MSR
so we can see that the flags that carried
And then when I coded this, I said
movinto are zero negative one. But when you go into Buggit, it's do doesn't move an invert. So when you invert the number zero, you end up with negative one.
And so that's gonna copy negative one into our zero.
All right, so we don't have any flag set,
and then we have to do an ad. S R zero r 01 So r zero has negative one. And if we add one, it will end up having the number zero. And so then the zero flag is going to get set.
So we got zero. And then additionally, we also had a carry fly get set.
So now are zero has three number one in it or zero in it. And if we do add again, the number one will end up with the number one in our zero,
and you can see that the flags
got taken away. So these instructions, like add s end up modifying our flags just like our
tests and our compare instructions did.
So We have several different flags that we can look at. We got the end flag for negative, so the result was negative
zero. Did the result yield zero? So again, when we added one to negative one. Our result was zero.
Um, was there carry or C? And that is was a result greater than 32 bits. So did it take more than 32 bits to store?
And then we have the for overflow, and then it's a sign result over 31 bits. So if assigned Result gets over 31 bits, then it's going to overflow into the sign flag and then mess things up. And so that's something that we can also test when we're dealing with,
um, numbers that air a positive or negative or assigned
so instructions we have conditional is that we can optionally add to a lot of different instructions
and these condition ALS specify which flags we care about so we can use the condition ALS and then Onley execute instructions when the correct set of flags are set.
And then the reason that we have this is one reason is that we can run faster than branches.
So if we think about it when we do a branch instruction, we have to flush the pipe line that they have on the processor, and this flushing can take approximately three cycles.
and so if you can avoid doing a branch, then you can save some cycles when you are programming for it. And so that's why conditional execution was was created. It makes it a lot faster than doing the branches, and we don't have to flush our pipeline. We can just keep executing.
And then we have a variety of different condition. ALS and flags
eso. Typically, you'll see the suffix like e que or not a key equal or not equal. Or if you're looking at something that's negative or positive, Um, and typically the ones most programmers we're going to use our,
um, less than greater than equal toe less than greater than less than equal to a man. You probably won't use the A l
So here we again move negative one into our zero we add on set the flags are zero R zero, the number one And so we have some additional qualifications on our move instructions so we can say move and then e que or move not eat equal.
And so here we have the zero flag set. So it's has taken reasons E. So the Z flag was set. When we do this movie queue
because when we added one to negative one, the result was zero.
But if we look at the next one,
we can see that we do move, not equal. So that's command is not going to get executed. So are one is not going to get the value of our two.
And then here we do another ad where we said it
So we can see when we executed this are one had the value of one. So this instruction got executed and this one did not.
And it just depends on how you program it as to which instructions would get executed or not get executed.
So here's another example.
So we basically, um if the user types in
the number 10 the results is gonna be 20. It's a tape. In a number, greater than 10 will get 100.
And every time something less than 10 we're going to get zero.
And so this program goes through and reads the number that the user print user enters. So calls read int. We jump into this function called bar, and then we basically do a compare of R zero in the number 10
and then we can have three different instructions based on this, right? So move equal, move less than move Greater than
so only one of these instructions. Because you can't be any two of them on Lee. One of these is going to get executed.
So the program counter will just go over each one and it'll check the flags and say, Okay, I did the compare, and then it will look at the flags and decide
which one of these it's going to actually execute. And so when you run this, then it will either do one of those three things.
So today we talked about conditional execution.
Um, what flags were that we could use and then instructions and adding the different the additional suspects is to make instructions be conditional.
So in the future, we're gonna talk about a conditional execution example, and then we're also going to talk about I t blocks.
So if we got move, are one the number one. Move our tooth. The number eight compare R one to r two.
Um, how would we move one into our zero if the value is negative.
So go back and look at your sheet and see if you can figure that out.
So move. M I
or for negative are zero. The number one.
If you have questions, you can email me Miller MJ at you and Kate, I e t you. You can find me on Twitter at Milhouse 30.