WEBVTT

1
00:01.420 --> 00:03.460
Hello and welcome back.

2
00:03.460 --> 00:07.600
In this lesson, I'm going to show you how to create functions.

3
00:07.600 --> 00:13.690
And creating functions is also the same as creating what we call stack frames.

4
00:13.690 --> 00:15.520
So what are stack frames?

5
00:15.850 --> 00:17.170
Let me explain.

6
00:17.470 --> 00:20.530
For example, you see here push -.

7
00:21.480 --> 00:31.560
Move - - and then at the end here you will have move - - and pop -.

8
00:31.830 --> 00:40.350
So a stack frame is starting from these two statements and then ending with two statements here.

9
00:40.620 --> 00:42.240
So that's a stack frame.

10
00:42.240 --> 00:45.150
There is one more line here which we have removed.

11
00:45.150 --> 00:53.190
It is supposed to read move - - over here is move - -.

12
00:53.340 --> 01:02.010
So over here there should be one more move - - the reverse of this, okay.

13
01:02.010 --> 01:04.140
So that is a stack frame.

14
01:04.140 --> 01:10.650
So this itself is a function. A function starts from a stack frame, starts from this opening statements,

15
01:10.650 --> 01:12.060
and ends here.

16
01:12.300 --> 01:14.220
And then there is a return.

17
01:14.580 --> 01:17.940
Now we are going to create a function within this big function.

18
01:17.940 --> 01:20.040
So how do we create a function?

19
01:20.190 --> 01:24.720
So to create a function, you must first find out where you want to put your function.

20
01:24.720 --> 01:29.640
Let's say we want to put our function over here, over here.

21
01:29.640 --> 01:31.680
So we need to copy this address.

22
01:32.250 --> 01:33.840
Copy this address.

23
01:35.160 --> 01:38.880
And then over here we make a call to the function.

24
01:41.310 --> 01:43.050
Then paste the address that we have copied.

25
01:44.970 --> 01:47.730
So this here is a call to the function.

26
01:47.730 --> 01:55.980
So when the execution comes to this line, it will call this function at this address 401569, which is

27
01:55.980 --> 01:57.690
401569.

28
01:57.690 --> 02:00.210
Over here it is supposed to jump over there.

29
02:00.210 --> 02:00.600
Right.

30
02:01.650 --> 02:03.840
And then, uh, maybe it's not so obvious.

31
02:03.840 --> 02:05.430
Let's choose an address.

32
02:05.430 --> 02:06.300
Scroll down here.

33
02:07.170 --> 02:09.090
Copy this address.

34
02:09.090 --> 02:09.300
All right.

35
02:09.300 --> 02:11.520
Let's change it so that it's more obvious.

36
02:15.960 --> 02:16.290
Okay.

37
02:16.290 --> 02:21.030
So now it will call 40156F, which is over here.

38
02:21.030 --> 02:22.200
40156F.

39
02:22.350 --> 02:25.350
So at this address here we need to create a function.

40
02:25.350 --> 02:29.070
So to create a function we must create a stack frame.

41
02:29.430 --> 02:34.200
So the first statement in the stack frame is always push -.

42
02:35.670 --> 02:42.120
Then it will be followed by move - -.

43
02:44.860 --> 02:46.870
So this is the start of your function.

44
02:47.110 --> 02:50.320
Then the function always ends with the opposite of this.

45
02:50.320 --> 02:56.230
That means somewhere down here, after you have done whatever you want to do within the function, you

46
02:56.230 --> 02:58.270
should reverse these two, this and this.

47
02:58.270 --> 03:01.300
So down here let's say we want to reverse that.

48
03:01.480 --> 03:13.270
So we come back here. We will do the opposite: move - - and then followed by pop -.

49
03:13.510 --> 03:16.150
And finally a return, ret.

50
03:17.320 --> 03:20.470
So the ret keyword is the opposite of a call.

51
03:20.770 --> 03:24.220
So when you run it will call this function.

52
03:24.220 --> 03:25.210
That means it will jump to

53
03:25.210 --> 03:29.620
here, perform these two opening statements of the stack frame.

54
03:29.740 --> 03:38.110
Then perform whatever you want to do inside here, and then do the reverse to, uh, unwind the stack

55
03:38.110 --> 03:43.480
frame, and then finally return back to the line after the call.

56
03:43.660 --> 03:45.790
So let's try this now and see how it works.

57
03:45.790 --> 03:47.350
So I put a breakpoint here.

58
03:47.350 --> 03:49.090
So we run to the breakpoint.

59
03:49.450 --> 03:51.940
Then we step over until we come to our call.

60
03:52.270 --> 03:59.650
So now in our call, if we were to continue to step over, what will happen is it will execute this line

61
03:59.650 --> 04:04.720
and then immediately come here. You won't see that the call going into the function.

62
04:04.720 --> 04:10.990
If you want to see the call going into the function, you need to use step into over here, step into.

63
04:10.990 --> 04:15.940
So when you click step into, you can see that it steps into the function.

64
04:16.600 --> 04:19.600
Now you're in the first line of the stack frame.

65
04:19.600 --> 04:21.520
So this is the start of the function.

66
04:21.520 --> 04:27.880
And then you can continue to step over over here, step over, step over, and perform whatever instructions,

67
04:27.880 --> 04:34.420
if any, until you come to the end of the stack frame where it will then unwind the stack frame to

68
04:34.420 --> 04:36.820
revert back to the original.

69
04:36.910 --> 04:39.850
So let's revert everything back to original.

70
04:40.790 --> 04:45.590
And then finally, when it finishes with the function, it will return to the line after this call.

71
04:45.590 --> 04:49.340
So from here it's going to jump back up and land over here.

72
04:49.340 --> 04:50.240
Let's take a look.

73
04:50.390 --> 04:52.700
Step over and you see it goes back here.

74
04:53.660 --> 04:58.100
So this is how you can create, uh, functions using stack frames.

75
04:58.100 --> 05:07.100
So every function usually has these opening statements to create the stack frame and the closing statements

76
05:07.100 --> 05:09.920
to destroy or unwind the stack frame.

77
05:09.920 --> 05:15.050
So this opening two lines are also known as function prologue.

78
05:15.080 --> 05:19.100
Function prologue meaning the start of the function.

79
05:19.460 --> 05:25.340
And these two lines are function epilogue, also known as the end of the function.

80
05:26.570 --> 05:28.580
So that's all for this lesson.

81
05:28.580 --> 05:29.960
Thank you for watching.