Unit2 Procedures
Unit2 Procedures
Example
Prologue
The first two instructions at the top of the function code save the original value of EBP to the top of the stack, and then copy the current ESP stack pointer (now pointing to the original value of EBP in the stack) to the EBP register.
Epilogue
After the function processing completes, the last two instructions in the function retrieve the original value in the ESP register that was stored in the EBP register, and restore the original EBP register value. Resetting the ESP register value ensures that any data placed on the stack within the function but not cleaned off will be discarded when execution returns to the main program (otherwise, the RET instruction could return to the wrong memory location).
Calling Conventions
A scheme for how subroutines receive parameters from their caller and how they return a result. Means :
where parameters and return values are placed (in registers; on the call stack; a mix of both) the order in which parameters are passed (or parts of a single parameter) how the task of setting up for and cleaning up after a function call is divided between the caller and the callee which registers that may be directly used by the
Calling Conventions
Differ
Types
Cdecl - In cdecl, function parameters are pushed on the stack in a right-to-left order. Function return values are returned in the EAX register (gcc) Syscall Optlink
Types
Callee clean-up - callee cleans the arguments from the stack it needs to be known at compile time how many bytes the stack needs to be adjusted
Pascal - the parameters are pushed on the stack in left-to-right order (opposite of cdecl), and the callee is responsible for balancing the stack before return. (MS...) Stdcall the parameters are pushed onto the stack in right-to-left order, as in the _cdecl calling convention. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register. Win32 API
Interesting point !
Can we have a function with variable number of arguments ? Which calling convention to use ?