Welcome back.
Now we are going to do the same thing, but this time we are going to create four local variables.
So same way we will move our
first parameter.
Move the second parameter to -, which is two.
Move the third parameter to -, three, and then move the fourth parameter to -.
Which is four.
All right.
Okay, so next thing we need to do is to create the shadow space.
So we sub -
32.
Then we make a call.
So the call here is same.
We want to use the same address for the function.
So we copy this address and we make a call to that address.
So we will call here.
And here we do the same thing.
We push -.
Push -.
Move - -, and now we are going to create four local variables.
So we need to do sub - 32, because each variable takes up eight bytes or quadword.
Since we have four, four times eight is 32.
How do we make use of those local variables?
For example, if we want to store, um, your first parameter to the first address
of the local variable.
What do you do?
Take a look at this.
So if you want access to those local variables, you need to use offsets.
Take a look at this.
- minus eight is local variable number one.
Minus 16 is the second one, and third one, fourth one, and so on.
So we're going to use this as our guide to access those local variables.
Okay, so the first one.
- minus eight.
So let's say we want to save the first argument to that local variable.
- minus eight.
This is how we do it.
We move
-, you have to dereference it, - minus eight.
This is the first local variable.
-.
All right.
Next one will be
second local variable will be - minus 16.
And then this one,
- will go there.
Do the same thing for the third local variable.
Minus 24, and - will go there.
And the last local variable.
- minus 32, and - will go to
there.
All right.
So.
So this is just to demonstrate, uh, how to create local variables and use them.
Right.
It is not meant to be optimized code.
Right.
So just to demonstrate.
So now you can do your addition and do whatever you want using, uh, this address of
all these four variables, okay.
Let's say now you want to do an addition.
Let's say, let's demonstrate.
So you XOR - with itself.
And then you're going to add - with the first local variable, which is keyword - minus eight.
Repeat the second one for the second one.
And I need to remind you, this is not optimized code.
This is just to demonstrate how to access local variables.
Okay.
Next one should be 24, which is 24.
The next one will be
32.
So, uh, that's it.
So at this point, your - will have the result of the additions.
So at this point we need to move - -, and then we do a pop -, and then a return.
Like this.
Next,
we are going to come back here after this return and we clean the stack.
So we clean the stack the same way we use add - 32, because we allocate 32 bytes for shadow space
over here.
So we need to clean it.
Note that if you over here, if you want to access the result of - and do something with it, you can
do it this way.
For example, if you want to move the result of the addition to register -, you could do something
like this.
Because when the function returns, it will have the result of the addition in -, and you can move
it, save it to - or whatever you want to do with it.
So we put a breakpoint here and run to the breakpoint and step over it.
Move all the four parameters to the registers.
So we have 1, 2, 3, 4 here.
Then you create a shadow space.
Then you step into the function call.
So now you're inside.
So now you created your new base pointer by doing these two lines.
So your base pointer is now 654.
So at this point in time, you are already done this.
You created the base pointer.
So now you're going to create these four local variables by doing this, by - minus 32.
So - was previously here.
So minus 1, 2, 3, 4, 4 times 8, 32.
So - minus 32, you create four local variables.
That's why we're doing this - minus, sub - 32.
This is hex 20, which is 32 in decimal.
So next, once you created the four local variables, then we are going to assign the arguments to it.
So the first argument is - minus eight.
Here, - minus eight.
So we're going to assign - to that.
So let's do that now.
So let's step over.
-.
Now look, - minus eight.
- is 65 4065 FD F0, which is here.
Minus eight is here.
So the first variable is assigned there.
So FD F0 is referring to this location -, and then - minus eight above it is referring to this.
And then we have just stored the argument - into it, which is one.
So now we're going to do the next one.
We're going to save - into second variable.
Step over.
And now you see second variable is stored in - minus 16.
Now we step over.
Next one, - is going to store - minus 24.
So you see - minus 24 is here. - is here, minus 8, minus 16, minus 24.
So this is - minus 24.
This is where you stored -.
Now we're going to store - into the fourth local variable. Step over.
And you see the fourth local variable is stored with - minus 32, which is here.
So this is how you can create local variables and access them and use them.
Next we are going to do the addition.
So we XOR -, - becomes zero.
Then we start adding them.
First we take - minus eight, which is here, add to -.
So - becomes one. Next is the - minus 16, which is here.
You can also see from here, - minus 16 is two.
Add two to -, so - becomes three.
One plus two is three.
Next is - minus 18.
- minus 18 is.
Remember, this is hex here. Minus 18 is three.
So this is three.
Over here, you see the address FD8, same as this FD8 here.
Let's add it.
So we're gonna take that three there, add to whatever is already in there.
So three.
So three plus three should give you a six there.
And now you got six.
Next, we're going to access - minus 32.
Minus 32 is four at this address 6540 here at E0.
So - is here. - minus 32 is here.
So you take this four, add to whatever is in -.
So - becomes ten. Then
now we're going to clean the stack.
So we move - into -.
Pop -, and we return to the line after this.
Now we clean the stack in the main function.
Then we now take the result in - and do whatever you want.
In this case, we just move it to -.
And now we see - has got the result ten.
So this is how we can create functions with four arguments and four local variables.
That's all for this video.
Thank you for watching.