Compare, Conditionals and Jumps
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 »

Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 4.1 of Assembly.
00:07
Today we're gonna talk about control structures and looping
00:11
control structures.
00:13
So control control structures allow us to create a system that is Turing complete. And that means that we have the ability to do selection.
00:21
And both high level and assembly level language contain the ability in order to do selection.
00:28
So how do we do it? We use a couple different commands. One is compare. So competitive takes register one and register to, and it does subtraction on them.
00:38
And so we take register one minus, register to, and then we go ahead and set the flags. So flags say what happened in the previous operation that occurred? And so if the Kerry fly gets set, that means it had to carry when it did subtraction. The overflow flag means that
00:55
we didn't have enough room and so we had to overflow into another location.
01:00
The zero flag means that
01:02
the result was zero. So register one and registered to are equal in that case
01:07
and then for our operations, it doesn't matter the order, but one of them has to be a register
01:11
so either this one or this one or both can be a register and the other one can be either a memory address which we'll talk about later, or an immediate or ah, hard coded value.
01:26
So if register one and registered to are equal, then the zero flag will get set. And we can use that in order to tell if two things are equal.
01:33
If register one is greater than registered to than the zero flag is zero and they carry flag is zero.
01:40
If register one is less than registered to than the zero flag is zero and the carry flag is one meaning that I carry
01:48
flag got set because borrow had to occur.
01:53
So along with conditional XYZ, the ability to do jump it jumping around or branching using those flags got set
02:02
and we have two different kinds of jumps. So one is called the unconditional jump jump location. So whenever that instruction occurs, is just gonna go somewhere else.
02:12
And then we have the conditional jump. So Onley jump on a flag is set, so if a flag is set, then you get a jump on the location. Otherwise you're gonna execute the next instruction that comes after the current instruction.
02:23
We've got a bunch of different ones right in. Each of these relates to the flags. So, for example, jump zero jumped, not zero jump, overflow, jump, not overflow. And we got signed carry and then parody.
02:38
So here's a simple example that just checks to see what the user did. So if you look here, we can see that it says call reading. So that's gonna read a number from the user.
02:49
And then it's gonna do a comparison. Compare yaks to 10 again. We put an immediate in here, and then we have a register.
02:57
And so then the flags they're gonna get set. And so zero means that if I took yea X and I minus 10 right, that they're equal,
03:04
You can see that this jump zero is going to go down here to this 10 label.
03:09
So it says if it zero it's gonna jump down here and then execute this instruction down here.
03:15
Otherwise, if it's not zero, it's gonna execute this movie X other,
03:21
and then it will jump down here to the print Notice this This is an unconditional jump, meaning always do that jump
03:28
and up here we have a couple of strings that were declared. So we have 10. You entered 10 or other incorrect exclamation point.
03:36
We can see here that there's a tan in here that's a new line and then a zero byte in order to end our string. And so those are things that we have to do.
03:44
So either way, right, it's gonna movie the either e x, the value of other or e X, a value of 10. And then they both come down here in to do the print string,
03:54
right? So someone do move, move 10 in T A X, and then this is the next instruction after that.
04:00
And so this will allow the user to enter a number. It'll check of its 10 around and then it'll print off the result. Either you enter 10 or incorrect,
04:09
so I think you should go ahead and use your virtual machine or your Lennox machine and enter the code and see if you can get it to run.
04:19
Looping.
04:21
So looping had allows us to do something over and over again, and there's a specific command called loop and so we can loop to some label which looks at E. C. X
04:32
right, and it jumps if it's not equal to zero. So if he see X is not zero, then it goes ahead and does a jump.
04:39
But a deck Ament ccx every time. So this allows you to
04:43
go through multiple times
04:46
and in the next lecture will look at an example using the loop or one of these two commands.
04:51
So we also have loop equal or loop zero. These are the same commands, right? Cause we have the zero flag, and if it zero, then that's what we use. Right? So jumps of E X is not is equal to zero and a zero flag is one or loop, not equal or loop, not zero.
05:11
And so here's an example using the loop command. So here we have the sort of formulaic version of that, and then we could translate that directly and assembly. So we're moving into yea X, the number zero we're moving into e c X the number 10 and then we're gonna add e X and easy X so e c X is 10.
05:30
Add that T a X, which is zero. So now yea x has 10
05:33
and then it's gonna do loop, so it's gonna check is E C X zero. Ready? Does a deck Ament on it checks to see if it zero. If it's not a jumps back up here, right, Does the ad then does the loop again, right? So we do this over and over and over again and tell
05:48
right? You see, X is gonna be 10 and then nine and then eight and 7654321
05:56
zero. Then it will exit the loop.
05:59
So that's an example of how the loop Command can be used.
06:03
So we're gonna do a real example. So we're going to read a number from the user and kind of on similar lines. We're gonna print all of the numbers up to that number,
06:13
So if the user enters seven, we're gonna print 1234567 And I'll put new lines in just so that we can kind of see how it works.
06:24
All right,
06:25
so I've already created my project.
06:32
So the first thing that we're gonna want to do is read the number from the user.
06:40
Now that number is stored in the X and I'd actually like to save it somewhere else because I want to use E X in my
06:47
compare. Er yeah, he X and my comparison and I want to be able to print it off and the only way to print it off really easily is Thebe print int, which requires the value that we're gonna print to be any X. And so I'm going to copy this into EBX.
07:03
So now I have a copy of my number and then I'm gonna set yea x 20
07:12
okay.
07:14
And then in order to do my comparison or to be able to decide when I'm done, I'm going to go ahead and compare
07:21
yea x and ebx.
07:25
And so if they're equal, then I'm done. So I will do jump equal.
07:30
Done now done is a label, which is what we need for jumping. So I'll go ahead and down here and
07:35
put a label called Done
07:38
and all a label is is a string of characters followed by a colon to say the instructions after this starting on this line are called that label.
07:49
All right, so if I'm not done, then what I want to do is I want to print the number that's in the X, So call
07:57
print
07:59
int,
08:01
and then I'll print a new line.
08:05
So I printed
08:07
extra key there. I printed my imager than I printed off a new line. And now what I'd like to do so e X is my number that I'm printing. So I want to make that one bigger or I can say
08:18
increment
08:20
e x.
08:22
And then what I want to do is I want to jump back up here. And so I'm gonna say, Jump,
08:31
I'll just call it to compare
08:33
and I'll put a label here,
08:41
so that way we'll jump up back up to the top now yay! X is one bigger. And so it does a comparison of the a X and EBX. Until they are equal,
08:50
go ahead and write and quit,
09:00
so find them into the number seven
09:03
it prints 036
09:05
So, prince, all the numbers up to that number, but not including that
09:09
you could change it to do it. A couple different versions of this
09:16
Wronki.
09:18
So, for example, if you wanted to start it at the number one
09:24
right, we could go ahead and do that
09:26
and we can do a couple of different versions of this, right? So I'll let you try and guess what they are.
09:33
You can try some examples. So, for example, there's also jump above or jump below jump greater than or equal to. There's a whole bunch. And so since I'm comparing e x t e b x, I maybe want to see if they are.
09:48
Let's see, I want to see if what will try one,
09:52
See if that works.
09:54
So jump. If this one is greater than so maybe
09:58
that one
10:01
See if that works
10:03
and you can always run it
10:07
and it looks like it did. Right? So a kept going until the first. The number we're printing was greater than instead of greater than or equal to.
10:16
And so we're going from the numbers from 1 to 7 is what we're printing off.
10:30
All right, So in summer we talked about control structures and looping, and we gave some examples of how to use both the loop construct and how to build our own loop. Using the compares and the jumps.
10:45
Looking forward, we're gonna do a full on loop example using the loop command, and then we'll talk about rotation and shifting.
10:54
If you have questions, you can contact me at Miller MJ at you and Kate. I e d u. And you can find me on Twitter at Milhouse 30.
Up Next
Similar Content