WEBVTT

1
00:02.410 --> 00:03.910
Hello and welcome back.

2
00:03.910 --> 00:09.100
In this video, we are going to do another if-else statement.

3
00:09.220 --> 00:10.600
Earlier on, we did if statement.

4
00:10.600 --> 00:12.670
So this time we are going to do if-else.

5
00:13.390 --> 00:15.340
So if-else statement,

6
00:15.370 --> 00:16.270
it looks like this.

7
00:16.270 --> 00:23.380
Assuming we have two variables A and B, and we will perform an if statement to compare the values in

8
00:23.380 --> 00:24.280
A and B.

9
00:24.280 --> 00:30.700
So if A is larger than B, then we assign three to B, else we assign three to A, then we end if.

10
00:30.700 --> 00:34.870
So to implement this in assembly, we will do something like this.

11
00:34.870 --> 00:36.520
So there is an address here.

12
00:36.520 --> 00:47.080
The label is if. Move QWORD PTR, the value in A to -, and then compare with the B, and if it's greater,

13
00:47.080 --> 00:49.120
then we jump greater.

14
00:49.120 --> 00:52.690
So we jump to the else label, which is also another address.

15
00:52.690 --> 00:59.050
However, if - is not larger than B, then we will not jump.

16
00:59.050 --> 01:03.850
Instead, we will continue to step through and perform this assignment.

17
01:03.850 --> 01:11.800
We will move three to A, and then when we finish doing that, we will jump to another address.

18
01:11.800 --> 01:15.820
And if we don't jump here, it will step through and perform the else part.

19
01:15.820 --> 01:18.250
That's why it is important to jump here.

20
01:18.760 --> 01:21.340
So all these are actually just addresses.

21
01:21.340 --> 01:25.330
So we have to find the addresses for A, B, else, and endif.

22
01:26.230 --> 01:26.590
Right.

23
01:26.590 --> 01:32.020
So the first thing we do is open your template2.exe.

24
01:32.020 --> 01:35.650
And then we will look for addresses for all of these.

25
01:35.890 --> 01:42.520
So A, B will be addresses in your BSS segment because they are variables. Else

26
01:42.520 --> 01:46.960
and endif will be the addresses in your instruction section,

27
01:46.960 --> 01:49.960
here, your text section, which is your instruction section.

28
01:50.380 --> 01:57.310
So for A and B, let's select dump one first and then go to memory map and look at BSS.

29
01:57.310 --> 01:59.350
Right-click on it and follow in dump.

30
01:59.950 --> 02:04.570
And here we will select two addresses, one for A and one for B.

31
02:04.570 --> 02:09.610
So let's scroll down somewhere at the bottom here and let us select this for A.

32
02:09.640 --> 02:15.640
So right-click this address, copy address, and then we paste it in here.

33
02:15.940 --> 02:18.490
So this is your first address.

34
02:18.760 --> 02:21.370
And then now we go to another location.

35
02:21.370 --> 02:23.170
Select this one for B.

36
02:23.830 --> 02:26.800
We copy the address and we paste it in here.

37
02:28.840 --> 02:30.130
So that is done.

38
02:30.850 --> 02:31.240
Else

39
02:31.240 --> 02:36.790
and endif, we will look somewhere down here at the bottom. We scroll down somewhere to the bottom.

40
02:36.790 --> 02:44.110
And maybe here we would do else over here, or maybe high up here, else.

41
02:44.110 --> 02:49.390
So we just right-click this address, copy this address, and we put it here.

42
02:50.860 --> 02:52.960
And endif will be the end of the if statement.

43
02:52.960 --> 02:55.300
So we can safely put it somewhere down here.

44
02:55.300 --> 03:00.250
So right-click this, copy the address, and put it down here.

45
03:01.090 --> 03:02.830
So we have four addresses.

46
03:03.220 --> 03:07.750
So let's go to the top here now and code all this.

47
03:07.930 --> 03:11.890
So the first one is to move this - QWORD PTR A.

48
03:11.890 --> 03:14.230
So we copy this address A first.

49
03:16.510 --> 03:21.970
Copy this, and then we go to this location and assemble our move.

50
03:28.910 --> 03:29.960
So that is done.

51
03:29.960 --> 03:31.760
We paste our address that we copied.

52
03:32.270 --> 03:35.000
Next one would be compare this with B.

53
03:35.000 --> 03:37.130
So let's copy B's address.

54
03:41.320 --> 03:43.660
So we come back here, we compare.

55
03:52.280 --> 03:58.160
Paste the address for B, hit okay, then we will jump to else if greater.

56
03:58.160 --> 04:00.740
So over here we copy the else address.

57
04:03.850 --> 04:08.290
Come back here and we paste our address there.

58
04:08.290 --> 04:13.720
Be sure to put the 0x in front of all addresses, the prefix, and then

59
04:13.720 --> 04:14.440
okay.

60
04:14.590 --> 04:18.760
And then we do this, move this to address A.

61
04:18.760 --> 04:20.290
So you copy address A again.

62
04:38.330 --> 04:41.600
And then we jump to endif, endif address over here.

63
04:41.600 --> 04:42.980
Let's copy the address.

64
04:52.720 --> 04:54.610
And then finally the else part.

65
04:54.610 --> 04:59.410
So we go to the else part over here and do this instruction.

66
04:59.410 --> 05:00.790
So we go to the else part.

67
05:01.000 --> 05:04.180
So the else part is somewhere down here,

68
05:04.180 --> 05:06.730
0x401586.

69
05:06.730 --> 05:12.910
So we go down to 0x401586 over here.

70
05:12.910 --> 05:13.960
586.

71
05:14.050 --> 05:16.990
So over here we will do this instruction.

72
05:17.860 --> 05:20.410
We will copy this, three to B.

73
05:20.410 --> 05:21.610
So address B is here.

74
05:21.610 --> 05:23.170
So we copy the address first.

75
05:27.170 --> 05:30.080
Come back to this one and then we move.

76
05:34.920 --> 05:35.910
Move three there.

77
05:36.750 --> 05:36.900
Here.

78
05:36.900 --> 05:37.440
Okay.

79
05:39.450 --> 05:40.980
All right, so it's done.

80
05:40.980 --> 05:45.750
So now just do a check to see we got it correct.

81
05:46.050 --> 05:48.330
So the first one is move

82
05:48.360 --> 05:50.730
-, 0x407100.

83
05:51.330 --> 05:55.020
Move -, 0x407100.

84
05:55.110 --> 05:57.000
Next one is compare with

85
05:57.000 --> 06:02.820
0x407120, 0x407120.

86
06:03.360 --> 06:06.480
Next one is jump 0x401586.

87
06:07.260 --> 06:11.460
Jump to else, which is 0x401586.

88
06:11.460 --> 06:11.850
Correct.

89
06:12.180 --> 06:16.650
Next is move 0x407103.

90
06:17.220 --> 06:21.450
Move 0x407103, and last.

91
06:21.450 --> 06:24.150
Next one is jump to 0x4015B9.

92
06:24.390 --> 06:27.930
Jump to 0x4015B9.

93
06:28.170 --> 06:34.170
And then the last one will be this statement at the else address.

94
06:34.170 --> 06:36.480
So else address is over here,

95
06:37.080 --> 06:44.220
0x401586, where we move B to three.

96
06:44.220 --> 06:49.530
So B is 0x407120, 0x407120, 3.

97
06:49.530 --> 06:49.890
Correct.

98
06:49.890 --> 06:51.660
So we are ready to execute this now.

99
06:51.870 --> 06:56.970
So we run. Before that, we have to put some values in the variables.

100
06:56.970 --> 07:02.610
So for A, we are going to put one. For B, we are going to put two.

101
07:02.700 --> 07:12.750
So we go to variable A, 0x407100, 0x407100.

102
07:13.170 --> 07:16.140
Right-click here and binary edit.

103
07:16.140 --> 07:18.030
And we put one over there.

104
07:19.080 --> 07:21.600
Be sure to put 01 and not 10, okay?

105
07:21.660 --> 07:23.760
Because it's little-endian convention.

106
07:23.760 --> 07:26.370
So when you read this back, it reads in reverse order.

107
07:26.370 --> 07:32.520
It will be 00, 00, 00, 00, 00, all the way to the end, 01.

108
07:32.520 --> 07:34.800
So this means one, not ten.

109
07:34.800 --> 07:34.950
Yeah.

110
07:34.950 --> 07:36.960
You don't put 10 here, 01.

111
07:36.960 --> 07:42.210
So this is variable A. Variable A is now having the value one.

112
07:42.210 --> 07:45.960
Next, we are going to initialize variable B with two.

113
07:45.990 --> 07:50.160
So variable B is this address, 0x407120, over here.

114
07:50.340 --> 07:52.770
So we are going to assign two there.

115
07:52.770 --> 07:55.560
So right-click here, binary edit.

116
07:55.560 --> 07:59.340
And here we put 02, okay.

117
07:59.730 --> 08:03.120
So now we got one in A and two in B.

118
08:03.120 --> 08:04.500
So we are ready to run.

119
08:04.500 --> 08:06.090
So we run to our breakpoint.

120
08:06.090 --> 08:11.940
Step over. It's going to move variable A to -.

121
08:12.300 --> 08:13.860
Step over. -

122
08:14.040 --> 08:17.460
now is one because it just moved one from variable A to -.

123
08:17.760 --> 08:20.880
Next, it's going to compare - with variable two.

124
08:20.880 --> 08:24.240
Variable B, sorry. Variable B is this one with two.

125
08:24.270 --> 08:25.650
So it's going to compare that.

126
08:26.040 --> 08:29.460
So after compare, it's going to test whether variable A is bigger.

127
08:29.460 --> 08:31.380
If variable A is bigger, then it will jump.

128
08:31.380 --> 08:32.670
But in this case, it is not.

129
08:32.670 --> 08:33.870
So it doesn't jump.

130
08:33.870 --> 08:37.590
That's why the grayed-out arrow is shown instead of red.

131
08:38.220 --> 08:39.510
So now it's not going to jump.

132
08:39.510 --> 08:41.250
It's going to move forward straight away.

133
08:41.640 --> 08:42.750
It's going to move forward.

134
08:43.140 --> 08:45.450
And it's going to move three to variable A.

135
08:45.480 --> 08:47.070
So variable A is this.

136
08:47.070 --> 08:48.000
So it's going to move three.

137
08:48.000 --> 08:51.060
Then step over, and you find three is being moved there.

138
08:51.780 --> 08:55.560
Then finally, it's going to jump all the way out into the endif part.

139
08:55.560 --> 09:00.930
So step over. You see that the red arrow here, red arrow is highlighted, meaning it's going to jump

140
09:00.930 --> 09:01.140
here.

141
09:01.140 --> 09:02.040
So step over.

142
09:02.250 --> 09:04.050
And true enough, it jumps over here.

143
09:04.530 --> 09:09.930
So this is how we can implement if-else statements inside your assembly.

144
09:09.930 --> 09:11.400
You need to do some planning.

145
09:11.400 --> 09:16.710
You have to set up all the addresses for the variables and the instructions before you can implement

146
09:16.710 --> 09:16.950
it.

147
09:17.100 --> 09:18.690
So that's all for this video.

148
09:18.690 --> 09:19.860
Thank you for watching.