WEBVTT

1
00:01.100 --> 00:01.700
Welcome back.

2
00:01.700 --> 00:05.870
Now we are going to do the same thing, but this time we are going to create four local variables.

3
00:05.960 --> 00:10.430
So same way we will move our

4
00:12.950 --> 00:14.210
first parameter.

5
00:15.020 --> 00:20.000
Move the second parameter to -, which is two.

6
00:21.020 --> 00:30.110
Move the third parameter to -, three, and then move the fourth parameter to -.

7
00:32.480 --> 00:33.530
Which is four.

8
00:33.590 --> 00:34.100
All right.

9
00:35.550 --> 00:40.560
Okay, so next thing we need to do is to create the shadow space.

10
00:40.710 --> 00:42.930
So we sub -

11
00:45.780 --> 00:46.530
32.

12
00:47.820 --> 00:48.930
Then we make a call.

13
00:50.580 --> 00:52.290
So the call here is same.

14
00:52.290 --> 00:55.080
We want to use the same address for the function.

15
00:55.380 --> 01:01.440
So we copy this address and we make a call to that address.

16
01:04.490 --> 01:06.200
So we will call here.

17
01:06.500 --> 01:08.150
And here we do the same thing.

18
01:08.150 --> 01:10.490
We push -.

19
01:12.020 --> 01:13.490
Push -.

20
01:15.630 --> 01:21.090
Move - -, and now we are going to create four local variables.

21
01:21.090 --> 01:29.400
So we need to do sub - 32, because each variable takes up eight bytes or quadword.

22
01:29.820 --> 01:32.160
Since we have four, four times eight is 32.

23
01:33.690 --> 01:36.120
How do we make use of those local variables?

24
01:36.180 --> 01:46.860
For example, if we want to store, um, your first parameter to the first address

25
01:48.500 --> 01:49.880
of the local variable.

26
01:50.420 --> 01:51.590
What do you do?

27
01:51.620 --> 01:52.640
Take a look at this.

28
01:53.150 --> 01:57.320
So if you want access to those local variables, you need to use offsets.

29
01:57.350 --> 01:58.370
Take a look at this.

30
01:58.760 --> 02:01.640
- minus eight is local variable number one.

31
02:01.640 --> 02:05.390
Minus 16 is the second one, and third one, fourth one, and so on.

32
02:05.960 --> 02:11.600
So we're going to use this as our guide to access those local variables.

33
02:12.110 --> 02:13.640
Okay, so the first one.

34
02:13.730 --> 02:15.140
- minus eight.

35
02:15.860 --> 02:20.480
So let's say we want to save the first argument to that local variable.

36
02:20.510 --> 02:21.290
- minus eight.

37
02:22.100 --> 02:23.210
This is how we do it.

38
02:24.020 --> 02:24.770
We move

39
02:28.080 --> 02:31.560
-, you have to dereference it, - minus eight.

40
02:32.220 --> 02:34.230
This is the first local variable.

41
02:35.430 --> 02:35.790
-.

42
02:38.290 --> 02:38.680
All right.

43
02:38.680 --> 02:40.000
Next one will be

44
02:42.590 --> 02:48.740
second local variable will be - minus 16.

45
02:50.670 --> 02:51.660
And then this one,

46
02:51.660 --> 02:52.650
- will go there.

47
02:54.780 --> 02:58.290
Do the same thing for the third local variable.

48
03:00.360 --> 03:03.210
Minus 24, and - will go there.

49
03:05.560 --> 03:07.600
And the last local variable.

50
03:10.770 --> 03:16.560
- minus 32, and - will go to

51
03:17.780 --> 03:18.260
there.

52
03:21.080 --> 03:21.410
All right.

53
03:21.410 --> 03:21.800
So.

54
03:22.370 --> 03:27.290
So this is just to demonstrate, uh, how to create local variables and use them.

55
03:27.770 --> 03:28.160
Right.

56
03:28.160 --> 03:30.290
It is not meant to be optimized code.

57
03:30.290 --> 03:30.620
Right.

58
03:30.620 --> 03:32.270
So just to demonstrate.

59
03:32.270 --> 03:39.860
So now you can do your addition and do whatever you want using, uh, this address of

60
03:39.860 --> 03:41.480
all these four variables, okay.

61
03:41.480 --> 03:42.890
Let's say now you want to do an addition.

62
03:42.890 --> 03:44.720
Let's say, let's demonstrate.

63
03:44.840 --> 03:49.070
So you XOR - with itself.

64
03:49.910 --> 03:58.310
And then you're going to add - with the first local variable, which is keyword - minus eight.

65
04:04.080 --> 04:06.900
Repeat the second one for the second one.

66
04:08.650 --> 04:11.350
And I need to remind you, this is not optimized code.

67
04:12.970 --> 04:17.260
This is just to demonstrate how to access local variables.

68
04:26.690 --> 04:27.170
Okay.

69
04:27.170 --> 04:32.270
Next one should be 24, which is 24.

70
04:34.840 --> 04:36.520
The next one will be

71
04:44.400 --> 04:45.120
32.

72
04:47.780 --> 04:50.570
So, uh, that's it.

73
04:53.210 --> 04:59.450
So at this point, your - will have the result of the additions.

74
04:59.750 --> 05:13.250
So at this point we need to move - -, and then we do a pop -, and then a return.

75
05:14.460 --> 05:15.060
Like this.

76
05:16.350 --> 05:16.980
Next,

77
05:16.980 --> 05:21.480
we are going to come back here after this return and we clean the stack.

78
05:22.380 --> 05:31.170
So we clean the stack the same way we use add - 32, because we allocate 32 bytes for shadow space

79
05:31.170 --> 05:31.560
over here.

80
05:31.560 --> 05:32.490
So we need to clean it.

81
05:34.740 --> 05:40.320
Note that if you over here, if you want to access the result of - and do something with it, you can

82
05:40.320 --> 05:40.830
do it this way.

83
05:41.130 --> 05:47.340
For example, if you want to move the result of the addition to register -, you could do something

84
05:47.340 --> 05:48.090
like this.

85
05:50.000 --> 05:55.250
Because when the function returns, it will have the result of the addition in -, and you can move

86
05:55.250 --> 05:58.280
it, save it to - or whatever you want to do with it.

87
05:58.910 --> 06:02.450
So we put a breakpoint here and run to the breakpoint and step over it.

88
06:03.380 --> 06:06.350
Move all the four parameters to the registers.

89
06:06.410 --> 06:09.410
So we have 1, 2, 3, 4 here.

90
06:09.410 --> 06:12.110
Then you create a shadow space.

91
06:12.500 --> 06:15.590
Then you step into the function call.

92
06:16.980 --> 06:18.780
So now you're inside.

93
06:20.090 --> 06:24.410
So now you created your new base pointer by doing these two lines.

94
06:24.500 --> 06:28.670
So your base pointer is now 654.

95
06:28.670 --> 06:33.560
So at this point in time, you are already done this.

96
06:34.340 --> 06:35.780
You created the base pointer.

97
06:35.780 --> 06:42.680
So now you're going to create these four local variables by doing this, by - minus 32.

98
06:42.710 --> 06:45.230
So - was previously here.

99
06:45.230 --> 06:48.680
So minus 1, 2, 3, 4, 4 times 8, 32.

100
06:48.710 --> 06:51.740
So - minus 32, you create four local variables.

101
06:52.430 --> 06:56.990
That's why we're doing this - minus, sub - 32.

102
06:57.170 --> 07:00.410
This is hex 20, which is 32 in decimal.

103
07:01.130 --> 07:06.620
So next, once you created the four local variables, then we are going to assign the arguments to it.

104
07:06.620 --> 07:10.760
So the first argument is - minus eight.

105
07:10.760 --> 07:13.130
Here, - minus eight.

106
07:13.370 --> 07:16.040
So we're going to assign - to that.

107
07:16.370 --> 07:18.260
So let's do that now.

108
07:18.650 --> 07:20.810
So let's step over.

109
07:22.480 --> 07:23.530
-.

110
07:24.160 --> 07:25.990
Now look, - minus eight.

111
07:26.230 --> 07:33.760
- is 65 4065 FD F0, which is here.

112
07:34.360 --> 07:35.650
Minus eight is here.

113
07:35.980 --> 07:38.140
So the first variable is assigned there.

114
07:38.140 --> 07:46.570
So FD F0 is referring to this location -, and then - minus eight above it is referring to this.

115
07:48.940 --> 07:54.160
And then we have just stored the argument - into it, which is one.

116
07:54.160 --> 07:56.200
So now we're going to do the next one.

117
07:56.200 --> 07:59.320
We're going to save - into second variable.

118
07:59.320 --> 07:59.980
Step over.

119
08:00.670 --> 08:05.590
And now you see second variable is stored in - minus 16.

120
08:06.700 --> 08:08.200
Now we step over.

121
08:08.200 --> 08:12.820
Next one, - is going to store - minus 24.

122
08:12.970 --> 08:21.820
So you see - minus 24 is here. - is here, minus 8, minus 16, minus 24.

123
08:21.820 --> 08:23.410
So this is - minus 24.

124
08:23.440 --> 08:25.450
This is where you stored -.

125
08:26.050 --> 08:29.980
Now we're going to store - into the fourth local variable. Step over.

126
08:30.130 --> 08:35.590
And you see the fourth local variable is stored with - minus 32, which is here.

127
08:35.590 --> 08:40.510
So this is how you can create local variables and access them and use them.

128
08:40.960 --> 08:42.700
Next we are going to do the addition.

129
08:42.700 --> 08:45.760
So we XOR -, - becomes zero.

130
08:45.760 --> 08:47.800
Then we start adding them.

131
08:47.800 --> 08:52.390
First we take - minus eight, which is here, add to -.

132
08:54.700 --> 09:00.520
So - becomes one. Next is the - minus 16, which is here.

133
09:02.970 --> 09:06.420
You can also see from here, - minus 16 is two.

134
09:08.070 --> 09:10.470
Add two to -, so - becomes three.

135
09:10.470 --> 09:11.700
One plus two is three.

136
09:11.850 --> 09:13.650
Next is - minus 18.

137
09:13.650 --> 09:15.660
- minus 18 is.

138
09:15.660 --> 09:18.180
Remember, this is hex here. Minus 18 is three.

139
09:18.180 --> 09:19.110
So this is three.

140
09:19.110 --> 09:24.570
Over here, you see the address FD8, same as this FD8 here.

141
09:26.460 --> 09:26.790
Let's add it.

142
09:27.530 --> 09:31.880
So we're gonna take that three there, add to whatever is already in there.

143
09:32.030 --> 09:32.720
So three.

144
09:32.720 --> 09:34.640
So three plus three should give you a six there.

145
09:35.150 --> 09:36.500
And now you got six.

146
09:36.620 --> 09:40.250
Next, we're going to access - minus 32.

147
09:40.760 --> 09:46.730
Minus 32 is four at this address 6540 here at E0.

148
09:46.790 --> 09:51.530
So - is here. - minus 32 is here.

149
09:51.950 --> 09:54.860
So you take this four, add to whatever is in -.

150
09:56.400 --> 09:58.680
So - becomes ten. Then

151
09:58.680 --> 09:59.790
now we're going to clean the stack.

152
09:59.790 --> 10:02.130
So we move - into -.

153
10:03.420 --> 10:07.260
Pop -, and we return to the line after this.

154
10:07.290 --> 10:09.750
Now we clean the stack in the main function.

155
10:10.950 --> 10:14.430
Then we now take the result in - and do whatever you want.

156
10:14.430 --> 10:16.830
In this case, we just move it to -.

157
10:17.370 --> 10:19.890
And now we see - has got the result ten.

158
10:20.340 --> 10:27.630
So this is how we can create functions with four arguments and four local variables.

159
10:27.750 --> 10:29.100
That's all for this video.

160
10:29.100 --> 10:30.480
Thank you for watching.