as there are no return statements, in the solution you gave, so does it means that stack unwinding is independent of the return statement.
the functions returns back due to stack unwinding.
one more thing, what all things are stored in the stack while pushing in the stack (function call)
Hi mithunp
To clarify what happen if a C function is called, let's analyse what will happen if this functionint fun (int a, double b) is to execute.
1. Just before the call
First, the calling program saves the values of EAX, ECX and EDX on the stack
Then the value of last actual parameter b is pushed on the stack. Next follows parameter a.
Finally contains of EBP register is pushed which is now on top of stack.
2. Now we enter function fun().
All local variables locally defined in fun will also be created on the stack. Then some calculation is done, especially the return value is calculated.
3. Just before leaving function fun
All locally defined variables are poped from stack (partly unwinding)
Important: the return value is passed back in EAX register and not on the stack. This is true as long as the length of the return value is less or equal 4 bytes (if greater, capacity of EAX would be exceeded).
Longer return values will be passed back by an extra actual parameter which is automatically generated if its length exceeds 4 bytes and which is an address. In such case the function fun would look like: int fun (*extra_parameter, int a, double b).
Therefore, return values only affect the contains of the stack by an additional address if return values are longer than 4 bytes.
Function fun() also restores the registers EBX, ESI and EDI before leaving.
4. Now again back in calling program
First, all actual parameters, in our example a and b, are poped from stack (final unwinding). Then, usually the return value transferred via EAX register will be saved.
There is something more detail work to be done inside the function fun to set up a complete stack frame.
I hope I have explained this somewhat understandably. (As for an assembly programmer
dealing with such calling mechanisms is daily job)
-- tesu