WEBVTT

1
00:00.580 --> 00:02.140
Hello and welcome.

2
00:02.320 --> 00:06.130
In this new lesson, we are going to take a look at do-while loop.

3
00:06.700 --> 00:09.430
Do-while loop is another variation of the loop.

4
00:09.850 --> 00:11.470
So it looks like this.

5
00:12.040 --> 00:17.800
You first have an integer counter which is assigned the value one.

6
00:17.800 --> 00:22.810
Then you have integer B assigned the value three, and then you do while.

7
00:23.320 --> 00:30.670
Notice now the expression is tested at the end of the block rather than at the start.

8
00:30.850 --> 00:36.250
So now we do the block of the code once, then only do the test.

9
00:36.520 --> 00:41.530
So you enter the loop first, and then you increase the counter by one.

10
00:41.530 --> 00:44.470
And then counter now becomes two.

11
00:44.560 --> 00:47.770
And then you test. Is two less than three?

12
00:48.190 --> 00:52.180
If this, then it goes up and continues with the second loop. Counter

13
00:52.180 --> 00:57.910
now increases to three, and then you test. Is counter less than or equal to three?

14
00:57.940 --> 00:58.420
Yes.

15
00:58.420 --> 01:00.580
So it goes up and enters the loop

16
01:00.580 --> 01:02.530
again. Counter becomes four.

17
01:02.770 --> 01:06.100
Then the test is, is four less than or equal to three?

18
01:06.100 --> 01:06.520
No.

19
01:06.520 --> 01:07.660
So it quits the loop.

20
01:07.960 --> 01:12.250
So to implement this in assembly code, we can do it this way.

21
01:12.520 --> 01:20.710
Now notice this time I make use of the memory more in the sense that I don't assign the counter

22
01:20.710 --> 01:21.280
to -.

23
01:21.490 --> 01:25.840
Instead, I directly manipulate the value in the memory.

24
01:26.140 --> 01:29.920
And here also, I don't compare two registers.

25
01:29.920 --> 01:33.190
Instead, I compare a register with the memory.

26
01:33.190 --> 01:36.700
So this is another variation of how you can implement loops.

27
01:37.480 --> 01:40.390
So the first thing you do is you increment the counter by one.

28
01:40.390 --> 01:42.910
And then you move the counter to -.

29
01:42.910 --> 01:48.880
And then you compare - with the memory in B, which holds the value for variable B.

30
01:49.150 --> 01:54.130
And then if it is less, then you jump to the do-while.

31
01:54.130 --> 01:55.750
So JLE means jump

32
01:55.750 --> 02:01.300
if less than or equal. It goes up here, increases counter again, and then you compare, and then you move

33
02:01.300 --> 02:02.170
the counter.

34
02:02.170 --> 02:05.560
Move the new value of the counter to -, and you compare again.

35
02:05.950 --> 02:11.140
So eventually you come to a situation where your - will be larger than three.

36
02:11.560 --> 02:14.080
And then it will quit the loop.

37
02:14.440 --> 02:20.530
So as usual, we have to go and look for the memory allocations.

38
02:20.800 --> 02:25.360
So I just clear this and redo again so that you will see how it's being done.

39
02:27.330 --> 02:32.340
So the first thing is to click on the dump and then go to memory map.

40
02:32.370 --> 02:34.320
Notice I'm using template2 again.

41
02:35.040 --> 02:42.420
And then right-click on BSS segment, follow in dump, and scroll down and look for empty location.

42
02:44.230 --> 02:47.830
So we right-click this and then we copy the address.

43
02:48.400 --> 02:50.110
This will be counter.

44
02:54.080 --> 02:55.520
Then we go down.

45
02:55.850 --> 02:57.860
Go back here and assign one to it.

46
02:57.860 --> 03:04.190
So right-click, binary edit, and key 01, click okay.

47
03:04.970 --> 03:07.580
Then we go down to another location.

48
03:07.580 --> 03:10.160
Right-click, copy this address.

49
03:10.160 --> 03:12.170
This one will be variable B.

50
03:12.860 --> 03:16.430
We paste it here, and this will assign three to it.

51
03:16.430 --> 03:22.670
So come back here, right-click, binary edit, and key in 03.

52
03:23.900 --> 03:24.920
Click okay.

53
03:25.250 --> 03:27.170
And the last one is do-while.

54
03:27.170 --> 03:35.720
So for do-while, we will use this location because do-while is the address where the increment instruction

55
03:35.720 --> 03:36.530
begins.

56
03:36.710 --> 03:39.110
So we copy this one over here.

57
03:39.230 --> 03:42.620
Copy address and paste it in here.

58
03:45.750 --> 03:47.250
Now we are ready to code.

59
03:47.580 --> 03:51.780
So we start by incrementing.

60
03:57.990 --> 04:01.380
Then we copy our counter address.

61
04:04.540 --> 04:05.890
And paste it in here.

62
04:09.950 --> 04:15.590
Next, we MOV -. So MOV -.

63
04:21.290 --> 04:22.880
Paste the address of counter here.

64
04:22.880 --> 04:23.390
So

65
04:26.000 --> 04:27.260
click okay.

66
04:27.290 --> 04:29.240
Next, we compare -.

67
04:39.610 --> 04:42.370
With the variable B.

68
04:42.790 --> 04:45.070
So the address of variable B is

69
04:45.400 --> 04:46.750
this. Copy it.

70
04:50.220 --> 04:51.810
And paste it in here.

71
04:55.280 --> 04:58.370
And finally, jump less than or equal to.

72
05:00.980 --> 05:01.640
JLE,

73
05:01.640 --> 05:06.110
and you copy the address of the do-while.

74
05:11.460 --> 05:13.920
Paste it here and click okay.

75
05:15.030 --> 05:16.980
Let's check to make sure it's correct.

76
05:17.580 --> 05:20.610
First is increment the address

77
05:20.640 --> 05:22.260
0x407080.

78
05:23.550 --> 05:25.440
This is the value of counter.

79
05:26.550 --> 05:30.330
Next, we copy that counter to -.

80
05:31.500 --> 05:36.180
Next, we compare - with 0x4070A0.

81
05:36.630 --> 05:38.400
So which is variable B.

82
05:39.180 --> 05:42.510
And finally, we jump less or equal to

83
05:42.960 --> 05:43.380
over here,

84
05:43.380 --> 05:44.070
over here,

85
05:45.030 --> 05:48.480
0x401564, which is the do-while loop.

86
05:50.070 --> 05:52.170
Put a breakpoint here and let's run.

87
05:53.520 --> 05:54.600
Then step over.

88
05:55.930 --> 06:01.870
So now it's going to increment 0x407080, which is the counter.

89
06:02.440 --> 06:03.280
It's going to increment.

90
06:03.280 --> 06:04.180
Step over.

91
06:04.360 --> 06:05.680
Now it becomes two.

92
06:06.370 --> 06:08.410
Then it's going to move it to -.

93
06:08.740 --> 06:12.160
Step over. Two is now in -.

94
06:12.310 --> 06:14.560
Then it's going to compare -.

95
06:15.480 --> 06:20.400
Which is two, compared with B, which is three.

96
06:20.700 --> 06:21.660
Step over.

97
06:22.290 --> 06:24.090
So is it less than or equal?

98
06:24.120 --> 06:24.510
Yes.

99
06:24.510 --> 06:25.440
So it jumps up.

100
06:25.440 --> 06:28.020
As you can see, the red arrow is highlighted.

101
06:28.050 --> 06:29.220
Jump is taken.

102
06:29.220 --> 06:33.510
So it jumps back up and increases again the counter by one.

103
06:33.510 --> 06:35.640
So counter is two, is going to become...

104
06:36.630 --> 06:38.310
As you step over, it becomes three.

105
06:38.760 --> 06:42.660
Then it moves the new value three into -.

106
06:42.660 --> 06:44.280
So - now is three.

107
06:44.970 --> 06:50.520
It compares -, which is three, with variable B, which is also three.

108
06:51.120 --> 06:54.210
And then step over.

109
06:54.600 --> 06:58.950
So since it is less than or equal, so it's going to jump up again.

110
06:59.550 --> 07:02.340
Increase now the counter by one.

111
07:03.000 --> 07:04.110
So now it's three.

112
07:04.110 --> 07:06.540
When you step over, it becomes four.

113
07:06.840 --> 07:10.020
Now it's going to move this four into -.

114
07:10.470 --> 07:11.790
- is four.

115
07:11.820 --> 07:14.940
Now it's going to compare four with three.

116
07:14.940 --> 07:17.160
So is four less than or equal to three?

117
07:17.190 --> 07:18.840
No. Step over.

118
07:18.840 --> 07:26.100
And you can see this time the arrow is grayed out, which means jump is not taken, as shown here.

119
07:26.340 --> 07:28.980
So step over, and it goes out of the loop.

120
07:29.460 --> 07:37.530
So this is how you can implement a do-while loop inside assembly language inside x64dbg.

121
07:37.980 --> 07:44.460
This time we use the memory more instead of using purely registers.

122
07:44.910 --> 07:48.090
So that's all for this lesson.

123
07:48.090 --> 07:49.440
Thank you for watching.