WEBVTT

1
00:00.920 --> 00:01.430
Okay.

2
00:01.430 --> 00:02.240
Welcome back.

3
00:02.270 --> 00:08.930
As a challenge to you, I'd like you to try to implement six arguments function calls with local variables

4
00:08.930 --> 00:09.590
as well.

5
00:09.890 --> 00:12.830
So go ahead and try it on your own before you see the solution.

6
00:13.370 --> 00:21.260
So how I would do it is first I will move, uh, - one.

7
00:22.440 --> 00:29.970
And then move - to, and then move - three.

8
00:30.930 --> 00:34.050
Move - four.

9
00:35.930 --> 00:42.200
Now I need to create a space for six arguments.

10
00:42.350 --> 00:45.560
I've already got four arguments here, but I need to push two to the stack.

11
00:45.620 --> 00:51.740
Means on the stack I need to have two variables, so I need two times 8, 16 bytes extra.

12
00:51.740 --> 00:53.030
So 16.

13
00:53.120 --> 00:58.130
Now we need to add 16 to your shadow space 32.

14
00:58.160 --> 01:01.910
So 16 plus 32 will give you 48 bytes.

15
01:01.910 --> 01:06.860
So I need to sub - 48.

16
01:09.580 --> 01:12.160
I hope you got that and you understood that.

17
01:12.760 --> 01:14.110
You need to have,

18
01:14.110 --> 01:15.580
let me explain that again,

19
01:15.580 --> 01:18.130
how to calculate this one.

20
01:18.910 --> 01:21.430
Your shadow space,

21
01:21.430 --> 01:22.510
you need 32 bytes.

22
01:22.510 --> 01:23.080
Yes.

23
01:23.650 --> 01:27.340
And then you need to have two arguments on the stack.

24
01:27.340 --> 01:29.830
Each argument is eight bytes.

25
01:29.830 --> 01:31.600
So two times eight.

26
01:33.490 --> 01:35.980
So for your fifth and sixth argument,

27
01:37.780 --> 01:40.270
fifth and sixth, you need to put it on the stack.

28
01:40.960 --> 01:45.820
So that will eat up two times 8, 16 bytes.

29
01:46.120 --> 01:51.250
So when you take 32, add 16, you get 48.

30
01:51.370 --> 01:53.530
That's why we sub 48 here.

31
01:54.330 --> 01:55.050
Okay.

32
01:55.620 --> 02:02.850
Uh, then next thing you need to do is you need to assign your fifth and sixth arguments to those

33
02:02.850 --> 02:05.610
on the stack, which is here and here.

34
02:05.820 --> 02:11.310
So to access that, you need to offset - plus 32 and - plus 40.

35
02:12.190 --> 02:13.570
So to do that, we do that.

36
02:13.570 --> 02:22.510
Now move, uh, keyword - plus, and then over here 32.

37
02:24.550 --> 02:27.910
So we move our fifth argument to the

38
02:28.920 --> 02:33.360
fifth argument on the stack.

39
02:33.840 --> 02:37.530
Then we do that for the sixth one as well, which is plus 40.

40
02:38.610 --> 02:45.180
So you repeat the same keyword, - plus 40.

41
02:46.790 --> 02:48.800
And this is the sixth one.

42
02:49.130 --> 02:54.680
So at this point in time, we have all our six arguments ready for the function call.

43
02:54.950 --> 03:00.290
So we can now call our function, assuming our function is way down here.

44
03:00.290 --> 03:01.430
This is a bit too near.

45
03:01.430 --> 03:02.930
So let's move it a bit down here.

46
03:04.250 --> 03:06.050
Uh, we copy this address.

47
03:06.970 --> 03:12.040
And then we make a call to that function.

48
03:14.930 --> 03:15.950
And then

49
03:16.770 --> 03:19.290
next, we need to clean the stack.

50
03:19.860 --> 03:22.290
After the call finishes, we need to clean the stack.

51
03:22.290 --> 03:26.460
So we add, uh, earlier on we sub 48.

52
03:26.460 --> 03:28.680
So now we have to add 48.

53
03:31.810 --> 03:35.740
So when you make a function call, it will jump to this address.

54
03:35.740 --> 03:39.700
40156596.

55
03:40.380 --> 03:44.820
4015960 is too near.

56
03:45.300 --> 03:45.930
Sorry.

57
03:45.930 --> 03:48.360
Let's change to a different address for a function.

58
03:48.360 --> 03:49.680
Let's use this instead.

59
03:50.280 --> 03:52.080
Copy address.

60
03:52.380 --> 03:56.130
Let's change the address of our function to something further down.

61
03:56.790 --> 03:59.730
Paste this new address over here.

62
04:00.720 --> 04:05.460
So now it's going to call the function at this address 40159.

63
04:05.640 --> 04:06.240
Over here.

64
04:06.810 --> 04:16.890
And over here we will push, push our -, and then we will move - -.

65
04:26.260 --> 04:26.860
Okay.

66
04:26.860 --> 04:31.900
And then now we are going to, uh, create four local variables.

67
04:31.990 --> 04:38.320
So we need to sub - with 32 for four local variables.

68
04:42.650 --> 04:49.040
And now we're going to XOR our, we are going to XOR our -.

69
04:51.400 --> 04:53.230
And then now we are going to add.

70
04:53.830 --> 04:55.660
So add the first one.

71
04:59.530 --> 05:00.970
And the second one.

72
05:03.880 --> 05:05.890
And the third one.

73
05:08.900 --> 05:14.150
-, add the fourth one.

74
05:14.330 --> 05:15.110
-.

75
05:18.450 --> 05:18.870
Like that.

76
05:19.170 --> 05:21.510
And now we are going to add the fifth one.

77
05:21.510 --> 05:28.080
So we need to dereference using this notation - plus 48.

78
05:30.640 --> 05:32.980
So we need to - -.

79
05:35.710 --> 05:36.310
Keyword.

80
05:38.320 --> 05:41.800
- plus 48.

81
05:43.360 --> 05:45.820
And then we add the sixth argument.

82
05:46.780 --> 05:52.960
Keyword - plus 56.

83
05:55.140 --> 05:56.040
56.

84
05:57.450 --> 05:59.340
Okay, I see. Then we need,

85
05:59.340 --> 06:02.610
so notice that we didn't use any of our local variables.

86
06:02.610 --> 06:03.210
Right.

87
06:03.210 --> 06:08.850
But if you wanted to, I already showed you in the earlier lesson how to make use of those local variables.

88
06:08.850 --> 06:10.740
In this case, we don't use them.

89
06:11.040 --> 06:17.100
But assuming you want to use the local variable to store the result, this is how we can do it.

90
06:17.580 --> 06:20.310
Let's say we want to store it in the first local variable,

91
06:20.310 --> 06:21.150
the result.

92
06:21.150 --> 06:24.750
So we can do this, move.

93
06:25.140 --> 06:26.550
Uh, keyword.

94
06:28.740 --> 06:33.270
And then here - minus 18.

95
06:33.450 --> 06:38.760
Sorry, minus eight here, - minus eight for the first local variable.

96
06:39.000 --> 06:40.740
And let's say you want to store the result there.

97
06:40.740 --> 06:43.080
So we will do something like this here.

98
06:44.610 --> 06:47.640
And finally, you want to clean the stack.

99
06:47.640 --> 06:59.970
So you will, uh, you will, uh, add, uh, - 32 like this, because here you created four local variables.

100
06:59.970 --> 07:01.440
So now to clean the stack.

101
07:02.710 --> 07:04.990
And then you will move

102
07:05.500 --> 07:06.610
- -.

103
07:06.880 --> 07:07.810
-

104
07:08.890 --> 07:11.410
And then you will pop -.

105
07:11.590 --> 07:15.130
And then you will return like that.

106
07:16.150 --> 07:17.830
Okay, so now we are ready to call.

107
07:18.310 --> 07:21.730
Put a breakpoint, run to the breakpoint, step over.

108
07:21.730 --> 07:29.350
Move all your four parameters to the registers, 1, 2, 3, 4.

109
07:30.310 --> 07:37.390
Then create space for the shadow plus, uh, fifth and sixth arguments on the stack.

110
07:38.170 --> 07:45.730
Move the fifth argument to the stack, - plus 20. - plus 20 is here.

111
07:45.760 --> 07:49.780
- is EF 65 DF0.

112
07:50.740 --> 07:56.530
See, when I click on this, I can see here - plus 20 is 06510.

113
07:57.490 --> 07:58.630
Fifth argument is there.

114
07:58.900 --> 08:01.180
Step over now.

115
08:01.180 --> 08:01.750
It's here.

116
08:01.780 --> 08:02.830
Sixth argument.

117
08:02.830 --> 08:03.820
Step over.

118
08:04.970 --> 08:07.610
This argument is going to move the.

119
08:08.340 --> 08:10.050
So now click on this.

120
08:10.050 --> 08:17.340
You see the sixth argument, six is there. - plus 28 is here, 65518 here.

121
08:17.340 --> 08:17.940
Sixth argument.

122
08:17.940 --> 08:19.410
So now you're ready to call.

123
08:20.350 --> 08:21.130
So you can,

124
08:21.130 --> 08:22.120
you are here now.

125
08:22.630 --> 08:26.230
Step into the call and you jump to the call, to the function.

126
08:26.230 --> 08:27.220
And then you move.

127
08:28.480 --> 08:29.050
Right.

128
08:29.110 --> 08:34.780
Then you create your local variables, 32 bytes, four local variables, you move, then you now you zero

129
08:34.840 --> 08:38.920
-, and then you add first number to -.

130
08:40.140 --> 08:45.270
You get one, add a second number, you get three, add a third number you get six.

131
08:45.510 --> 08:48.000
Add the fourth number, you get ten.

132
08:48.690 --> 08:50.730
Add the fifth number, you should get 15.

133
08:50.730 --> 08:55.110
So - plus 30, you can see from here is five.

134
08:55.350 --> 09:04.200
So - plus is over here. - plus 48, plus 48 is here, five.

135
09:04.200 --> 09:06.570
So you can take this five and add to six.

136
09:06.570 --> 09:09.300
So five plus ten will be 15.

137
09:10.260 --> 09:11.010
There you go.

138
09:11.040 --> 09:14.490
Now you can take - plus 38, which is six over here.

139
09:14.490 --> 09:16.500
Add to the 15.

140
09:16.500 --> 09:20.790
You should get 21, and you get 21, 16 plus five is 21.

141
09:20.790 --> 09:24.090
So this is hex, but you convert to decimal is 21.

142
09:24.540 --> 09:30.900
Next we're going to move this, the result of the addition to the local variable - minus 18.

143
09:30.900 --> 09:33.450
So - minus 18 is this location.

144
09:33.690 --> 09:41.460
So step over, and now click back on the above one. - minus 18 now has got the result of the addition, which

145
09:41.460 --> 09:43.710
is 21, 16 plus five.

146
09:43.710 --> 09:47.070
And it's at this address 658.

147
09:47.190 --> 09:49.530
When you click on this, you will see here as well.

148
09:49.530 --> 09:51.600
Or you click on this, you will see here as well.

149
09:51.960 --> 09:52.560
Right?

150
09:52.560 --> 09:57.120
When you want to revisit the previous location of the stack, you just click on it.

151
09:57.120 --> 10:00.450
You can see here and here, that's the result.

152
10:00.930 --> 10:01.500
All right.

153
10:01.710 --> 10:07.050
Now we clean the stack, clean the stack, and then clean this as well.

154
10:08.430 --> 10:11.190
Pop, return, and that's it.

155
10:11.190 --> 10:14.370
And now we clean the stack of the function caller.

156
10:14.550 --> 10:15.450
That's it.

157
10:15.570 --> 10:21.600
So this is how you can implement a function call with six parameters and also local variables.

158
10:21.600 --> 10:23.100
That's all for this video.

159
10:23.160 --> 10:24.810
Thank you for watching.