Conditional Execution Example
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.11 of assembly.
Today, we're gonna talk about conditional execution, and we're going to do an example.
So conditional. Execution example. So what's our problem? To be solved.
So we're gonna write a program that generates a random number, and then we're gonna try and guess the number, and we're going to use conditional execution inside of our program.
All right, so the top will have a couple messages so
telling them it's too big, small,
and then we'll have one more message.
It's as though ask them for a number
from 0 to 20.
Parents should give him. You can change this range if you want,
but you don't have to.
So a couple things that we're gonna dio um, so we're gonna use the time function s rand and then rand,
so the time function tells us what time it is. According to the processor,
the S Rand initialize is our random number generator, and then the rand function will give us a random number.
So we'll just go ahead and call those. So we just call time
and then here we're gonna do a little bit of mass. So what I'd like to do is I would like to get a number between zero and 20 but this is actually just giving you an absolutely random number. And so there's a formula it says see
is equal to
a mod be,
and that's the same as C is equal to
a minus. Bi
a divided by B.
So this is just a bunch of math so that weaken end up doing mod.
So I'm going to go ahead and do the math. You don't have to try and replicate it, but
this is just taking some well known formulas and converting them into assembly. So we're gonna move into our one,
and here you can change your number. So I'm using the number 20
and then I'm using you div.
Storing the resulting R three r zero r one.
I'll put some comments in here a divided leg
a divided by B
and then more are for as a result
and our three.
This is B times
a divided by B.
and then we do sub.
are zero in our four.
So are eight has our number.
So this is our random number that we're gonna have,
and then here, we're going to start off and we will use move into our nine,
the number zero.
so then we have our loop. Here is the top of our loop
at the bottom. We're gonna do a
branch, not equal
So we're going to use a couple different things, and so you'll get to see some of the different conditional executions that we can have.
So, first, we will ask the user for our number load register are zero our message
asking them to enter a number.
Well, then print F
and then we'll call.
All right. So now are zero has our number that we are looking at
and we want to compare it. So are eight has our random number that we chose up here.
So we're gonna do Ah, compare
are zero or eight.
I don't put a comment. Just so we see it are zero
is our number,
because our random number
So what we can do is we're going to load into, um, are zero a different message based on what they do so we can do, load, register, and then add e que to say, loaded of its equal are zero
the same message, right? So it says that there are the same.
We could also do load register greater than
it's too big
load register less than
And then I'm gonna put this up here cause they're kind of along the same lines.
What I'm basically gonna do is set a flag to tell myself. Oh, by the way,
we are done. So we'll just do a
o r. Nine
the number one. And so that's going to say our nine starts at zero saying we're not done. And then we said it toe one to say that we are done.
So then down here, Well, first of all, will Procol a print off
we will, um,
are nine to the number one.
So if it's not equal to the number one that it will jump back up here to the top, print off our message,
read the number from the user and then compare our number
with the random number that we were given.
All right, let's see what we did.
So I think one of the things we forgot to do is move into
are zero the number zero for our time function to tell us to give it the current time.
So now we get to enter a number. So let's say if I'm picking just
terribly, I can say 12345
or I could start in the middle. 10 10 is too small. 15 15 is too small.
18 is too big 17 just right,
and each time you run it, it should give you a different numbers. So let's again do. 10. 10 is too small. 15
15 is too big.
So we can see inside of here that we had our conditional execution that allowed us to dio
four different instructions based on whatever flags that we cared about for equal, greater than or less than
so we We didn't have a bunch of comparison jumps,
so today we solved the problem of guessing a number using conditional execution.
Looking forward, we're going to do conditional execution vs branching and then we'll talk about Indian nous and we'll talk about I t box.
So if you have questions you can contact me, Miller MJ at you in Kedah et you. And you can find me on Twitter at Milhouse 30.