WEBVTT

00:01.360 --> 00:03.124
Additionally, we have the operations

00:03.370 --> 00:05.559
like call we can call a function

00:05.560 --> 00:07.449
name, so what call does is it

00:07.450 --> 00:08.859
pushes the address of the next

00:08.860 --> 00:10.210
instruction onto the stack

00:10.990 --> 00:12.166
and then it copies IP to

00:12.880 --> 00:14.350
that new function, location or

00:14.860 --> 00:15.860
address.

00:16.390 --> 00:18.129
And then the reverse of that is the

00:18.130 --> 00:19.600
return, the return is going to

00:19.960 --> 00:21.579
allow us to exit a function

00:22.000 --> 00:23.590
so or return from a function.

00:24.160 --> 00:25.924
So what that does is it pops off the

00:25.930 --> 00:27.789
return address from the stack and

00:27.790 --> 00:29.709
then moves VIP to that new

00:29.710 --> 00:30.710
address.

00:30.970 --> 00:32.587
So we end up always having a call

00:32.950 --> 00:34.389
to a function and then inside the

00:34.390 --> 00:36.009
function when it's done, we'll have

00:36.010 --> 00:37.010
a return.

00:37.060 --> 00:38.439
And so that's how we can write a

00:38.560 --> 00:39.909
program with functions.

00:40.330 --> 00:41.799
Now, you've done this in class.

00:41.860 --> 00:43.689
We have called a lot of different

00:43.690 --> 00:44.679
functions.

00:44.680 --> 00:46.529
So for example, call, read to

00:46.540 --> 00:48.549
call, print and call, print and tell

00:48.880 --> 00:50.301
all of those we are calling a

00:50.350 --> 00:51.699
function and then we assume that

00:51.700 --> 00:53.469
it's going to resume from the

00:53.470 --> 00:55.869
location that we were at before.

00:58.420 --> 01:00.184
We also have the ability to allocate

01:00.730 --> 01:03.130
space using add and subtract.

01:03.640 --> 01:05.949
So, for example, we can say sub hesp

01:05.980 --> 01:08.079
X and then I'll subtract X bytes

01:08.080 --> 01:09.080
from ESP.

01:09.490 --> 01:10.719
So typically when you enter a

01:10.720 --> 01:12.099
function, you're going to do a

01:12.100 --> 01:13.423
subtract on ESP to allocate

01:13.930 --> 01:15.879
some temporary space that we can use

01:15.880 --> 01:17.829
for local variables or buffer's.

01:18.700 --> 01:20.199
And then at the end of the function

01:20.200 --> 01:22.209
we can add back ESP, so

01:22.210 --> 01:24.069
we'll typically add and subtract the

01:24.070 --> 01:25.360
same number of bytes.

01:26.020 --> 01:27.784
And so this will allocate that space

01:28.000 --> 01:29.617
and we'll do that right before we

01:29.650 --> 01:30.650
leave a function.

01:31.240 --> 01:32.955
And then if you ever go through and

01:32.980 --> 01:34.419
you are reverse engineering some

01:34.420 --> 01:35.939
code, you might see that when a

01:36.190 --> 01:37.419
function enters, it might do

01:37.420 --> 01:38.694
something like and ESP and

01:39.370 --> 01:40.989
then you've got a bunch of F's and a

01:40.990 --> 01:41.990
zero.

01:42.160 --> 01:43.875
And this is because it is trying to

01:43.900 --> 01:45.566
make sure that E.S.P is on an even

01:45.970 --> 01:48.189
boundary and this is a security

01:48.190 --> 01:49.660
protection that some operating

01:49.780 --> 01:51.544
systems and some compilers implement

01:51.790 --> 01:53.113
to make it harder to attack

01:53.650 --> 01:54.790
these type of systems.

