WEBVTT

1
00:01.530 --> 00:01.890
Hello.

2
00:01.890 --> 00:02.610
Welcome back.

3
00:02.610 --> 00:06.120
In this lesson we are going to do some practicals on what we have studied.

4
00:06.120 --> 00:11.250
In the last lesson, we are going to create a simple function to add numbers.

5
00:11.250 --> 00:17.700
So let's say you pass eight arguments to the function, and the function will add all those arguments and

6
00:17.700 --> 00:19.020
save it to the register -.

7
00:19.590 --> 00:24.270
So - is traditionally used to return the result of a function call.

8
00:24.450 --> 00:25.860
So let's get started.

9
00:26.010 --> 00:32.430
So the first thing we need to do is, uh, we have to move those arguments to a register.

10
00:32.910 --> 00:35.400
So here we move the arguments.

11
00:35.400 --> 00:40.530
So the first example, we are going to have only four numbers to add.

12
00:40.980 --> 00:43.590
So the first number, we'll move one to -.

13
00:47.200 --> 00:50.830
Second number, we move to -.

14
00:54.640 --> 00:56.620
Third number, we move to -.

15
00:56.650 --> 01:00.010
This is we are following the Microsoft convention.

16
01:01.600 --> 01:07.210
And then we move to - fourth number.

17
01:07.980 --> 01:13.590
So since we don't use the stack, we don't have to allocate the arguments for the stack, but we still

18
01:13.590 --> 01:15.660
have to create the shadow space.

19
01:15.660 --> 01:19.470
So to create shadow space, we need to sub - 32.

20
01:19.470 --> 01:25.920
Because Microsoft convention requires us to create 32 bytes shadow space.

21
01:26.940 --> 01:31.440
Next, we are going to make a call to our function, assuming our function is over here.

22
01:31.440 --> 01:36.210
So we copy the address first, and then we put it here.

23
01:37.320 --> 01:40.590
We do a call, paste the address I will copy.

24
01:40.590 --> 01:41.850
So this is our function.

25
01:45.320 --> 01:45.680
All right.

26
01:45.680 --> 01:51.290
So when a call returns, it's going to save. You can see the result in the - register.

27
01:51.410 --> 01:51.920
All right.

28
01:51.920 --> 01:54.440
So you can do whatever you want with it.

29
01:54.620 --> 01:56.870
But when the call returns, you need to clean the stack.

30
01:56.870 --> 02:01.700
So since you sub - 20, you need to add

31
02:02.940 --> 02:05.010
- 20.

32
02:05.280 --> 02:07.290
So 20 here is actually hex here.

33
02:07.530 --> 02:11.340
So in the decimal will be 32.

34
02:13.130 --> 02:17.840
So now we can implement our function call, which is here.

35
02:18.290 --> 02:24.200
So in the function call, the first thing you do is to, uh, push -.

36
02:24.800 --> 02:27.710
Note that I'm just referring to the notes here.

37
02:28.840 --> 02:29.620
The notes here.

38
02:30.040 --> 02:31.000
Like this.

39
02:32.850 --> 02:34.770
In the function call, you need to push -.

40
02:34.800 --> 02:37.710
Remember we studied before in the earlier lessons.

41
02:37.980 --> 02:41.910
The first thing you do in the function call is push - and move - -.

42
02:43.580 --> 02:45.350
So that's what we're doing here.

43
02:47.570 --> 02:50.240
Next is to move - -.

44
02:53.990 --> 02:57.830
Thereafter, we can access those numbers.

45
02:58.190 --> 03:05.660
But first we need to zero out - so that - maybe - with itself.

46
03:05.750 --> 03:06.890
We are zeroing in.

47
03:06.890 --> 03:08.300
Zeroing it out.

48
03:08.990 --> 03:09.410
Why?

49
03:09.440 --> 03:13.100
Because we are going to use - to be the accumulator

50
03:13.190 --> 03:14.870
to sum up all the numbers.

51
03:15.380 --> 03:19.040
So the first number, we are going to do is to add

52
03:23.090 --> 03:25.220
- -.

53
03:27.430 --> 03:28.630
-

54
03:32.710 --> 03:35.680
That is the first argument.

55
03:37.450 --> 03:39.250
Then we add - -.

56
03:39.460 --> 03:40.660
-

57
03:43.090 --> 03:44.080
Then we add

58
03:44.970 --> 03:45.960
- -.

59
03:46.380 --> 03:47.340
-

60
03:50.480 --> 03:53.660
Then we add - -.

61
03:59.300 --> 04:01.280
Then we have to return now.

62
04:01.700 --> 04:05.780
So we're not creating any local variables, so we can just return.

63
04:06.260 --> 04:14.090
So before we return, we need to move, need to clean the stack, although we don't use it, but we

64
04:14.090 --> 04:18.470
always do this - -.

65
04:20.490 --> 04:22.230
Maybe pop -.

66
04:24.090 --> 04:25.380
Then we return.

67
04:28.000 --> 04:28.390
Let's see.

68
04:30.300 --> 04:30.570
Okay.

69
04:30.570 --> 04:32.970
So we put a breakpoint here.

70
04:33.090 --> 04:34.560
We run to our breakpoint.

71
04:34.560 --> 04:35.580
We step over.

72
04:36.300 --> 04:43.020
So we see we move all this into the registers -, -, -, -.

73
04:43.770 --> 04:46.590
Next, we are going to create shadow space.

74
04:46.920 --> 04:47.850
Then we will call.

75
04:48.450 --> 04:50.550
So we want to follow and trace the code.

76
04:50.550 --> 04:51.780
We need to step into it.

77
04:51.780 --> 04:53.100
So step into the call.

78
04:53.100 --> 04:56.640
We jump down here, then we push -.

79
04:57.360 --> 04:59.040
So - is now on the stack.

80
05:00.000 --> 05:01.230
This is the old -.

81
05:02.460 --> 05:08.910
- 65420, which is here, 65420, which is here.

82
05:09.960 --> 05:10.620
Is -.

83
05:11.310 --> 05:13.320
Then we move - to -.

84
05:14.560 --> 05:17.230
So now - goes to

85
05:17.230 --> 05:23.290
656540, 6550,

86
05:23.290 --> 05:25.270
and the old - is pushed to the stack.

87
05:25.510 --> 05:26.860
Here, you can see here.

88
05:27.280 --> 05:31.720
So when the call finishes, it will unwind the stack and go back to this old -.

89
05:32.170 --> 05:40.360
But now that you have moved - -, - is pointing to the same location as the -.

90
05:40.390 --> 05:46.810
So this now, from here onwards, will be used as a base pointer to reference the arguments if necessary.

91
05:47.200 --> 05:51.820
So now we are going to add all these registers.

92
05:51.970 --> 05:54.340
First, - becomes zero.

93
05:54.370 --> 06:03.340
Then you add one to -, - to -, and - to -, and - to -, -.

94
06:03.820 --> 06:09.070
So by now - will have the result of the addition one plus two plus three plus four.

95
06:09.790 --> 06:11.440
One plus two is three.

96
06:11.440 --> 06:14.680
Three plus three is six, six plus four is ten.

97
06:14.680 --> 06:15.700
So result should be ten.

98
06:15.700 --> 06:16.510
So we got ten.

99
06:16.510 --> 06:17.500
So it's correct.

100
06:17.980 --> 06:19.660
Then we now clean the stack.

101
06:20.480 --> 06:21.080
And then pop.

102
06:21.080 --> 06:23.930
The old - value back to it.

103
06:24.290 --> 06:30.290
So the old - at the top of the stack will be the old -.

104
06:30.440 --> 06:31.880
So now we pop it back.

105
06:33.530 --> 06:34.160
So now

106
06:34.160 --> 06:38.750
- points to the old stack, and then now we return.

107
06:38.750 --> 06:41.840
So when we return, it will go back to the line after the call.

108
06:42.470 --> 06:45.110
Then from here you can clean the stack.

109
06:45.440 --> 06:47.540
So here you need to clean the stack.

110
06:47.540 --> 06:52.430
Here you sub, uh, - 32.

111
06:52.460 --> 06:54.140
So here you need to clean the stack.

112
06:54.140 --> 06:55.400
Sorry, that's a mistake.

113
06:55.400 --> 06:56.810
There it should be add.

114
06:58.910 --> 07:00.440
- 32.

115
07:01.940 --> 07:02.570
Yes.

116
07:02.750 --> 07:03.050
Like that.

117
07:03.380 --> 07:03.920
All right.

118
07:03.920 --> 07:05.210
So now you clean the stack.

119
07:07.670 --> 07:12.050
And now the stack is back to the original state, which it was in before the call.

120
07:12.380 --> 07:18.530
So this is how you can implement a function call with four parameters and no local variables.

121
07:18.590 --> 07:19.820
That's all for this video.

122
07:19.820 --> 07:20.990
Thank you for watching.