### Assembly

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14

### Video Transcription

00:00
Hello. This is Dr Miller, and this is Episode 4.2 of Assembly.
00:08
Today, we're gonna be talking about the if translation looping and then a an example of the if and loop altogether.
00:19
So the if in the loop.
00:22
So from our template, we have the example that we can do a compare on, for example, one register and another one
00:30
and then in order to
00:31
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.
00:45
And then I don't continue on through the end label.
00:48
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.
00:56
And so that's an example. If you have a high level language, you can implement an if statement or an if else statement
01:04
using this template here.
01:08
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.
01:25
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.
01:34
So I've already created a directory.
01:45
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
01:53
to read a number from the user,
01:57
and then I'm gonna copy that in D. C. X so I can do a loop on that number.
02:04
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
02:15
so I can go back
02:17
Now. What I'd like to do is just print off this number
02:21
so I can copy that back into
02:27
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,
02:36
and then I'll call print
02:38
and
02:39
followed by
02:43
print New line. So it'll print a new line at the end.
02:51
So if we run our program when we type the number 10 in,
02:54
it'll print off. So I typed the number 10. It printed 10 987654321
03:01
What happens if you do? Zero.
03:06
So as you can see, it all had control, seed and kill that.
03:08
So what happened is it did a deck Ament on our register.
03:14
Right? So it does a deck Ament of EEC ex Soviet zero. The number becomes negative. One.
03:20
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
03:32
negative numbers and see if I
03:37
Yes, so it went to it, started off at negative one negative to negative three right and just kept going down. So just kept
03:44
decreasing that negative number forever until I hit control C.
03:49
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.
04:06
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
04:16
and then at an additional case, that weaken, stop if they enter the number negative one.
04:23
So let's create a new project.
04:32
So I mean, the projects directory. I should see my new project here.
04:48
All right, so the first thing that I want to do is I want to print a prompt for the user to enter something.
04:54
So
04:57
I need some prompt text. So I'm going to call this
05:02
prince thing method to print our string,
05:06
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.
05:14
We can call it whatever we want, but I'm gonna call it prompt,
05:17
and they'll say
05:19
enter the number.
05:28
I like putting a colon because it kind of shows where they should type something.
05:33
I'm gonna have ah, couple other problems. So we'll get to those as we as we get in there.
05:39
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,
05:46
hint.
05:48
And then again, we're going to use S E X for our
05:53
loop. So I'm gonna copy
05:57
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
06:03
five different numbers.
06:04
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.
06:15
So I'll go ahead and zero that out.
06:17
They don't have the top of my loop,
06:20
and at some point here, I'll have
06:26
looping back to the top.
06:28
So now what I want to do is I want to read a number from the user.
06:32
So again, I want to print another prompt asking them to enter the number. So let's go up here.
06:39
06:47
I'll call it enter
06:54
again, putting a colon to make it look nice
06:58
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
07:04
again to print off a string we move into Yea xar prompt
07:11
which is actually called enter
07:15
and I'll put see let's put enter
07:18
prompt
07:20
Make sure
07:21
enter is a reserved words So
07:31
Okay, so we
07:33
put out prompt into yea X call prints showing. So now prince it. And now we want to read the number from the user.
07:45
So we call reading. So that'll read a number from the user.
07:48
And then we were gonna want to add that eventually too
07:54
E b x right. And the result of read int is gonna have our value in the X.
08:01
And so we go ahead and add that to our current some, which is in EBX.
08:05
But we also wanted to have the ability to say if they entered the number negative one.
08:09
Then we want to exit the loop right away.
08:13
So
08:13
I'll do a compare
08:18
and then
08:20
jump of it. Zero to done so done is going to be something that's going to come down here,
08:28
and this is where I'm going to go ahead and print off the some.
08:33
So down here I'm going again. Print another string, so
08:39
I'll call my string some.
08:43
So now I got to go to find my variable called some at the top,
08:58
printing off on my total
09:01
again putting in zero to make sure I terminate it.
09:07
Okay,
09:09
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
09:18
and then print into a print. Whatever is any axe
09:24
and print, New Line will print a new line at the end.
09:31
So that's my program.
09:33
We'll make sure it compiles here.
09:37
I didn't have any heirs,
09:39
and so now when I run, it
09:43
does how many numbers you want and I'll say four.
09:46
So enter four numbers,
09:50
adds them up. That should be 10.
09:52
If I say I want 100 numbers and I type 10 2030
10:00
negative one,
10:01
a negative one tells it that you want to stop. And so I've got the numbers going to 30.
10:09
No. One thing, I'll point out. So when we get later on and you're gonna end up calling functions like
10:16
print int or print F.
10:18
Um, some of those will actually modify E c X
10:22
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
10:41
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.
10:54
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,
11:03
looking forward, we're gonna talk about doing shifts and rotations.
11:09
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

### Assembly

This course will provide background and information related to programming in assembly. Assembly is the lowest level programming language which is useful in reverse engineering and malware analysis.

### Instructed By

Matthew Miller
Assistant Professor at the University of Nebraska at Kearney
Instructor