Okay.
Welcome back.
As a challenge to you, I'd like you to try to implement six arguments function calls with local variables
as well.
So go ahead and try it on your own before you see the solution.
So how I would do it is first I will move, uh, - one.
And then move - to, and then move - three.
Move - four.
Now I need to create a space for six arguments.
I've already got four arguments here, but I need to push two to the stack.
Means on the stack I need to have two variables, so I need two times 8, 16 bytes extra.
So 16.
Now we need to add 16 to your shadow space 32.
So 16 plus 32 will give you 48 bytes.
So I need to sub - 48.
I hope you got that and you understood that.
You need to have,
let me explain that again,
how to calculate this one.
Your shadow space,
you need 32 bytes.
Yes.
And then you need to have two arguments on the stack.
Each argument is eight bytes.
So two times eight.
So for your fifth and sixth argument,
fifth and sixth, you need to put it on the stack.
So that will eat up two times 8, 16 bytes.
So when you take 32, add 16, you get 48.
That's why we sub 48 here.
Okay.
Uh, then next thing you need to do is you need to assign your fifth and sixth arguments to those
on the stack, which is here and here.
So to access that, you need to offset - plus 32 and - plus 40.
So to do that, we do that.
Now move, uh, keyword - plus, and then over here 32.
So we move our fifth argument to the
fifth argument on the stack.
Then we do that for the sixth one as well, which is plus 40.
So you repeat the same keyword, - plus 40.
And this is the sixth one.
So at this point in time, we have all our six arguments ready for the function call.
So we can now call our function, assuming our function is way down here.
This is a bit too near.
So let's move it a bit down here.
Uh, we copy this address.
And then we make a call to that function.
And then
next, we need to clean the stack.
After the call finishes, we need to clean the stack.
So we add, uh, earlier on we sub 48.
So now we have to add 48.
So when you make a function call, it will jump to this address.
40156596.
4015960 is too near.
Sorry.
Let's change to a different address for a function.
Let's use this instead.
Copy address.
Let's change the address of our function to something further down.
Paste this new address over here.
So now it's going to call the function at this address 40159.
Over here.
And over here we will push, push our -, and then we will move - -.
Okay.
And then now we are going to, uh, create four local variables.
So we need to sub - with 32 for four local variables.
And now we're going to XOR our, we are going to XOR our -.
And then now we are going to add.
So add the first one.
And the second one.
And the third one.
-, add the fourth one.
-.
Like that.
And now we are going to add the fifth one.
So we need to dereference using this notation - plus 48.
So we need to - -.
Keyword.
- plus 48.
And then we add the sixth argument.
Keyword - plus 56.
56.
Okay, I see. Then we need,
so notice that we didn't use any of our local variables.
Right.
But if you wanted to, I already showed you in the earlier lesson how to make use of those local variables.
In this case, we don't use them.
But assuming you want to use the local variable to store the result, this is how we can do it.
Let's say we want to store it in the first local variable,
the result.
So we can do this, move.
Uh, keyword.
And then here - minus 18.
Sorry, minus eight here, - minus eight for the first local variable.
And let's say you want to store the result there.
So we will do something like this here.
And finally, you want to clean the stack.
So you will, uh, you will, uh, add, uh, - 32 like this, because here you created four local variables.
So now to clean the stack.
And then you will move
- -.
-
And then you will pop -.
And then you will return like that.
Okay, so now we are ready to call.
Put a breakpoint, run to the breakpoint, step over.
Move all your four parameters to the registers, 1, 2, 3, 4.
Then create space for the shadow plus, uh, fifth and sixth arguments on the stack.
Move the fifth argument to the stack, - plus 20. - plus 20 is here.
- is EF 65 DF0.
See, when I click on this, I can see here - plus 20 is 06510.
Fifth argument is there.
Step over now.
It's here.
Sixth argument.
Step over.
This argument is going to move the.
So now click on this.
You see the sixth argument, six is there. - plus 28 is here, 65518 here.
Sixth argument.
So now you're ready to call.
So you can,
you are here now.
Step into the call and you jump to the call, to the function.
And then you move.
Right.
Then you create your local variables, 32 bytes, four local variables, you move, then you now you zero
-, and then you add first number to -.
You get one, add a second number, you get three, add a third number you get six.
Add the fourth number, you get ten.
Add the fifth number, you should get 15.
So - plus 30, you can see from here is five.
So - plus is over here. - plus 48, plus 48 is here, five.
So you can take this five and add to six.
So five plus ten will be 15.
There you go.
Now you can take - plus 38, which is six over here.
Add to the 15.
You should get 21, and you get 21, 16 plus five is 21.
So this is hex, but you convert to decimal is 21.
Next we're going to move this, the result of the addition to the local variable - minus 18.
So - minus 18 is this location.
So step over, and now click back on the above one. - minus 18 now has got the result of the addition, which
is 21, 16 plus five.
And it's at this address 658.
When you click on this, you will see here as well.
Or you click on this, you will see here as well.
Right?
When you want to revisit the previous location of the stack, you just click on it.
You can see here and here, that's the result.
All right.
Now we clean the stack, clean the stack, and then clean this as well.
Pop, return, and that's it.
And now we clean the stack of the function caller.
That's it.
So this is how you can implement a function call with six parameters and also local variables.
That's all for this video.
Thank you for watching.