13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 4.2 of Assembly.
Today, we're gonna be talking about the if translation looping and then a an example of the if and loop altogether.
So the if in the loop.
So from our template, we have the example that we can do a compare on, for example, one register and another one
and then in order to
jump so we can jump over some code. And that is the code in here for the negation. So we can say jump zero label A. So if they're equal, it will jump down here and then do the code when they're equal.
And then I don't continue on through the end label.
But if they're not zero, then we can execute some code where they are not equal, and then we can jump to the end.
And so that's an example. If you have a high level language, you can implement an if statement or an if else statement
using this template here.
Additionally, we have the loop construct. So we talked about that in the previous lecture where Decker, Ament, ccx, and then it checks to see is that zero if it's not, zero will jump back to the top or the label dis listed inside of the loop.
And then we'll give an example here of a bad loop of something that students often do when they're doing coding. So let's do that example.
So I've already created a directory.
So I'm going to go ahead and edit that and let's just do a really simple program. So, for example, we're going to call read int
to read a number from the user,
and then I'm gonna copy that in D. C. X so I can do a loop on that number.
So remember, the result of Re Dent is an e X saw. Copy that TC X, and then I'll create a label for the top of my loop
so I can go back
Now. What I'd like to do is just print off this number
so I can copy that back into
into the variable where we can print something so yea X and I'll copy the value of V C. X. So what? My loop counter is at this time,
and then I'll call print
print New line. So it'll print a new line at the end.
So if we run our program when we type the number 10 in,
it'll print off. So I typed the number 10. It printed 10 987654321
What happens if you do? Zero.
So as you can see, it all had control, seed and kill that.
So what happened is it did a deck Ament on our register.
Right? So it does a deck Ament of EEC ex Soviet zero. The number becomes negative. One.
Well, it doesn't actually know that that's a negative number. Edges thinks that it's a really big number, and so it'll basically loop. So as we can see, when we ran this program, it was looping forever because it went into
negative numbers and see if I
Yes, so it went to it, started off at negative one negative to negative three right and just kept going down. So just kept
decreasing that negative number forever until I hit control C.
So if you're writing a loop, you want to make sure one of the things you don't do is start off with the number zero because then the loop will decker mint and it will basically look forever, or at least a loop for a really long time, which is probably not what you intended.
All right, so let's do another example. So we're gonna read the numbers from the user, and then we're going to print prompts for each of the inputs and print prompts for the outputs
and then at an additional case, that weaken, stop if they enter the number negative one.
So let's create a new project.
So I mean, the projects directory. I should see my new project here.
All right, so the first thing that I want to do is I want to print a prompt for the user to enter something.
I need some prompt text. So I'm going to call this
prince thing method to print our string,
but we don't have a string, so we need to create one. If you recall, we can put in the data section are prompt.
We can call it whatever we want, but I'm gonna call it prompt,
and they'll say
enter the number.
I like putting a colon because it kind of shows where they should type something.
I'm gonna have ah, couple other problems. So we'll get to those as we as we get in there.
So we move our prompt in t a X we call print shrinks and out prints that string from the user. And then we want to read something from News is so we'll call, read,
And then again, we're going to use S E X for our
loop. So I'm gonna copy
into E c x the value of V a X. This is how maney numbers I want to read. So if they type five, I want to read in
five different numbers.
Additionally, I'm going to use EBX for these the sum of all my numbers. So I'm gonna add up all the numbers that they have typed.
So I'll go ahead and zero that out.
They don't have the top of my loop,
and at some point here, I'll have
looping back to the top.
So now what I want to do is I want to read a number from the user.
So again, I want to print another prompt asking them to enter the number. So let's go up here.
We'll add another prompt.
I'll call it enter
again, putting a colon to make it look nice
and putting a zero to make sure we terminated if we don't will get a bunch of garbage. So that would probably not be good
again to print off a string we move into Yea xar prompt
which is actually called enter
and I'll put see let's put enter
enter is a reserved words So
Okay, so we
put out prompt into yea X call prints showing. So now prince it. And now we want to read the number from the user.
So we call reading. So that'll read a number from the user.
And then we were gonna want to add that eventually too
E b x right. And the result of read int is gonna have our value in the X.
And so we go ahead and add that to our current some, which is in EBX.
But we also wanted to have the ability to say if they entered the number negative one.
Then we want to exit the loop right away.
I'll do a compare
jump of it. Zero to done so done is going to be something that's going to come down here,
and this is where I'm going to go ahead and print off the some.
So down here I'm going again. Print another string, so
I'll call my string some.
So now I got to go to find my variable called some at the top,
printing off on my total
again putting in zero to make sure I terminate it.
so I print the string and then EBX should have my some and so to print an inch, I gotta move that back into yea X
and then print into a print. Whatever is any axe
and print, New Line will print a new line at the end.
So that's my program.
We'll make sure it compiles here.
I didn't have any heirs,
and so now when I run, it
does how many numbers you want and I'll say four.
So enter four numbers,
adds them up. That should be 10.
If I say I want 100 numbers and I type 10 2030
a negative one tells it that you want to stop. And so I've got the numbers going to 30.
No. One thing, I'll point out. So when we get later on and you're gonna end up calling functions like
print int or print F.
Um, some of those will actually modify E c X
and so that will break your loop. So you go along, you're writing something you call print F of all of a sudden you're loop will stop working. And that's because print f modified e c X. We'll get to that later. But it's just ah, helpful hint to know that sometimes
things will change and registers get modified, and easy X is one of them that can get modified and you don't realize it. And then all of a sudden you're code doesn't work anymore.
Arts of Today We showed an example of doing some loops and if statement and then kind of putting them to get her together in one large example,
looking forward, we're gonna talk about doing shifts and rotations.
If you have questions, you can contact me at Miller MJ at you and Kate, I E. D U And on Twitter at No House, 30
How to Use GDB (BSWJ)
In this part of the Breaking Stuff with Joe (BSWJ) series, Joe Perry demonstrates how ...
Certificate of Completion Offered
How to Use IDA (BSWJ)
The IDA is used throughout the IT and cybersecurity industries by exploit developers, vulnerability analysts, ...
Certificate of Completion Offered