Hello.
Welcome back.
In this lesson we are going to do some practicals on what we have studied.
In the last lesson, we are going to create a simple function to add numbers.
So let's say you pass eight arguments to the function, and the function will add all those arguments and
save it to the register -.
So - is traditionally used to return the result of a function call.
So let's get started.
So the first thing we need to do is, uh, we have to move those arguments to a register.
So here we move the arguments.
So the first example, we are going to have only four numbers to add.
So the first number, we'll move one to -.
Second number, we move to -.
Third number, we move to -.
This is we are following the Microsoft convention.
And then we move to - fourth number.
So since we don't use the stack, we don't have to allocate the arguments for the stack, but we still
have to create the shadow space.
So to create shadow space, we need to sub - 32.
Because Microsoft convention requires us to create 32 bytes shadow space.
Next, we are going to make a call to our function, assuming our function is over here.
So we copy the address first, and then we put it here.
We do a call, paste the address I will copy.
So this is our function.
All right.
So when a call returns, it's going to save. You can see the result in the - register.
All right.
So you can do whatever you want with it.
But when the call returns, you need to clean the stack.
So since you sub - 20, you need to add
- 20.
So 20 here is actually hex here.
So in the decimal will be 32.
So now we can implement our function call, which is here.
So in the function call, the first thing you do is to, uh, push -.
Note that I'm just referring to the notes here.
The notes here.
Like this.
In the function call, you need to push -.
Remember we studied before in the earlier lessons.
The first thing you do in the function call is push - and move - -.
So that's what we're doing here.
Next is to move - -.
Thereafter, we can access those numbers.
But first we need to zero out - so that - maybe - with itself.
We are zeroing in.
Zeroing it out.
Why?
Because we are going to use - to be the accumulator
to sum up all the numbers.
So the first number, we are going to do is to add
- -.
-
That is the first argument.
Then we add - -.
-
Then we add
- -.
-
Then we add - -.
Then we have to return now.
So we're not creating any local variables, so we can just return.
So before we return, we need to move, need to clean the stack, although we don't use it, but we
always do this - -.
Maybe pop -.
Then we return.
Let's see.
Okay.
So we put a breakpoint here.
We run to our breakpoint.
We step over.
So we see we move all this into the registers -, -, -, -.
Next, we are going to create shadow space.
Then we will call.
So we want to follow and trace the code.
We need to step into it.
So step into the call.
We jump down here, then we push -.
So - is now on the stack.
This is the old -.
- 65420, which is here, 65420, which is here.
Is -.
Then we move - to -.
So now - goes to
656540, 6550,
and the old - is pushed to the stack.
Here, you can see here.
So when the call finishes, it will unwind the stack and go back to this old -.
But now that you have moved - -, - is pointing to the same location as the -.
So this now, from here onwards, will be used as a base pointer to reference the arguments if necessary.
So now we are going to add all these registers.
First, - becomes zero.
Then you add one to -, - to -, and - to -, and - to -, -.
So by now - will have the result of the addition one plus two plus three plus four.
One plus two is three.
Three plus three is six, six plus four is ten.
So result should be ten.
So we got ten.
So it's correct.
Then we now clean the stack.
And then pop.
The old - value back to it.
So the old - at the top of the stack will be the old -.
So now we pop it back.
So now
- points to the old stack, and then now we return.
So when we return, it will go back to the line after the call.
Then from here you can clean the stack.
So here you need to clean the stack.
Here you sub, uh, - 32.
So here you need to clean the stack.
Sorry, that's a mistake.
There it should be add.
- 32.
Yes.
Like that.
All right.
So now you clean the stack.
And now the stack is back to the original state, which it was in before the call.
So this is how you can implement a function call with four parameters and no local variables.
That's all for this video.
Thank you for watching.