WEBVTT

00:01.540 --> 00:03.039
Hello, this is Dr. Miller, and this

00:03.040 --> 00:04.659
is Episode six point seven of

00:04.660 --> 00:05.660
Assembly.

00:06.340 --> 00:07.389
Today, we're going to talk about

00:07.390 --> 00:08.410
function arguments,

00:09.490 --> 00:10.490
function arguments.

00:11.110 --> 00:12.727
These allow us to pass parameters

00:13.420 --> 00:14.530
to a function.

00:14.590 --> 00:16.089
This allows the function to do

00:16.090 --> 00:17.349
different things based on those

00:17.350 --> 00:18.350
parameters.

00:18.580 --> 00:19.839
And it turns out we only have a

00:19.840 --> 00:21.212
limited number of registers.

00:21.310 --> 00:22.449
And so we're going to end up having

00:22.450 --> 00:23.450
to use the stack.

00:24.160 --> 00:25.749
And so because of that, we are going

00:25.750 --> 00:27.465
to by default, we're going to start

00:27.550 --> 00:29.039
using an EVP based stack.

00:29.200 --> 00:30.669
We've talked about that in previous

00:30.670 --> 00:31.670
lectures.

00:31.900 --> 00:33.615
So we set up our function using our

00:33.670 --> 00:34.944
Prolog and then at the end

00:35.560 --> 00:37.275
we're going to restore our function

00:37.420 --> 00:38.420
using the epilog.

00:39.340 --> 00:40.569
And so every time you write a

00:40.570 --> 00:42.639
function, you should use these

00:42.640 --> 00:43.640
by default.

00:46.210 --> 00:47.739
And then recall, when we set up

00:47.740 --> 00:49.210
using a Prolog, right, we have

00:49.570 --> 00:51.089
SBP, which is our extended base

00:51.340 --> 00:53.229
pointer, so the base pointer is

00:53.230 --> 00:54.849
going to point to the old value of

00:54.850 --> 00:56.529
EBP. So that's what's on top of the

00:56.530 --> 00:57.530
stack.

00:57.550 --> 00:58.509
And then we're going to have our

00:58.510 --> 00:59.979
return address and that's going to

00:59.980 --> 01:01.659
be at EBP plus four.

01:02.470 --> 01:04.208
And then argument one is going to be

01:04.209 --> 01:06.159
at EBP plus eight, an

01:06.160 --> 01:08.319
argument two, is it EBP plus C?

01:08.590 --> 01:09.766
Argument three is at EBP

01:10.420 --> 01:11.890
plus 10 Hex.

01:13.170 --> 01:14.248
And then it goes so on

01:15.090 --> 01:16.217
and so forth. So 14, 18

01:17.040 --> 01:18.040
Wannsee,

01:20.130 --> 01:22.109
so for example, right, if

01:22.110 --> 01:23.819
we want to copy the data, the first

01:23.820 --> 01:25.609
parameter into a register X,

01:25.740 --> 01:27.480
we can use SBP plus eight

01:28.170 --> 01:29.429
if we want to copy the second

01:29.430 --> 01:30.209
parameter.

01:30.210 --> 01:31.484
So in a Nazeem, we have to

01:32.040 --> 01:34.170
put a zero if we're using a letter

01:34.590 --> 01:35.579
to start.

01:35.580 --> 01:36.749
And that's because otherwise it

01:36.750 --> 01:37.679
thinks it's a variable.

01:37.680 --> 01:39.450
So we got zero C in hex.

01:40.510 --> 01:41.840
So that's our second parameter.

01:42.660 --> 01:44.369
So you can try on your own to figure

01:44.370 --> 01:45.780
out what is the third parameter.

01:48.840 --> 01:50.819
So the best way to learn

01:50.820 --> 01:53.069
this is to do an example.

01:53.100 --> 01:54.419
So we're going to do example while

01:54.420 --> 01:55.420
we're going to

01:56.250 --> 01:58.019
read one integer and print it and

01:58.020 --> 01:59.579
then we'll we'll modify it so that

01:59.580 --> 02:00.569
it uses two.

02:00.570 --> 02:01.919
And then we're going to use print F

02:01.950 --> 02:03.209
and we're going to print the values

02:03.210 --> 02:04.260
in hexadecimal.

02:11.390 --> 02:12.615
All right, so we have our

02:13.310 --> 02:14.749
I've already created our project.

02:17.800 --> 02:19.329
So I'll go ahead and start entering

02:19.330 --> 02:20.949
it, so the first thing that we're

02:20.950 --> 02:22.059
going to do is we're going to set up

02:22.060 --> 02:23.060
our format.

02:27.110 --> 02:28.580
So our format is going to have

02:29.300 --> 02:31.009
a zero X at the beginning so that we

02:31.010 --> 02:32.088
know this is a hex and

02:32.840 --> 02:34.209
then you can look up the printf

02:34.220 --> 02:36.380
string so percent zero

02:36.410 --> 02:38.629
eight and then an uppercase

02:38.630 --> 02:40.340
X. So this is going to print off

02:40.700 --> 02:42.250
zero leading digits.

02:42.260 --> 02:43.338
So the if we have less

02:44.090 --> 02:45.259
than eight digits, it'll print

02:45.260 --> 02:46.249
zeros.

02:46.250 --> 02:47.359
We're going to print up to eight

02:47.360 --> 02:48.360
digits.

02:48.410 --> 02:49.729
The X means we're going to print in

02:49.730 --> 02:51.619
hex and the uppercase

02:51.620 --> 02:53.360
X means it's going to be uppercase

02:54.200 --> 02:56.660
X uppercase hexadecimal.

02:57.710 --> 02:59.149
And then we'll put a new line at the

02:59.150 --> 03:00.620
end so that we have that at the end.

03:01.730 --> 03:03.298
Then in our main, we'll go ahead

03:03.710 --> 03:05.960
and read our numbers.

03:09.500 --> 03:11.389
And we're going to push our number

03:11.390 --> 03:12.249
onto this.

03:12.250 --> 03:13.639
So remember, read into

03:14.630 --> 03:16.345
the result Wozniak's so we're going

03:16.520 --> 03:17.929
to push that onto the top of our

03:17.930 --> 03:19.909
stack and

03:19.910 --> 03:21.079
our function is going to be called

03:21.080 --> 03:22.080
Pantex.

03:24.910 --> 03:26.529
So I'll do that, and then while I'm

03:26.530 --> 03:27.399
here, I'm going to remember to

03:27.400 --> 03:28.509
correct the stack, so I put

03:28.530 --> 03:30.129
something on, so I need to pop it

03:30.130 --> 03:32.349
off. So I'll just do add

03:32.770 --> 03:33.770
E.S.P four,

03:34.900 --> 03:36.229
because I pushed on one argument.

03:37.120 --> 03:38.439
So now we'll come down here and to

03:38.440 --> 03:39.440
our function

03:41.410 --> 03:42.189
here is a function.

03:42.190 --> 03:44.710
We'll do our Prolog, push BP,

03:44.920 --> 03:45.920
move

03:47.300 --> 03:48.369
EVP,

03:50.590 --> 03:51.590
ESP.

03:51.640 --> 03:53.169
I'll come down here and do my epilog

03:53.170 --> 03:54.444
while I'm remembering move

03:55.210 --> 03:57.009
sbp e.s.p

03:57.400 --> 03:58.979
pop SBP

03:59.860 --> 04:00.860
return.

04:01.720 --> 04:03.279
So now I'm ready to do the main part

04:03.280 --> 04:04.280
of my function.

04:04.660 --> 04:06.129
And so what I'd like to do here is

04:06.130 --> 04:08.290
I'd like to copy into

04:08.440 --> 04:10.029
Yak's my first parameter.

04:10.270 --> 04:11.650
Some vacs

04:12.610 --> 04:14.710
remember it's an EBP based act, so

04:14.860 --> 04:16.389
the Valuate SBP

04:17.980 --> 04:19.659
plus eight,

04:21.130 --> 04:22.698
I'll move that index and now I'm

04:22.870 --> 04:24.193
going to call my my Prenter

04:24.310 --> 04:25.310
function.

04:25.630 --> 04:27.190
So the first argument is the format

04:27.220 --> 04:28.689
during the second argument is the

04:28.690 --> 04:29.819
value I want to print.

04:29.840 --> 04:31.380
So I got to push my value first.

04:31.390 --> 04:32.980
So I'm going to push the X

04:34.000 --> 04:35.620
and then I will

04:35.890 --> 04:37.299
move the X

04:38.560 --> 04:41.080
format, push

04:41.350 --> 04:43.350
X and I push the format.

04:43.840 --> 04:45.249
So I've added two things on top of

04:45.250 --> 04:46.250
the stack.

04:46.360 --> 04:48.160
I'm going to call print F

04:48.460 --> 04:50.109
and then I need to correct the stack

04:50.110 --> 04:51.279
because I've just printed off

04:51.280 --> 04:52.059
something.

04:52.060 --> 04:54.690
So add E.S.P

04:54.730 --> 04:56.151
eight because I pushed on two

04:56.620 --> 04:58.269
things, each one of them taking up

04:58.270 --> 04:59.270
four bytes.

05:00.720 --> 05:01.739
Now, if you remember from the

05:01.740 --> 05:03.689
printf, we have

05:03.690 --> 05:05.111
to tell it the print of as an

05:05.130 --> 05:06.149
external function,

05:07.380 --> 05:09.300
so we have to say extern.

05:15.860 --> 05:16.860
Right.

05:18.250 --> 05:19.250
OK,

05:20.080 --> 05:21.989
so I've accessed my first parameter,

05:22.120 --> 05:23.737
I pushed that on and I'm going to

05:23.920 --> 05:25.194
have my format and now I'm

05:26.110 --> 05:28.299
pushing on my format, calling

05:28.300 --> 05:30.270
print and then correcting the stack

05:31.060 --> 05:32.060
so we should have

05:32.890 --> 05:33.890
everything lined up.

05:37.150 --> 05:38.499
All right, complains about my clock

05:38.500 --> 05:40.329
being off, which is fine, so

05:40.330 --> 05:41.589
let's go ahead and run this.

05:43.130 --> 05:44.539
So if I put in the number one

05:44.540 --> 05:45.569
hundred, it prints me

05:46.490 --> 05:48.319
off at the zero X at

05:48.320 --> 05:49.692
the beginning, it prints off

05:50.240 --> 05:51.808
the leading digits, so I got six

05:52.130 --> 05:53.839
zeros in the front and then one

05:53.840 --> 05:55.459
hundred and hexadecimal is sixty

05:55.460 --> 05:56.460
four.

05:56.660 --> 05:58.549
Or I can do something like zero X,

05:58.550 --> 05:59.550
B

06:00.950 --> 06:02.149
and again it's printing an all

06:02.150 --> 06:03.649
uppercase because I did that with

06:03.650 --> 06:04.650
mine.

06:05.380 --> 06:06.699
So let's suppose that we wanted to

06:06.700 --> 06:08.649
read in two numbers, let's

06:08.650 --> 06:09.940
go ahead and go back in here.

06:10.870 --> 06:11.899
So I read one number,

06:12.790 --> 06:14.709
push it so I can do this again,

06:14.730 --> 06:15.730
call, read

06:17.320 --> 06:19.950
it pushbacks.

06:20.670 --> 06:22.299
All right, so now I've pushed on a

06:22.300 --> 06:23.289
second parameter.

06:23.290 --> 06:24.809
So now instead of doing before,

06:25.480 --> 06:26.769
I need to do by eight.

06:28.000 --> 06:29.229
Right. Because I pushed on two

06:29.230 --> 06:30.879
things. I got to basically pop them

06:30.880 --> 06:31.880
off or remove them.

06:31.930 --> 06:33.400
And I'm doing it using add you

06:33.790 --> 06:34.790
could do two pops.

06:34.800 --> 06:36.159
That would be an equivalent way to

06:36.160 --> 06:37.160
do it.

06:38.200 --> 06:39.759
So here I grab the first parameter.

06:39.790 --> 06:41.079
So now I want to grab my second

06:41.080 --> 06:42.080
parameter.

06:42.880 --> 06:44.739
So my second parameter should be

06:47.320 --> 06:48.459
SBP

06:49.720 --> 06:52.149
plus C.

06:57.750 --> 06:59.040
I'll do this a little different

07:00.330 --> 07:01.996
so I can directly push the format,

07:02.100 --> 07:03.360
because it's just an address.

07:07.150 --> 07:08.865
Call my printer function, and I got

07:08.980 --> 07:09.879
to correct the stack because I

07:09.880 --> 07:10.989
pushed on two things.

07:13.050 --> 07:14.050
Ate

07:15.270 --> 07:16.936
my function, epilogs should be the

07:16.980 --> 07:17.980
same.

07:18.000 --> 07:19.529
So I haven't changed anything in

07:19.530 --> 07:20.530
that.

07:21.460 --> 07:22.460
OK,

07:23.400 --> 07:24.449
now if I run, make

07:25.650 --> 07:27.218
and then run my program so I can

07:27.300 --> 07:29.760
type in one hundred and Zurich's

07:31.170 --> 07:32.170
cafe.

07:33.670 --> 07:35.649
And so they push these on

07:35.650 --> 07:37.071
in the reverse order, so CAFE

07:37.540 --> 07:38.789
is the first one that gets printed.

07:38.800 --> 07:40.779
So that's argument one and

07:40.780 --> 07:42.152
one hundred is argument two.

07:42.250 --> 07:43.659
And that's just by the way, I push

07:43.660 --> 07:44.660
them onto the stack.

07:49.840 --> 07:51.369
So today, we talked about function

07:51.370 --> 07:52.987
arguments, we gave an example, we

07:53.230 --> 07:54.896
ran the example, we modified it so

07:55.150 --> 07:56.589
that it had more than one argument

07:57.040 --> 07:58.479
and we made sure that we corrected

07:58.480 --> 07:59.480
the stack every time.

08:01.160 --> 08:02.209
So looking forward, we're going to

08:02.210 --> 08:03.829
talk about saving registers when you

08:03.830 --> 08:05.509
modify them in a function and we'll

08:05.510 --> 08:06.919
look at some more complicated

08:06.920 --> 08:07.920
functions.

08:08.930 --> 08:09.979
So here's our quiz.

08:10.160 --> 08:11.509
What is the first parameter of an

08:11.510 --> 08:12.649
MVP based Steck?

08:15.500 --> 08:17.209
Would be plus eight,

08:17.420 --> 08:18.679
so EBP plus eight.

08:20.300 --> 08:22.489
What's the third parameter of an MVP

08:22.490 --> 08:23.490
based Steck?

08:26.950 --> 08:28.959
So that would be 10 X or

08:28.960 --> 08:30.610
16 days, 10.

08:32.760 --> 08:33.839
If you have questions, you can

08:33.840 --> 08:35.506
contact me at millirems, at Unica,

08:35.789 --> 08:37.979
Edu, and you can find me on Twitter.

08:37.980 --> 08:39.029
Atencio has 30.

