Hello and welcome back.
In this lesson, we are going to learn how to swap data between two memory addresses.
We thought that maybe you could do something like this, but this is an illegal instruction.
It is not permitted to exchange directly two memory addresses.
So how do we solve this?
The tip:
Use the register.
So just pause the video and give this a try on your own, and then come back later and watch the solution
below.
So good luck.
Okay.
I hope that you gave it a try.
Let's scroll down to see the solution.
So the solution is to move memory,
the first memory to the - register.
This is a temporary register to hold the data in memory one.
Then we swap the memory two with the register.
And finally, we move whatever is in the register to memory one.
So let's try this now in the x64dbg.
I've opened up my template two and let's
now put a breakpoint and run to the breakpoint.
Step over.
So now we come to this line.
We are going to put data in memory one and two.
So let's go to memory map and select the data segment and select two addresses.
First address.
Maybe we can use this one.
So we copy this out, copy address and put it here.
Then,
now let's choose another address.
Maybe the one after that.
The next one.
Let's copy this address and put it here as well.
Let's go back to the first one.
The first one is here.
So we select this address and put some value there.
So binary edit. We'll put 1111.
Okay.
Now we go to the second address and right-click and we binary edit.
And we put 2222.
So now the challenge is to swap data between these two addresses.
That means 1111.
You want to swap it to over here.
Here should become 1111 and 2222.
We will swap it up.
So 2222 will move up here.
So at the end of this, you should have 2222 on top and 1111 below.
So let's do that now.
First step is to move memory one to -.
So this is memory one.
Memory one is containing the value 1111.
What if we try to do this directly?
XCHG two memory directly.
Let's try that and see what happens.
We put
memory one and now memory two.
And you will see the error here.
Instruction encoding error.
Invalid instruction.
So this proves that you cannot swap memory directly.
You have to use the register to act as a temporary holding area.
So the first step is to move memory one to -.
So this one doesn't work.
So let's cancel this and do it the correct way.
So the correct way first is to move your memory to -.
So let's do that now.
Memory one.
Copy memory one and paste it here.
Now we step over this.
In memory one,
whatever is in memory one is now copied to -.
Next one is exchange with memory two.
That means whatever is here, you want to swap it with memory two.
So let's do that.
Now we copy memory two.
Paste memory two.
Hit
okay.
Now we step over this and we should see this.
- 1111 will be swapped with memory two.
You know it has been swapped.
1111 is now in the memory.
Memory two.
And whatever was in memory two has been swapped into -.
So the final step is to move this value in - to memory one.
So we assemble.
Move.
We copy
memory one.
And we paste here.
Okay.
And now we step over this.
And we see now memory one now has got the value two two two two.
So this is how you can swap values in two memories.
You need to use a temporary register as a holding area.
So that's all for this video.
Thank you for watching.