# Conditional Execution

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 12.10 of Assembly.
00:06
Today we're gonna talk about conditional execution with flags and instructions.
00:11
Arm conditional execution.
00:15
So what is conditional execution? Dio. So it allows an instruction to be executed on Lee when a condition is met,
00:21
and then it's based on when flags are set. So we have compares or tests or even add addition and multiplication.
00:30
And then we have conditional instructions and we have the i t block.
00:35
So here's an instruction ad with without carry, right?
00:40
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,
00:51
system.
00:52
And so that way, when we goto execute another instruction, we can check and see if any of the flags have been set.
01:00
So here's an example.
01:03
So we can see that we have, um, currently in the CPS are we have the zero flag and the carry flag,
01:11
and then we're going to basically clear our flags with the MSR
01:17
so we can see that the flags that carried
01:19
And then when I coded this, I said
01:22
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.
01:32
And so that's gonna copy negative one into our zero.
01:37
All right, so we don't have any flag set,
01:38
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.
01:53
So we got zero. And then additionally, we also had a carry fly get set.
01:57
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,
02:07
and you can see that the flags
02:08
got taken away. So these instructions, like add s end up modifying our flags just like our
02:16
tests and our compare instructions did.
02:21
So We have several different flags that we can look at. We got the end flag for negative, so the result was negative
02:27
zero. Did the result yield zero? So again, when we added one to negative one. Our result was zero.
02:35
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?
02:44
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,
02:59
um, numbers that air a positive or negative or assigned
03:04
so instructions we have conditional is that we can optionally add to a lot of different instructions
03:10
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.
03:22
And then the reason that we have this is one reason is that we can run faster than branches.
03:28
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.
03:38
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.
03:54
And then we have a variety of different condition. ALS and flags
03:59
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,
04:14
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
04:24
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.
04:44
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
04:54
because when we added one to negative one, the result was zero.
04:58
But if we look at the next one,
05:00
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.
05:09
And then here we do another ad where we said it
05:12
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.
05:19
And it just depends on how you program it as to which instructions would get executed or not get executed.
05:29
So here's another example.
05:31
So we basically, um if the user types in
05:36
the number 10 the results is gonna be 20. It's a tape. In a number, greater than 10 will get 100.
05:43
And every time something less than 10 we're going to get zero.
05:46
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
06:00
and then we can have three different instructions based on this, right? So move equal, move less than move Greater than
06:06
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.
06:15
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
06:24
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.
06:33
So today we talked about conditional execution.
06:36
Um, what flags were that we could use and then instructions and adding the different the additional suspects is to make instructions be conditional.
06:46
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.
06:56
So if we got move, are one the number one. Move our tooth. The number eight compare R one to r two.
07:02
Um, how would we move one into our zero if the value is negative.
07:08
So go back and look at your sheet and see if you can figure that out.
07:15
So move. M I
07:16
or for negative are zero. The number one.
07:23
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.
Up Next