jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
I have a C/C++ code like below
When calling function is executed first the value 75 is pushed to the stack and then value 25 is pushed to the stack. Then return address is pushed on to the stack. Return address will be the address of the next instruction after the calling function. Right? How does the return address calculated?
Then ebp, esi, edi are pushed on the stack and ebp is set to esp. So, ebp and esp will be pointing to the top of the stack which contains edi.
Then when the called function is executed, a local variable result is created on the stack and stack will be pointing to result variable.
then values of _op1 and _op2 on the stack is referenced and value for result is computed and stored in result variable on the stack.
How is the result returned to the calling function?
Is _sum(op1, op2) the calling function or is it main() the calling function?
Is the function definition of _sum() the called function or is it _sum() in the main() the called function?
See the asm code below and complete the process after executing the _sum() function
Where actually the stack frame gets created? Is it when mov ebp, esp is executed?
old value of esp is stored in ebp and then ebp is used to reference the variables on the stack but ebp never changes but esp changes during stack operation. Finally when returning from the function esp is assigned its old value which is in ebp. Right?
In the asm code show how value of result is returned to main function?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 //Function prototype int _sum(int _op1, int _op2); //Main Function int main() { int op1, op2, sum; op1 = 25; op2 = 75; //Calling function sum = _sum(op1, op2); return (0); } //Function Definition //Called function int _sum(int _op1, int _op2) { int result; result = _op1 + _op2; return result; }
When calling function is executed first the value 75 is pushed to the stack and then value 25 is pushed to the stack. Then return address is pushed on to the stack. Return address will be the address of the next instruction after the calling function. Right? How does the return address calculated?
Then ebp, esi, edi are pushed on the stack and ebp is set to esp. So, ebp and esp will be pointing to the top of the stack which contains edi.
Then when the called function is executed, a local variable result is created on the stack and stack will be pointing to result variable.
then values of _op1 and _op2 on the stack is referenced and value for result is computed and stored in result variable on the stack.
How is the result returned to the calling function?
Is _sum(op1, op2) the calling function or is it main() the calling function?
Is the function definition of _sum() the called function or is it _sum() in the main() the called function?
See the asm code below and complete the process after executing the _sum() function
Code ASM - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 push 75 push 25 push return address push ebp mov ebp, esp push esi push edi push result mov ax, 25 add ax, 75 mov [result], ax . . . pop edi pop esi mov esp, ebp pop edi pop esi ret
Where actually the stack frame gets created? Is it when mov ebp, esp is executed?
old value of esp is stored in ebp and then ebp is used to reference the variables on the stack but ebp never changes but esp changes during stack operation. Finally when returning from the function esp is assigned its old value which is in ebp. Right?
In the asm code show how value of result is returned to main function?
Last edited: