Hello and welcome.
In this new lesson, we are going to take a look at do-while loop.
Do-while loop is another variation of the loop.
So it looks like this.
You first have an integer counter which is assigned the value one.
Then you have integer B assigned the value three, and then you do while.
Notice now the expression is tested at the end of the block rather than at the start.
So now we do the block of the code once, then only do the test.
So you enter the loop first, and then you increase the counter by one.
And then counter now becomes two.
And then you test. Is two less than three?
If this, then it goes up and continues with the second loop. Counter
now increases to three, and then you test. Is counter less than or equal to three?
Yes.
So it goes up and enters the loop
again. Counter becomes four.
Then the test is, is four less than or equal to three?
No.
So it quits the loop.
So to implement this in assembly code, we can do it this way.
Now notice this time I make use of the memory more in the sense that I don't assign the counter
to -.
Instead, I directly manipulate the value in the memory.
And here also, I don't compare two registers.
Instead, I compare a register with the memory.
So this is another variation of how you can implement loops.
So the first thing you do is you increment the counter by one.
And then you move the counter to -.
And then you compare - with the memory in B, which holds the value for variable B.
And then if it is less, then you jump to the do-while.
So JLE means jump
if less than or equal. It goes up here, increases counter again, and then you compare, and then you move
the counter.
Move the new value of the counter to -, and you compare again.
So eventually you come to a situation where your - will be larger than three.
And then it will quit the loop.
So as usual, we have to go and look for the memory allocations.
So I just clear this and redo again so that you will see how it's being done.
So the first thing is to click on the dump and then go to memory map.
Notice I'm using template2 again.
And then right-click on BSS segment, follow in dump, and scroll down and look for empty location.
So we right-click this and then we copy the address.
This will be counter.
Then we go down.
Go back here and assign one to it.
So right-click, binary edit, and key 01, click okay.
Then we go down to another location.
Right-click, copy this address.
This one will be variable B.
We paste it here, and this will assign three to it.
So come back here, right-click, binary edit, and key in 03.
Click okay.
And the last one is do-while.
So for do-while, we will use this location because do-while is the address where the increment instruction
begins.
So we copy this one over here.
Copy address and paste it in here.
Now we are ready to code.
So we start by incrementing.
Then we copy our counter address.
And paste it in here.
Next, we MOV -. So MOV -.
Paste the address of counter here.
So
click okay.
Next, we compare -.
With the variable B.
So the address of variable B is
this. Copy it.
And paste it in here.
And finally, jump less than or equal to.
JLE,
and you copy the address of the do-while.
Paste it here and click okay.
Let's check to make sure it's correct.
First is increment the address
0x407080.
This is the value of counter.
Next, we copy that counter to -.
Next, we compare - with 0x4070A0.
So which is variable B.
And finally, we jump less or equal to
over here,
0x401564, which is the do-while loop.
Put a breakpoint here and let's run.
Then step over.
So now it's going to increment 0x407080, which is the counter.
It's going to increment.
Step over.
Now it becomes two.
Then it's going to move it to -.
Step over. Two is now in -.
Then it's going to compare -.
Which is two, compared with B, which is three.
Step over.
So is it less than or equal?
Yes.
So it jumps up.
As you can see, the red arrow is highlighted.
Jump is taken.
So it jumps back up and increases again the counter by one.
So counter is two, is going to become...
As you step over, it becomes three.
Then it moves the new value three into -.
So - now is three.
It compares -, which is three, with variable B, which is also three.
And then step over.
So since it is less than or equal, so it's going to jump up again.
Increase now the counter by one.
So now it's three.
When you step over, it becomes four.
Now it's going to move this four into -.
- is four.
Now it's going to compare four with three.
So is four less than or equal to three?
No. Step over.
And you can see this time the arrow is grayed out, which means jump is not taken, as shown here.
So step over, and it goes out of the loop.
So this is how you can implement a do-while loop inside assembly language inside x64dbg.
This time we use the memory more instead of using purely registers.
So that's all for this lesson.
Thank you for watching.