Hello and welcome back.
In this lesson, we are going to study
how we're going to implement the if structure in assembly. In C programming, we have a structure called
if structure.
And this is what it looks like.
So you will have an if statement here.
If length equals to zero, then assign one to length and then end if.
Now if this if is going to be implemented in assembly, length would need to be created as a variable
in the memory.
As we have stated before, to create variables in assembly, we assign values to memory.
So in this example here, we are going to do just that.
Later I will show you.
So to implement this in assembly, we will have an address here.
And then at this address here.
This, by the way, is just a label.
If you are implementing this, you need to use addresses instead of labels.
So at this address here, you will move the value stored at this address into -.
Then you will test - with - whether it's zero.
And then if it is zero, then this will be true, jump not equal.
And then it will jump to this address.
And endif is just another address here.
If you were going to implement this using compare, then it will look like this.
We have an address here, if address.
And over here we move the value in this address into - register.
Then we do a comparison.
We will compare whether - is zero, and jump not equal will be true if - is not equal to zero.
So it will jump over here.
So these are the two ways in which you could implement this.
So let's try this in x64dbg.
I have opened template2.exe, put a breakpoint here.
And now we have to look for memory to store this variable length.
So we select dump one and then go to memory map and then look for the BSS segment.
BSS is for the variables.
Right-click on BSS, follow in dump.
So it will be in dump one.
So in dump one, you find that there are many empty locations we can use.
So let us choose 0x407040.
So this location here is already zero.
So we don't have to do anything.
So at this location, we assume this is an address that holds variable length, and it is already zero.
So now we're going to move this value into -.
So we need to copy this address here.
And then copy address and then come up here.
We will type MOV into -, QWORD PTR, and then the address.
Next, we are going to test - with -.
And then we are going to do a jump not equal to an address.
So what address are we going to jump to?
So for this endif here, let's choose an address somewhere down here.
Maybe we choose this one.
So we copy this address.
We come here, we perform a jump not equal.
And then we paste the address that we want to jump to.
Yes.
So you see, we already implemented this if statement using assembly.
So let us run to our code here now.
So we run, it hits our breakpoint.
We step over.
So now it's going to move the value at this address into -.
If you come down here, you will see that this address, the value is zero.
Now if it is not zero, let's say for some reason, during the execution of this pushad
or whatever, it has written some value there.
So to solve the problem, you just right-click and then here you just binary edit and just clean all to
zeros.
That's all.
So now it shows zero.
So now we are going to move this zero into -.
So let's step over, and you find that now - is zero.
Then we are going to test the value in - whether it is zero or not.
So we step over.
And now you notice that it is tested.
And then when it comes to this next line, jump not equal,
you notice that the jump arrow is grayed out.
This indicates that it is not going to jump.
And the reason for that is because it will only jump if not equal to zero.
But in this case here, it is zero.
Therefore, it won't jump.
So this is how it works.
So if you continue running, stepping over, it will just continue on like this.
So this is how you implement this inside your assembly using TEST.
Now let's repeat this with compare.
So for compare, we do the same thing.
We are going to use this same code here.
So we just copy this code and paste it here.
And then we are going to compare - with zero.
And then we are going to do a jump not equal.
So we just copy this code here.
What I did was I pressed spacebar on this line here, and then I just select all this and Ctrl+C to
copy, or right-click on the mouse to copy.
Then I come back down here.
Press spacebar again and Ctrl+V to paste.
Or right-click the mouse and select paste.
Click okay.
So now we are here, and let's step over.
Again, it's going to move the value in here to -.
So assuming - is not zero, let's say we modify this to two.
So now you step over this and see what happens, and it becomes two.
Next, we will compare with zero.
Step over that.
And it has compared.
And when it comes to the jump not equal, the arrow is grayed out.
So this indicates again that the jump is not going to happen because - is zero.
So that's why it doesn't jump.
So this is how we can implement the if statements inside assembly.
That's all for this video.
Thank you for watching.