More Complicated Function
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 6.9 of Assembly.
00:07
Today we're gonna do is we're gonna write a function from scratch. And what we're gonna do is we're gonna use parameters.
00:13
The prologue and epilogue. We're going to save registers, and then we're gonna call some external functions.
00:21
So here's our problem to be solved. We want to write a program that prints out multiplication tables for the numbers one through nine, not including 10.
00:30
And we're going to read in the, um,
00:34
how many multiples we want to print. So, for example, if we type three,
00:39
then we'd print off for for the number of, say, five would print off 5 10 and 15. Or if we did four or 5 10 15 and 20.
00:48
And so we're gonna
00:50
we're going to start from the beginning and we will write an entire function and we'll do a minimal main program,
00:59
so I'll start off. I'm gonna define what my format string is for printing off the data. So
01:06
I'll just to find that first since, um, at the top of the file,
01:12
and we'll just put ah, we're gonna use print F.
01:17
So because we're going to use print if we need to define that as an external function,
01:26
okay.
01:26
And then in our main, we're just going to read a number. And we were gonna push it onto the stack because we're going to use parameters.
01:33
And then we got to correct the stack, so we'll call, read,
01:38
and
01:38
we will the value of that distort any X, So push X.
01:45
And then I'm gonna call my function printing. Um
01:51
And then I got to correct the stack so I could either add DSP or I could say,
01:55
um, pop e x Either one works.
02:00
So now we're gonna write our function, so I'll start off
02:02
and give our function of name, which have already said is, friend,
02:07
no.
02:10
Now I'll start off with the prologue, So push E v p
02:15
movie B p E s p will come down here and define my epilogue.
02:23
And I'll put a label here
02:24
something to use that later saying where when I am, I done
02:36
so put in my epilogue, put in my return at the end and I've got my done label.
02:42
All right,
02:43
so we have our parameter on the stack and what we're gonna do is I'm gonna use E b X in order to
02:50
store what I'm incriminating stuff by. So
03:00
it was ebx is gonna count our our number, our initial numbers. Sorry.
03:13
And then we're gonna use E x
03:15
for the multiple to print, and I must start dot off at one.
03:23
And then I'm gonna use e d. X
03:29
as the number that I need to add. And I'm gonna start off zero because I want to increment it inside of my initialization.
03:39
Okay,
03:42
So in my initialization,
03:45
what I want to do is I want to incriminate GDX. So each time through, I wanted to be one higher than what it was before.
03:54
And then I want to use e c x for my loop. So easy X is going to say, How many times am I going to go and print off this number? So it might be, you know, four or 5 10 what have you So I'll use E c x for that,
04:09
and then I'm using any BP based stack. So if you recall,
04:14
PBP plus eight is our first parameter, So that should be the parameter that is up here that's getting pushed onto the stack, right, Because we used our standard prologue.
04:27
And then here I'm going to,
04:29
um, copy into yea x
04:32
the value of e d. X. And so that's going to write. GDX is gonna increment each time, and it's going to say what I'm gonna add to it. And so then each time through it will be the next number in my loop
04:46
and then we'll copy that back. So we've implemented E. D. X copied the incremental version two yea x and then copied that back ttx just to be safe.
04:59
All right, and then inside of here, we want to be able to
05:03
print off our number, so to print our number.
05:06
So the number we're gonna print is going to be any X. So I'm gonna push
05:11
yea x
05:15
push my format that I want to print, which I defined at the top
05:19
call print and F
05:24
And then I gotta fix the stack because I to push two parameters on. I need to fix those. And so we'll go ahead and
05:32
add to yes, p
05:36
eight Mary, because four for each one of those
05:41
soon I'll call print F
05:43
and then when I'm all done right
05:46
each time through my loop
05:47
I want to,
05:49
um, ad
05:51
to be a X, the value that's a needy ex. So if I'm counting my fives, e d x will have five and e x will have five the first time in in 10 and 15 and then 20 and 25.
06:04
And then
06:05
each time through here, I want to loop back up to, um,
06:10
the top, and this is where I'm going to print it again. So it's a loop,
06:14
and then I'll define a variable crop top. And so that's where I'm going to go in print,
06:24
so this will print off. So if we have the number one, it's gonna print off 12345 If five is our maximum,
06:30
and then what we want to do is we want to go now and do 2468 10
06:35
for our example. So we're gonna have another loop, right? So we can't use to loop constructs. We would have to actually implement incremental it ourselves.
06:46
And so to do that, we're using EBX. So ebx is our number that we want to do, so we'll go ahead and increment
06:54
e B X
06:56
and then compare
06:58
E v X to the number, say 10. So I'm using 10. We can we're gonna go up to, but not including 10 in this example. But you could change it
07:09
to meet your needs.
07:11
And then each time through, I will want to print a new lines that I know that I'm done with, say, the multiples of one, then the multiples of two and in multiples of three.
07:20
So go ahead and call print
07:23
New line
07:25
And then
07:26
So I've done my comparison to EBX and 10 and so I can say jump. Not if it's not zero. Then I
07:34
I went to go back up to the and yet part,
07:38
and that's why I set up in it the way I did. Right? So it does some,
07:42
um you know, incremental GDX. So the first time through e. D. X was one because I incremental from zero.
07:48
So now it should go to two and then a copy to Ta X and then should Sylvia needy ex
07:55
and then we go through in print off right. Adding the number two each time Ta x, which again is set to to
08:05
Okay,
08:07
so
08:09
that should be the entirety of the part that we need in order to get stuff done.
08:18
Looks like I didn't have any syntax airs.
08:20
So if I want to print my multiples of five,
08:24
we end up with a problem.
08:26
And so the problem is that inside of
08:30
the print of function, it actually destroys values for E x e d X and E C X.
08:37
So print F is messing up our variables that we're using a for the looping and be for our numbers, right, because the return result of a function
08:46
is whether or not it was successful or the value that it needed.
08:50
And so this is messing up all of our variables. And so what we need to do is we need to save our our registers
09:00
that we are using. So in our function, we're using a X e b x,
09:05
sorry x,
09:07
yeah, ebx ccx and Edie X. So I can go ahead and save all of those
09:18
before I call print F. So here I am,
09:22
calling print F,
09:26
And then I've got some work that I'm doing down here.
09:30
So I want to restore my registers,
09:39
so you should think about What is the order that I need to restore my registers?
09:45
All right, so all do it. So we're gonna do pop GDX
09:50
pop E C X
09:52
pop E b X
09:56
pop e X. Right. So now I have restored all of my registers from the previous values that they had,
10:03
right? And they're saved on the stack. So now when print up messes with my function
10:07
right, it won't actually hurt my registers.
10:24
So I typed five for my number and so right, The first loop. It's using one as my what? I'm gonna increment by. So I go. 12345
10:33
And now in our function, right e d X becomes too. And so that's what I'm incremental by. So I got to for 68 10
10:43
and then 369 12 15 doing each multiple up to, but not including the number 10.
10:50
And then if I enter the number 10 right, so I print off 10 multiples of each one of these numbers.
11:01
Now, one of the things that we also talked about was saving all the registers that you use so that you don't mess them up.
11:07
So again we could save our registers. So we used yea x e b x e c x and e d x
11:15
So I'll pick a random order,
11:24
right? So I did e c x then ea accident GDX than ebx.
11:31
So at the end of my function,
11:33
if I can get down here
11:37
now, my order is gonna kill me here. So let's look
11:43
b and n d
11:54
be an Indy and then a and then see
12:07
Okay,
12:07
so I just have to make sure that I know what the order was before I called the function and what the order is after
12:16
and what we can do is we can see if we did it right. So we're getting ready. We're calling our function
12:22
so we can
12:26
dump our registers
12:37
before and after,
12:43
so I'll just do to
12:46
so we can see.
12:48
But you could pick a different number.
12:50
So again, see a X e x is two nuggets restored ebx zero before zero after
12:58
e c x
13:00
looks like ff 988 f d zero
13:05
Same thing there
13:05
and then here FF
13:09
98 ff four on that is also restored so we can see that we didn't modify anything right because we went through and made sure that all of the registered that were using in our function we saved
13:22
before we modified them. And then when we got to the end, we restored the values back to the where they were.
13:30
We used our function prologue and and here's our epilogue.
13:35
We used our prologue at the very beginning,
13:39
right? And then
13:39
we had to fix the air when the fact that another function had obliterated our registers, we needed it to save them and restore them before and after it obliterated them because we wanted to get back to the previous state that we're at.
13:54
We also used one parameter.
13:56
All right, so e v P plus eight was our parameter. And we use that in order to determine how many times that we were going to do our
14:03
our inner loop.
14:11
So today we looked at creating a function from the very beginning, using the conventions that we've talked about in previous lectures.
14:20
And then we ran into a problem and we had to understand what is the way that I can solve it? And why is it a problem? Why is it a problem that functions,
14:26
Um, obliterate registers. And how do you fix that problem?
14:33
Next time we're going to talk about a recursive function or a function that calls itself.
14:39
And if you have questions, you can contact me at Miller MJ at you and Kate. I e do you can find me on Twitter at Milhouse 30.
Up Next
Similar Content