Hello and welcome back.
In this video, we are going to do another if-else statement.
Earlier on, we did if statement.
So this time we are going to do if-else.
So if-else statement,
it looks like this.
Assuming we have two variables A and B, and we will perform an if statement to compare the values in
A and B.
So if A is larger than B, then we assign three to B, else we assign three to A, then we end if.
So to implement this in assembly, we will do something like this.
So there is an address here.
The label is if. Move QWORD PTR, the value in A to -, and then compare with the B, and if it's greater,
then we jump greater.
So we jump to the else label, which is also another address.
However, if - is not larger than B, then we will not jump.
Instead, we will continue to step through and perform this assignment.
We will move three to A, and then when we finish doing that, we will jump to another address.
And if we don't jump here, it will step through and perform the else part.
That's why it is important to jump here.
So all these are actually just addresses.
So we have to find the addresses for A, B, else, and endif.
Right.
So the first thing we do is open your template2.exe.
And then we will look for addresses for all of these.
So A, B will be addresses in your BSS segment because they are variables. Else
and endif will be the addresses in your instruction section,
here, your text section, which is your instruction section.
So for A and B, let's select dump one first and then go to memory map and look at BSS.
Right-click on it and follow in dump.
And here we will select two addresses, one for A and one for B.
So let's scroll down somewhere at the bottom here and let us select this for A.
So right-click this address, copy address, and then we paste it in here.
So this is your first address.
And then now we go to another location.
Select this one for B.
We copy the address and we paste it in here.
So that is done.
Else
and endif, we will look somewhere down here at the bottom. We scroll down somewhere to the bottom.
And maybe here we would do else over here, or maybe high up here, else.
So we just right-click this address, copy this address, and we put it here.
And endif will be the end of the if statement.
So we can safely put it somewhere down here.
So right-click this, copy the address, and put it down here.
So we have four addresses.
So let's go to the top here now and code all this.
So the first one is to move this - QWORD PTR A.
So we copy this address A first.
Copy this, and then we go to this location and assemble our move.
So that is done.
We paste our address that we copied.
Next one would be compare this with B.
So let's copy B's address.
So we come back here, we compare.
Paste the address for B, hit okay, then we will jump to else if greater.
So over here we copy the else address.
Come back here and we paste our address there.
Be sure to put the 0x in front of all addresses, the prefix, and then
okay.
And then we do this, move this to address A.
So you copy address A again.
And then we jump to endif, endif address over here.
Let's copy the address.
And then finally the else part.
So we go to the else part over here and do this instruction.
So we go to the else part.
So the else part is somewhere down here,
0x401586.
So we go down to 0x401586 over here.
586.
So over here we will do this instruction.
We will copy this, three to B.
So address B is here.
So we copy the address first.
Come back to this one and then we move.
Move three there.
Here.
Okay.
All right, so it's done.
So now just do a check to see we got it correct.
So the first one is move
-, 0x407100.
Move -, 0x407100.
Next one is compare with
0x407120, 0x407120.
Next one is jump 0x401586.
Jump to else, which is 0x401586.
Correct.
Next is move 0x407103.
Move 0x407103, and last.
Next one is jump to 0x4015B9.
Jump to 0x4015B9.
And then the last one will be this statement at the else address.
So else address is over here,
0x401586, where we move B to three.
So B is 0x407120, 0x407120, 3.
Correct.
So we are ready to execute this now.
So we run. Before that, we have to put some values in the variables.
So for A, we are going to put one. For B, we are going to put two.
So we go to variable A, 0x407100, 0x407100.
Right-click here and binary edit.
And we put one over there.
Be sure to put 01 and not 10, okay?
Because it's little-endian convention.
So when you read this back, it reads in reverse order.
It will be 00, 00, 00, 00, 00, all the way to the end, 01.
So this means one, not ten.
Yeah.
You don't put 10 here, 01.
So this is variable A. Variable A is now having the value one.
Next, we are going to initialize variable B with two.
So variable B is this address, 0x407120, over here.
So we are going to assign two there.
So right-click here, binary edit.
And here we put 02, okay.
So now we got one in A and two in B.
So we are ready to run.
So we run to our breakpoint.
Step over. It's going to move variable A to -.
Step over. -
now is one because it just moved one from variable A to -.
Next, it's going to compare - with variable two.
Variable B, sorry. Variable B is this one with two.
So it's going to compare that.
So after compare, it's going to test whether variable A is bigger.
If variable A is bigger, then it will jump.
But in this case, it is not.
So it doesn't jump.
That's why the grayed-out arrow is shown instead of red.
So now it's not going to jump.
It's going to move forward straight away.
It's going to move forward.
And it's going to move three to variable A.
So variable A is this.
So it's going to move three.
Then step over, and you find three is being moved there.
Then finally, it's going to jump all the way out into the endif part.
So step over. You see that the red arrow here, red arrow is highlighted, meaning it's going to jump
here.
So step over.
And true enough, it jumps over here.
So this is how we can implement if-else statements inside your assembly.
You need to do some planning.
You have to set up all the addresses for the variables and the instructions before you can implement
it.
So that's all for this video.
Thank you for watching.