Hello and welcome back.
In this lesson, we are going to study the while loop in C code.
The while loop can be written as follows.
We need to have a counter of type integer, and we assign one to it.
Then we are going to assign three to variable B, and then we do a while loop.
So in the while loop, the test is to test whether the counter is less than or equal to B.
If it is less than or equal to B, then it will enter the loop and increase the counter.
After that, it will go back to the while loop again on top, and now test whether counter is still less
than or equal to B.
It still is.
Then it enters the loop the second time and increases the counter again.
Eventually, it will come to a situation where the counter value will be larger than B, and this expression
will be false.
So when that is false, then it will exit the loop.
So to implement this in assembly code, we can do it as follows.
We use two registers, - and -.
Then we move the value in variable counter to -, and we move the value in variable B to -.
And then we enter the while loop.
So the while loop is where you have the address here, which has got the instruction for comparing.
So inside here we compare - with - value.
And if - is greater than -, then it will jump JG to the end while. JG means jump if greater.
But if - is not larger than -, then this will not jump.
It will go straight and increment -, and then it will jump to while again.
Then it will repeat itself again.
Eventually, there will come a situation where - is larger than -, so if that happens, then the jump
greater will happen.
It will jump out and go to the end while, which is the address down here.
So as usual, we need to find the addresses that we are going to use for counter and for variable B,
and also the address in the text segment for the while and the end while addresses.
So let's get started.
First, we look for the addresses that we want to use in the BSS segment.
Click on the dump and then go to the memory map.
Right-click on the BSS segment.
Follow in dump.
Scroll down.
Somewhere down here, look for blank addresses so we can use this one.
Right-click on this.
Copy the address and paste it here.
This will be our counter address, and we are going to assign one to it.
So right-click here and then binary edit and key 01.
Next, we look at the address for variable B.
We can use this one here. Right-click, copy the address, and paste it in here.
And we are going to assign three to variable B.
So right-click,
binary edit, key 03, hit okay.
So the next thing to do is to code.
We still don't know where the while address will be, because the while address will be at the location
where the compare instruction happens, but we don't know where the compare instruction will be at.
So let us code these two lines first.
Then we'll come to this.
Then we know where the while address will be.
So the first instruction is MOV.
And this one will be the address for the counter.
So we come to the counter.
Copy it.
And we paste it in here.
Click okay.
The second one will be MOV -.
Then we come here and copy the address for -.
And we paste it in here.
Click
okay.
Next time we'll come to the address for compare.
So now we know where is the while address.
So we can go here and copy this address.
And paste it in here.
And at this address we will do this instruction, CMP.
Here.
Okay.
Next, we will do the jump greater.
So for the end while, we can safely jump right to the bottom and choose the address way down here.
Right-click this, copy address.
The reason why we jump all the way to the bottom is because after JG, there will be two more instructions.
So this also will eat up some memory.
That's why we jump all the way to the bottom.
We then paste the address here.
This will be your end while, end while address.
Now we can do the jump greater.
So let's go back up here.
JG, and paste the end while address.
Here, okay.
And then we do the increment INC -, followed by JMP while, JMP, and then the address for the while, which
is this address.
So let's copy this.
And paste it in here.
All right, so now we are done.
Let's double check.
MOV -, this one, -, this one will be the counter address, which is 0x407080. MOV -,
this one will be the variable B, 0x4070A0, variable B.
And then compare - and -, and then jump greater
to 0x4015BA.
So jump greater to end while.
So end while is 0x4015
BA. Then increment -, and then jump while.
So while will be the address 0x401572, 0x401572.
So we are right.
So make sure you have a breakpoint here.
And then run to it and then step over.
Now it's going to move the variable counter value into -.
So our counter has got this address, 0x407080.
So this is your counter address.
It's going to move one to -.
Step over and check. - is one.
Next, it's going to move the variable B from this address. This is variable B.
So it's going to move three to -.
So let's step over and check -. - is three.
Then it's going to compare - and -.
So let's step over.
And then you notice that it doesn't jump because the gray arrow is gray.
It's not red.
And here is also stated jump is not taken.
And the reason is because - is not greater than -.
- is one, - is three.
So it doesn't jump because - is less than -, so it will go straight and increase - by one.
So let's check - now. - now becomes two.
Then it will jump.
As you can see, the red arrow is lighted, so it will jump back up to the while loop.
So this is the while address.
So step over, and it jumps back up.
And now it goes down to compare again, - with -.
And again, it won't jump because - is two, which is still not greater than -, which is three.
So it goes straight and increases - by one.
- now becomes three, then it jumps back up.
Now it compares again.
Again, it won't jump because - is not greater than -, so it goes straight, increases -. - becomes four,
then it jumps back up again.
Compare - and -.
This time it will jump because - now is greater than -, so it will jump.
As you can see, the red arrow is lighted.
Jump is taken, so it will jump all the way down here.
So let's step over, and it jumps here.
So this is how you can implement a while loop in assembly language inside x64dbg.
That's all for this video.
Thank you for watching.