WEBVTT

1
00:01.040 --> 00:02.840
Hello and welcome back.

2
00:02.840 --> 00:08.690
In this lesson, we are going to study the while loop in C code.

3
00:09.050 --> 00:12.170
The while loop can be written as follows.

4
00:12.530 --> 00:17.270
We need to have a counter of type integer, and we assign one to it.

5
00:17.300 --> 00:23.420
Then we are going to assign three to variable B, and then we do a while loop.

6
00:23.420 --> 00:29.600
So in the while loop, the test is to test whether the counter is less than or equal to B.

7
00:29.600 --> 00:35.150
If it is less than or equal to B, then it will enter the loop and increase the counter.

8
00:36.020 --> 00:41.360
After that, it will go back to the while loop again on top, and now test whether counter is still less

9
00:41.360 --> 00:42.560
than or equal to B.

10
00:42.590 --> 00:43.640
It still is.

11
00:43.640 --> 00:47.030
Then it enters the loop the second time and increases the counter again.

12
00:47.330 --> 00:54.410
Eventually, it will come to a situation where the counter value will be larger than B, and this expression

13
00:54.410 --> 00:55.220
will be false.

14
00:55.220 --> 00:57.920
So when that is false, then it will exit the loop.

15
00:58.850 --> 01:03.170
So to implement this in assembly code, we can do it as follows.

16
01:03.170 --> 01:06.650
We use two registers, - and -.

17
01:06.650 --> 01:14.570
Then we move the value in variable counter to -, and we move the value in variable B to -.

18
01:14.690 --> 01:16.850
And then we enter the while loop.

19
01:16.970 --> 01:23.960
So the while loop is where you have the address here, which has got the instruction for comparing.

20
01:23.960 --> 01:28.520
So inside here we compare - with - value.

21
01:28.670 --> 01:37.850
And if - is greater than -, then it will jump JG to the end while. JG means jump if greater.

22
01:37.970 --> 01:41.840
But if - is not larger than -, then this will not jump.

23
01:41.840 --> 01:47.030
It will go straight and increment -, and then it will jump to while again.

24
01:47.210 --> 01:49.220
Then it will repeat itself again.

25
01:49.220 --> 01:55.220
Eventually, there will come a situation where - is larger than -, so if that happens, then the jump

26
01:55.220 --> 01:56.720
greater will happen.

27
01:56.720 --> 02:01.280
It will jump out and go to the end while, which is the address down here.

28
02:01.970 --> 02:10.400
So as usual, we need to find the addresses that we are going to use for counter and for variable B,

29
02:10.400 --> 02:16.190
and also the address in the text segment for the while and the end while addresses.

30
02:16.400 --> 02:18.590
So let's get started.

31
02:18.950 --> 02:24.950
First, we look for the addresses that we want to use in the BSS segment.

32
02:24.950 --> 02:28.160
Click on the dump and then go to the memory map.

33
02:28.160 --> 02:31.610
Right-click on the BSS segment.

34
02:31.610 --> 02:32.810
Follow in dump.

35
02:33.530 --> 02:34.490
Scroll down.

36
02:34.490 --> 02:41.180
Somewhere down here, look for blank addresses so we can use this one.

37
02:41.180 --> 02:42.680
Right-click on this.

38
02:42.680 --> 02:46.100
Copy the address and paste it here.

39
02:46.100 --> 02:50.450
This will be our counter address, and we are going to assign one to it.

40
02:50.450 --> 02:56.480
So right-click here and then binary edit and key 01.

41
02:58.820 --> 03:02.900
Next, we look at the address for variable B.

42
03:03.230 --> 03:09.380
We can use this one here. Right-click, copy the address, and paste it in here.

43
03:11.510 --> 03:14.240
And we are going to assign three to variable B.

44
03:14.510 --> 03:16.160
So right-click,

45
03:16.780 --> 03:21.880
binary edit, key 03, hit okay.

46
03:24.220 --> 03:26.470
So the next thing to do is to code.

47
03:26.560 --> 03:33.610
We still don't know where the while address will be, because the while address will be at the location

48
03:33.610 --> 03:38.950
where the compare instruction happens, but we don't know where the compare instruction will be at.

49
03:38.950 --> 03:40.930
So let us code these two lines first.

50
03:40.930 --> 03:41.920
Then we'll come to this.

51
03:41.920 --> 03:44.110
Then we know where the while address will be.

52
03:44.110 --> 03:46.420
So the first instruction is MOV.

53
03:54.380 --> 03:57.140
And this one will be the address for the counter.

54
03:57.140 --> 03:58.730
So we come to the counter.

55
03:58.730 --> 03:59.570
Copy it.

56
04:03.280 --> 04:05.020
And we paste it in here.

57
04:09.380 --> 04:10.520
Click okay.

58
04:11.090 --> 04:13.280
The second one will be MOV -.

59
04:20.670 --> 04:24.030
Then we come here and copy the address for -.

60
04:28.320 --> 04:30.060
And we paste it in here.

61
04:30.270 --> 04:30.990
Click

62
04:32.450 --> 04:33.440
okay.

63
04:34.340 --> 04:37.550
Next time we'll come to the address for compare.

64
04:37.550 --> 04:39.740
So now we know where is the while address.

65
04:39.740 --> 04:42.710
So we can go here and copy this address.

66
04:45.550 --> 04:47.200
And paste it in here.

67
04:49.470 --> 04:53.190
And at this address we will do this instruction, CMP.

68
05:00.980 --> 05:01.250
Here.

69
05:01.250 --> 05:01.880
Okay.

70
05:03.520 --> 05:05.980
Next, we will do the jump greater.

71
05:06.550 --> 05:15.430
So for the end while, we can safely jump right to the bottom and choose the address way down here.

72
05:16.150 --> 05:19.810
Right-click this, copy address.

73
05:19.810 --> 05:24.820
The reason why we jump all the way to the bottom is because after JG, there will be two more instructions.

74
05:24.820 --> 05:26.650
So this also will eat up some memory.

75
05:26.650 --> 05:28.420
That's why we jump all the way to the bottom.

76
05:29.860 --> 05:31.570
We then paste the address here.

77
05:32.620 --> 05:36.100
This will be your end while, end while address.

78
05:36.100 --> 05:38.350
Now we can do the jump greater.

79
05:38.350 --> 05:39.880
So let's go back up here.

80
05:43.500 --> 05:48.630
JG, and paste the end while address.

81
05:50.340 --> 05:51.600
Here, okay.

82
05:51.600 --> 06:07.350
And then we do the increment INC -, followed by JMP while, JMP, and then the address for the while, which

83
06:07.350 --> 06:08.640
is this address.

84
06:08.640 --> 06:09.990
So let's copy this.

85
06:13.060 --> 06:14.740
And paste it in here.

86
06:18.110 --> 06:19.400
All right, so now we are done.

87
06:19.400 --> 06:20.720
Let's double check.

88
06:21.050 --> 06:29.570
MOV -, this one, -, this one will be the counter address, which is 0x407080. MOV -,

89
06:29.810 --> 06:37.190
this one will be the variable B, 0x4070A0, variable B.

90
06:38.360 --> 06:45.290
And then compare - and -, and then jump greater

91
06:46.880 --> 06:49.370
to 0x4015BA.

92
06:50.030 --> 06:51.830
So jump greater to end while.

93
06:51.860 --> 06:53.390
So end while is 0x4015

94
06:53.420 --> 06:57.470
BA. Then increment -, and then jump while.

95
06:57.740 --> 07:03.470
So while will be the address 0x401572, 0x401572.

96
07:03.500 --> 07:04.400
So we are right.

97
07:04.550 --> 07:06.530
So make sure you have a breakpoint here.

98
07:06.530 --> 07:09.110
And then run to it and then step over.

99
07:09.710 --> 07:14.360
Now it's going to move the variable counter value into -.

100
07:14.720 --> 07:18.380
So our counter has got this address, 0x407080.

101
07:18.380 --> 07:19.760
So this is your counter address.

102
07:19.760 --> 07:21.440
It's going to move one to -.

103
07:21.650 --> 07:24.770
Step over and check. - is one.

104
07:25.550 --> 07:31.040
Next, it's going to move the variable B from this address. This is variable B.

105
07:32.090 --> 07:34.310
So it's going to move three to -.

106
07:34.310 --> 07:37.970
So let's step over and check -. - is three.

107
07:38.450 --> 07:40.640
Then it's going to compare - and -.

108
07:40.820 --> 07:42.020
So let's step over.

109
07:42.740 --> 07:50.540
And then you notice that it doesn't jump because the gray arrow is gray.

110
07:50.570 --> 07:51.440
It's not red.

111
07:51.560 --> 07:54.500
And here is also stated jump is not taken.

112
07:54.620 --> 07:59.810
And the reason is because - is not greater than -.

113
08:00.950 --> 08:04.730
- is one, - is three.

114
08:04.730 --> 08:12.830
So it doesn't jump because - is less than -, so it will go straight and increase - by one.

115
08:12.830 --> 08:16.100
So let's check - now. - now becomes two.

116
08:16.220 --> 08:17.450
Then it will jump.

117
08:17.720 --> 08:22.220
As you can see, the red arrow is lighted, so it will jump back up to the while loop.

118
08:22.220 --> 08:23.930
So this is the while address.

119
08:23.930 --> 08:26.510
So step over, and it jumps back up.

120
08:27.740 --> 08:31.700
And now it goes down to compare again, - with -.

121
08:31.940 --> 08:38.120
And again, it won't jump because - is two, which is still not greater than -, which is three.

122
08:38.270 --> 08:40.910
So it goes straight and increases - by one.

123
08:40.910 --> 08:43.850
- now becomes three, then it jumps back up.

124
08:44.840 --> 08:46.310
Now it compares again.

125
08:46.640 --> 08:55.610
Again, it won't jump because - is not greater than -, so it goes straight, increases -. - becomes four,

126
08:55.640 --> 08:57.410
then it jumps back up again.

127
08:58.190 --> 08:59.900
Compare - and -.

128
09:00.170 --> 09:05.540
This time it will jump because - now is greater than -, so it will jump.

129
09:05.720 --> 09:07.970
As you can see, the red arrow is lighted.

130
09:08.090 --> 09:10.970
Jump is taken, so it will jump all the way down here.

131
09:11.990 --> 09:14.780
So let's step over, and it jumps here.

132
09:15.140 --> 09:21.530
So this is how you can implement a while loop in assembly language inside x64dbg.

133
09:21.830 --> 09:23.360
That's all for this video.

134
09:23.360 --> 09:24.620
Thank you for watching.