Hello and welcome back.
In this lesson, I'm going to show you how to create functions.
And creating functions is also the same as creating what we call stack frames.
So what are stack frames?
Let me explain.
For example, you see here push -.
Move - - and then at the end here you will have move - - and pop -.
So a stack frame is starting from these two statements and then ending with two statements here.
So that's a stack frame.
There is one more line here which we have removed.
It is supposed to read move - - over here is move - -.
So over here there should be one more move - - the reverse of this, okay.
So that is a stack frame.
So this itself is a function. A function starts from a stack frame, starts from this opening statements,
and ends here.
And then there is a return.
Now we are going to create a function within this big function.
So how do we create a function?
So to create a function, you must first find out where you want to put your function.
Let's say we want to put our function over here, over here.
So we need to copy this address.
Copy this address.
And then over here we make a call to the function.
Then paste the address that we have copied.
So this here is a call to the function.
So when the execution comes to this line, it will call this function at this address 401569, which is
401569.
Over here it is supposed to jump over there.
Right.
And then, uh, maybe it's not so obvious.
Let's choose an address.
Scroll down here.
Copy this address.
All right.
Let's change it so that it's more obvious.
Okay.
So now it will call 40156F, which is over here.
40156F.
So at this address here we need to create a function.
So to create a function we must create a stack frame.
So the first statement in the stack frame is always push -.
Then it will be followed by move - -.
So this is the start of your function.
Then the function always ends with the opposite of this.
That means somewhere down here, after you have done whatever you want to do within the function, you
should reverse these two, this and this.
So down here let's say we want to reverse that.
So we come back here. We will do the opposite: move - - and then followed by pop -.
And finally a return, ret.
So the ret keyword is the opposite of a call.
So when you run it will call this function.
That means it will jump to
here, perform these two opening statements of the stack frame.
Then perform whatever you want to do inside here, and then do the reverse to, uh, unwind the stack
frame, and then finally return back to the line after the call.
So let's try this now and see how it works.
So I put a breakpoint here.
So we run to the breakpoint.
Then we step over until we come to our call.
So now in our call, if we were to continue to step over, what will happen is it will execute this line
and then immediately come here. You won't see that the call going into the function.
If you want to see the call going into the function, you need to use step into over here, step into.
So when you click step into, you can see that it steps into the function.
Now you're in the first line of the stack frame.
So this is the start of the function.
And then you can continue to step over over here, step over, step over, and perform whatever instructions,
if any, until you come to the end of the stack frame where it will then unwind the stack frame to
revert back to the original.
So let's revert everything back to original.
And then finally, when it finishes with the function, it will return to the line after this call.
So from here it's going to jump back up and land over here.
Let's take a look.
Step over and you see it goes back here.
So this is how you can create, uh, functions using stack frames.
So every function usually has these opening statements to create the stack frame and the closing statements
to destroy or unwind the stack frame.
So this opening two lines are also known as function prologue.
Function prologue meaning the start of the function.
And these two lines are function epilogue, also known as the end of the function.
So that's all for this lesson.
Thank you for watching.