Again, it depends on the size of integer.
I compiled this using gcc-mingw:
Code:
#include<stdio.h>
int add(int, int);
int main()
{
int m=10,n=20; /* it will take 2 *4 =8 bytes */
printf("%d", add(m,n));
/* printf will take 4 bytes+ function call will take 2 bytes to store add */
return 0;
}
int add(int x, int y)
{
return (x+y);
}
The essense of the call inside main is (T= means total usage):
Code:
pushl %ebp // 4 bytes. T=4
movl %esp, %ebp
subl $24, %esp // This uses 24 bytes. T=28
...
movl $10, -4(%ebp)
movl $20, -8(%ebp)
movl -8(%ebp), %eax
movl %eax, 4(%esp)
movl -4(%ebp), %eax
movl %eax, (%esp)
call _add // 4 bytes. T=32
movl %eax, 4(%esp)
movl $LC0, (%esp) // 4 Bytes. T=32
call _printf // 4 btyes. T=36
movl $0, %eax
...
_add:
pushl %ebp // 4 bytes, T = 36
movl %esp, %ebp
movl 12(%ebp), %eax
addl 8(%ebp), %eax
popl %ebp // -4 bytes. T = 32
ret // -4 bytes. T = 28
...
If you want to know exactly, you must look at (and understand) the assembler code generated by the compiler. Note that the compiler may not restore the stack-pointer immediately after a call - as seen above.
There is another way: Fill the stack with a certeain pattern. We can do that in main, for example (but we do need to know how large the stack is or things can go REALLY badly quickly)
I will post some code later on to show that.
--
Mats