WEBVTT

00:01.780 --> 00:03.495
Hello, this is Dr. Miller, and this

00:03.520 --> 00:04.941
is Episode six point three of

00:05.230 --> 00:06.230
assembly

00:07.300 --> 00:08.469
today we're going to start using the

00:08.470 --> 00:10.059
stack and then we're going to have

00:10.060 --> 00:11.619
an example of calling the function

00:11.620 --> 00:12.620
printf.

00:13.630 --> 00:14.630
Using the stick.

00:15.910 --> 00:17.589
So when we have a stack, one of the

00:17.590 --> 00:18.549
things that we're going to want to

00:18.550 --> 00:20.109
do is we're going to want to either

00:20.110 --> 00:21.969
push things on or pop

00:21.970 --> 00:22.970
things off.

00:23.560 --> 00:25.079
And so every time that you push

00:25.300 --> 00:27.519
something on, it modifies

00:27.520 --> 00:29.620
ESP and

00:29.770 --> 00:31.239
when you're done, you need to pop it

00:31.240 --> 00:32.048
back off.

00:32.049 --> 00:33.970
So an example would be push yak's,

00:34.060 --> 00:36.009
push SBX and then we can pop them

00:36.010 --> 00:37.280
in the reverse order.

00:38.200 --> 00:39.572
So that's one way to restore

00:40.420 --> 00:41.799
the stack back to its original

00:41.800 --> 00:43.299
state, which is what we need to do

00:43.300 --> 00:44.300
all the time.

00:44.950 --> 00:46.539
The other thing that we could do is

00:46.540 --> 00:48.679
we could push on two registers or

00:48.700 --> 00:50.415
register twice and then we can just

00:50.590 --> 00:51.590
add E.S.P to correct

00:52.450 --> 00:53.680
the amount that we put on there.

00:54.400 --> 00:56.049
If we don't do this, then our

00:56.050 --> 00:57.399
program is going to crash and we'll

00:57.400 --> 00:58.809
show an example of that in a little

00:58.810 --> 00:59.810
while.

01:01.240 --> 01:02.906
So, for example, you might want to

01:02.920 --> 01:04.749
save some data when you enter

01:04.750 --> 01:05.979
a function and we'll talk about how

01:05.980 --> 01:07.509
to create functions in the next

01:07.510 --> 01:08.510
lecture.

01:08.860 --> 01:10.624
So when you enter your function, you

01:10.810 --> 01:12.090
might want to push registers.

01:12.100 --> 01:14.339
So, for example, you exceed

01:14.980 --> 01:15.980
S.I and then when

01:16.840 --> 01:18.399
you're done, you need to pop them

01:18.400 --> 01:19.730
off in the reverse order.

01:19.750 --> 01:21.639
So the last one that you pushed is

01:21.640 --> 01:22.709
the first one that you pop.

01:23.320 --> 01:24.320
So you can see we did

01:25.580 --> 01:27.669
X, X and

01:27.670 --> 01:28.670
then X.

01:29.470 --> 01:30.849
And so when we create functions,

01:30.850 --> 01:31.959
we're going to want to enter.

01:31.970 --> 01:33.249
We're going to save our our

01:33.250 --> 01:34.916
registers. And when we exit, we're

01:34.930 --> 01:36.351
going to restore them back in

01:36.760 --> 01:38.079
the reverse order that we had.

01:39.970 --> 01:40.970
So printf so

01:41.980 --> 01:43.499
printf is a C function that has

01:43.960 --> 01:45.819
a variable amount of arguments,

01:45.820 --> 01:47.469
which means that you can have zero

01:47.470 --> 01:49.569
one, two, three, four, 10, 20,

01:49.990 --> 01:51.429
whatever the number of arguments

01:51.430 --> 01:52.430
that you want is.

01:52.810 --> 01:53.979
And we have this thing called a

01:53.980 --> 01:54.939
format string.

01:54.940 --> 01:56.349
So format string has these

01:56.770 --> 01:58.659
characters and then that gets

01:58.660 --> 02:00.326
replaced with the actual variables

02:00.430 --> 02:02.050
X, Y and C.

02:05.290 --> 02:06.579
So we're going to do is we're going

02:06.580 --> 02:08.830
to call a print function

02:09.759 --> 02:11.529
and we've got to push our arguments

02:11.530 --> 02:13.210
in the reverse order because

02:13.390 --> 02:14.860
the first thing on is the last

02:15.340 --> 02:16.389
thing that we have in there.

02:16.990 --> 02:18.459
So we'll go ahead and start our

02:18.460 --> 02:19.460
project here.

02:21.200 --> 02:22.699
So I've already built the project.

02:31.340 --> 02:32.329
So the first thing that we're going

02:32.330 --> 02:33.259
to do is we're going to declare a

02:33.260 --> 02:34.779
couple variables and then we're

02:34.820 --> 02:36.379
going to define our format string.

02:39.770 --> 02:41.059
So I'm going to try and do the same

02:41.060 --> 02:43.399
format string that we had before.

02:46.420 --> 02:47.909
I'm just typing that in.

02:49.930 --> 02:51.339
The present day means that it's

02:51.340 --> 02:52.989
going to be a integer.

02:56.950 --> 02:59.289
The sea is a character,

03:00.490 --> 03:02.259
and then the 10 is our new line and

03:02.260 --> 03:03.975
the zero terminates our stream, and

03:04.150 --> 03:05.219
then we'll declare a couple

03:05.230 --> 03:06.699
variables. So X

03:07.850 --> 03:10.240
has 10 Y.

03:11.760 --> 03:12.760
As 20.

03:15.310 --> 03:17.119
Then I'll show some examples here,

03:18.930 --> 03:20.609
so we have to push on our arguments

03:20.610 --> 03:21.719
in the reverse order, so I'm going

03:21.720 --> 03:23.909
to push my character first.

03:25.950 --> 03:27.599
So I can define a character, so that

03:27.600 --> 03:29.249
is the character a right, so that

03:29.250 --> 03:30.490
would put it onto the stack.

03:30.510 --> 03:32.580
Now I could use

03:32.850 --> 03:34.889
a register to put that in, but

03:34.890 --> 03:36.869
we'll just show pushing an immediate

03:36.870 --> 03:38.369
or just pushing a hard coded value

03:38.370 --> 03:39.370
onto the stack

03:40.830 --> 03:42.269
here. We want to get the value of

03:42.270 --> 03:43.439
what? Why?

03:49.490 --> 03:51.139
And I'm just using X because it's

03:51.140 --> 03:52.140
convenient.

03:57.560 --> 03:59.479
And we'll do X

03:59.480 --> 04:00.480
here.

04:02.700 --> 04:04.023
All right, so I've put them

04:04.620 --> 04:06.119
on in the reverse order, so I got my

04:06.120 --> 04:07.499
character is the first thing that I

04:07.500 --> 04:08.879
put on because it's the last thing

04:08.880 --> 04:09.880
in here.

04:09.930 --> 04:11.253
Then I got Y and then I got

04:11.910 --> 04:13.380
X, and now I need to put on my

04:13.530 --> 04:14.530
format string

04:17.700 --> 04:18.778
and for the format, we

04:19.709 --> 04:21.199
want the address of it.

04:21.209 --> 04:22.679
We don't want the actual data at it.

04:22.680 --> 04:24.029
So we're getting the address and

04:24.030 --> 04:25.139
then we'll push that on

04:25.890 --> 04:26.890
to our stack.

04:27.870 --> 04:29.009
So now we're ready to call our

04:29.010 --> 04:30.010
function.

04:32.640 --> 04:34.470
So the function is called printf,

04:34.650 --> 04:36.389
but it doesn't exist in our normal

04:36.390 --> 04:37.390
spot, it exists in

04:38.220 --> 04:40.019
the C library, so we have to tell it

04:40.020 --> 04:41.020
that we have an

04:41.910 --> 04:43.779
external function called print

04:43.800 --> 04:44.800
F,

04:46.200 --> 04:47.768
and then we can see here that we

04:48.060 --> 04:50.399
pushed on one to three,

04:50.400 --> 04:52.250
four items onto the stack.

04:52.860 --> 04:54.839
And so when we're done, we have to

04:54.840 --> 04:55.739
correct a stack.

04:55.740 --> 04:57.269
So the easiest way to do that is to

04:57.270 --> 04:59.850
add to E.S.P

05:00.480 --> 05:01.799
the number of 16

05:02.730 --> 05:04.199
or you could do 10 in hex.

05:04.560 --> 05:05.369
Either one is fine.

05:05.370 --> 05:06.660
So 16, base 10.

05:06.780 --> 05:08.370
Or we can do list 10

05:09.030 --> 05:10.030
X.

05:18.360 --> 05:19.529
So we built our project,

05:20.590 --> 05:21.799
so now we can go ahead and run it.

05:23.980 --> 05:25.839
So print it off the data that we

05:25.840 --> 05:26.899
thought it would print off.

05:27.250 --> 05:28.389
So let's go back in.

05:30.200 --> 05:31.879
And let's look at if we forgot to

05:31.880 --> 05:33.439
correct the stats, I'll just comment

05:33.440 --> 05:34.440
on this line.

05:35.660 --> 05:37.279
So now when I build my project.

05:42.210 --> 05:43.560
It causes it to crash

05:44.670 --> 05:46.529
because there are pointers

05:46.530 --> 05:47.759
that are saved on the stack that if

05:47.760 --> 05:49.049
we don't restore the stock back to

05:49.050 --> 05:50.189
its original, then it's going to

05:50.190 --> 05:51.190
have problems.

05:51.600 --> 05:53.730
And even if I if I did this and I

05:54.000 --> 05:56.009
let's say only corrected it by

05:57.270 --> 05:59.519
12 instead of 16

06:00.330 --> 06:01.649
because I forgot about one of my

06:01.650 --> 06:03.180
arguments that I put on there.

06:08.160 --> 06:09.385
I'll clean my project and

06:10.260 --> 06:11.260
run it.

06:13.430 --> 06:14.839
It also caused it to crash,

06:15.890 --> 06:17.359
so one of the keys to understanding

06:17.360 --> 06:18.559
here is that you want to make sure

06:18.560 --> 06:19.736
that you put the correct

06:20.420 --> 06:22.399
amount back on solidary 16

06:22.400 --> 06:23.400
and decimal.

06:25.280 --> 06:26.869
Now, when we run our function,

06:27.350 --> 06:29.269
it works fine and it doesn't crash

06:30.230 --> 06:31.289
in a segmentation fault.

06:31.310 --> 06:32.629
This means that the program tried to

06:32.630 --> 06:34.009
access a region of memory that it

06:34.010 --> 06:35.389
wasn't allowed to access.

06:40.790 --> 06:42.169
All right, so today we looked at

06:42.170 --> 06:44.269
using the stack by calling

06:44.270 --> 06:46.370
our function that was called printf.

06:48.840 --> 06:49.799
So here's a couple of quick

06:49.800 --> 06:51.569
questions when exiting a function,

06:51.570 --> 06:53.399
what operation to perform

06:53.400 --> 06:54.400
on E.S.P.

06:56.540 --> 06:58.459
So generally, we add E.S.P

06:58.460 --> 07:00.019
and then some amount, and that's the

07:00.020 --> 07:01.399
amount that we allocated when we

07:01.400 --> 07:02.821
were entering the function or

07:03.350 --> 07:04.790
before we were calling a function.

07:07.170 --> 07:09.210
And then how do we save registers?

07:11.990 --> 07:13.910
So we push the register, so

07:14.000 --> 07:15.649
we looked at earlier, you push them

07:15.650 --> 07:17.180
and then you would pop them, so

07:17.690 --> 07:19.111
push X or push Ebix, whatever

07:19.790 --> 07:21.079
the register that you want to save

07:21.080 --> 07:22.080
is.

07:24.680 --> 07:26.689
Looking forward, we're going to look

07:26.690 --> 07:28.309
at calling functions and then even

07:28.310 --> 07:29.829
creating what we call recursive

07:29.990 --> 07:30.990
functions.

07:32.740 --> 07:33.789
If you have questions, you can

07:33.790 --> 07:35.211
contact me at Milang, at you,

07:35.320 --> 07:37.149
Ankita, Edu, and you can find me on

07:37.150 --> 07:38.529
Twitter at Milhouse 30.

