Conditional Execution Example

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:02
Hello. This is Dr Miller, and this is episode 12.11 of assembly.
00:09
Today, we're gonna talk about conditional execution, and we're going to do an example.
00:13
So conditional. Execution example. So what's our problem? To be solved.
00:18
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.
00:29
All right, so the top will have a couple messages so
00:34
big
00:42
telling them it's too big, small,
00:55
and then
01:10
all right,
01:11
and then we'll have one more message.
01:15
It's as though ask them for a number
01:27
from 0 to 20.
01:30
Parents should give him. You can change this range if you want,
01:36
but you don't have to.
01:38
All right.
01:40
So a couple things that we're gonna dio um, so we're gonna use the time function s rand and then rand,
01:48
so the time function tells us what time it is. According to the processor,
01:52
the S Rand initialize is our random number generator, and then the rand function will give us a random number.
02:00
So we'll just go ahead and call those. So we just call time
02:07
s rand
02:10
rand
02:13
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
02:27
is equal to
02:29
a mod be,
02:30
and that's the same as C is equal to
02:35
a minus. Bi
02:38
times
02:40
a divided by B.
02:44
So this is just a bunch of math so that weaken end up doing mod.
02:47
So I'm going to go ahead and do the math. You don't have to try and replicate it, but
02:54
this is just taking some well known formulas and converting them into assembly. So we're gonna move into our one,
03:00
and here you can change your number. So I'm using the number 20
03:06
and then I'm using you div.
03:08
Storing the resulting R three r zero r one.
03:14
I'll put some comments in here a divided leg
03:17
a divided by B
03:22
and then more are for as a result
03:28
are one
03:30
and our three.
03:36
This is B times
03:38
a divided by B.
03:40
All right,
03:42
and then we do sub.
03:45
Our eight
03:46
are zero in our four.
03:52
So are eight has our number.
03:55
So this is our random number that we're gonna have,
04:00
and then here, we're going to start off and we will use move into our nine,
04:05
the number zero.
04:09
Okay,
04:11
so then we have our loop. Here is the top of our loop
04:15
at the bottom. We're gonna do a
04:17
branch, not equal
04:20
top.
04:24
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.
04:30
So, first, we will ask the user for our number load register are zero our message
04:38
asking them to enter a number.
04:41
Well, then print F
04:45
and then we'll call.
04:46
Read it.
04:49
All right. So now are zero has our number that we are looking at
04:55
and we want to compare it. So are eight has our random number that we chose up here.
05:00
So we're gonna do Ah, compare
05:01
are zero or eight.
05:05
Okay.
05:06
I don't put a comment. Just so we see it are zero
05:12
is our number,
05:15
our eight,
05:16
because our random number
05:19
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
05:38
the same message, right? So it says that there are the same.
05:42
We could also do load register greater than
05:49
it's too big
05:51
load register less than
05:59
small.
06:00
And then I'm gonna put this up here cause they're kind of along the same lines.
06:03
What I'm basically gonna do is set a flag to tell myself. Oh, by the way,
06:09
we are done. So we'll just do a
06:14
move
06:15
equal
06:18
o r. Nine
06:21
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.
06:30
So then down here, Well, first of all, will Procol a print off
06:36
and then
06:39
we will, um,
06:43
compare
06:44
are nine to the number one.
06:47
So if it's not equal to the number one that it will jump back up here to the top, print off our message,
06:54
read the number from the user and then compare our number
06:59
with the random number that we were given.
07:08
All right, let's see what we did.
07:12
So I think one of the things we forgot to do is move into
07:16
are zero the number zero for our time function to tell us to give it the current time.
07:27
So now we get to enter a number. So let's say if I'm picking just
07:31
terribly, I can say 12345
07:36
or I could start in the middle. 10 10 is too small. 15 15 is too small.
07:44
18 is too big 17 just right,
07:48
and each time you run it, it should give you a different numbers. So let's again do. 10. 10 is too small. 15
07:56
15 is too big.
07:57
13
08:00
12 11
08:05
So we can see inside of here that we had our conditional execution that allowed us to dio
08:11
four different instructions based on whatever flags that we cared about for equal, greater than or less than
08:20
so we We didn't have a bunch of comparison jumps,
08:28
so today we solved the problem of guessing a number using conditional execution.
08:33
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.
08:43
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.
Up Next