WEBVTT

1
00:00.440 --> 00:02.180
Hello and welcome back.

2
00:02.180 --> 00:07.820
In this lesson, we are going to learn how to swap data between two memory addresses.

3
00:08.630 --> 00:14.360
We thought that maybe you could do something like this, but this is an illegal instruction.

4
00:14.360 --> 00:19.040
It is not permitted to exchange directly two memory addresses.

5
00:19.040 --> 00:20.900
So how do we solve this?

6
00:22.520 --> 00:23.090
The tip:

7
00:23.840 --> 00:25.370
Use the register.

8
00:26.630 --> 00:35.090
So just pause the video and give this a try on your own, and then come back later and watch the solution

9
00:35.090 --> 00:35.870
below.

10
00:37.810 --> 00:38.890
So good luck.

11
00:43.030 --> 00:43.600
Okay.

12
00:43.600 --> 00:45.190
I hope that you gave it a try.

13
00:45.370 --> 00:47.620
Let's scroll down to see the solution.

14
00:47.980 --> 00:50.830
So the solution is to move memory,

15
00:51.040 --> 00:54.670
the first memory to the - register.

16
00:55.690 --> 01:00.610
This is a temporary register to hold the data in memory one.

17
01:01.210 --> 01:04.480
Then we swap the memory two with the register.

18
01:05.440 --> 01:09.190
And finally, we move whatever is in the register to memory one.

19
01:09.940 --> 01:12.550
So let's try this now in the x64dbg.

20
01:13.510 --> 01:17.440
I've opened up my template two and let's

21
01:17.860 --> 01:21.370
now put a breakpoint and run to the breakpoint.

22
01:21.880 --> 01:22.840
Step over.

23
01:23.830 --> 01:25.720
So now we come to this line.

24
01:26.080 --> 01:29.620
We are going to put data in memory one and two.

25
01:30.040 --> 01:36.520
So let's go to memory map and select the data segment and select two addresses.

26
01:37.360 --> 01:38.230
First address.

27
01:38.230 --> 01:39.910
Maybe we can use this one.

28
01:40.270 --> 01:45.910
So we copy this out, copy address and put it here.

29
01:50.890 --> 01:51.730
Then,

30
01:51.730 --> 01:54.280
now let's choose another address.

31
01:57.300 --> 01:59.130
Maybe the one after that.

32
01:59.910 --> 02:00.570
The next one.

33
02:00.570 --> 02:03.570
Let's copy this address and put it here as well.

34
02:05.910 --> 02:07.530
Let's go back to the first one.

35
02:08.070 --> 02:09.330
The first one is here.

36
02:09.330 --> 02:12.600
So we select this address and put some value there.

37
02:13.440 --> 02:18.240
So binary edit. We'll put 1111.

38
02:18.900 --> 02:19.680
Okay.

39
02:21.540 --> 02:28.500
Now we go to the second address and right-click and we binary edit.

40
02:29.550 --> 02:31.830
And we put 2222.

41
02:33.210 --> 02:38.670
So now the challenge is to swap data between these two addresses.

42
02:38.670 --> 02:41.010
That means 1111.

43
02:41.040 --> 02:44.520
You want to swap it to over here.

44
02:45.030 --> 02:48.810
Here should become 1111 and 2222.

45
02:48.840 --> 02:50.160
We will swap it up.

46
02:50.370 --> 02:52.500
So 2222 will move up here.

47
02:52.530 --> 02:57.930
So at the end of this, you should have 2222 on top and 1111 below.

48
02:58.560 --> 02:59.850
So let's do that now.

49
03:00.720 --> 03:03.690
First step is to move memory one to -.

50
03:03.810 --> 03:05.370
So this is memory one.

51
03:06.750 --> 03:10.020
Memory one is containing the value 1111.

52
03:10.290 --> 03:13.590
What if we try to do this directly?

53
03:15.220 --> 03:17.320
XCHG two memory directly.

54
03:18.430 --> 03:20.140
Let's try that and see what happens.

55
03:25.030 --> 03:25.570
We put

56
03:27.140 --> 03:31.280
memory one and now memory two.

57
03:36.390 --> 03:38.490
And you will see the error here.

58
03:39.780 --> 03:41.370
Instruction encoding error.

59
03:41.370 --> 03:42.360
Invalid instruction.

60
03:42.600 --> 03:45.780
So this proves that you cannot swap memory directly.

61
03:45.780 --> 03:52.380
You have to use the register to act as a temporary holding area.

62
03:52.950 --> 03:55.920
So the first step is to move memory one to -.

63
03:56.100 --> 03:57.630
So this one doesn't work.

64
03:57.630 --> 04:00.900
So let's cancel this and do it the correct way.

65
04:01.560 --> 04:07.260
So the correct way first is to move your memory to -.

66
04:07.680 --> 04:09.330
So let's do that now.

67
04:14.200 --> 04:15.310
Memory one.

68
04:21.680 --> 04:25.790
Copy memory one and paste it here.

69
04:31.850 --> 04:33.380
Now we step over this.

70
04:35.320 --> 04:36.280
In memory one,

71
04:36.280 --> 04:40.000
whatever is in memory one is now copied to -.

72
04:40.480 --> 04:43.210
Next one is exchange with memory two.

73
04:43.210 --> 04:47.080
That means whatever is here, you want to swap it with memory two.

74
04:47.890 --> 04:48.700
So let's do that.

75
04:48.700 --> 04:50.350
Now we copy memory two.

76
05:04.990 --> 05:06.130
Paste memory two.

77
05:08.470 --> 05:08.710
Hit

78
05:08.710 --> 05:09.340
okay.

79
05:10.570 --> 05:13.780
Now we step over this and we should see this.

80
05:14.200 --> 05:17.470
- 1111 will be swapped with memory two.

81
05:20.340 --> 05:22.350
You know it has been swapped.

82
05:22.980 --> 05:25.590
1111 is now in the memory.

83
05:25.620 --> 05:26.700
Memory two.

84
05:26.700 --> 05:30.720
And whatever was in memory two has been swapped into -.

85
05:31.320 --> 05:36.120
So the final step is to move this value in - to memory one.

86
05:38.670 --> 05:40.680
So we assemble.

87
05:40.680 --> 05:41.490
Move.

88
05:48.120 --> 05:48.660
We copy

89
05:48.660 --> 05:49.410
memory one.

90
05:52.830 --> 05:54.150
And we paste here.

91
06:00.730 --> 06:01.300
Okay.

92
06:02.560 --> 06:04.120
And now we step over this.

93
06:05.120 --> 06:10.760
And we see now memory one now has got the value two two two two.

94
06:11.450 --> 06:16.190
So this is how you can swap values in two memories.

95
06:16.430 --> 06:20.930
You need to use a temporary register as a holding area.

96
06:21.410 --> 06:23.150
So that's all for this video.

97
06:23.150 --> 06:24.380
Thank you for watching.