In the CDECL call convention, can I reuse the arguments that I inserted on the stack?

In the GCC calling convention cdecl, can I rely on the arguments I popped onto the stack to be the same after the call comes back? Even when mixing ASM and C and optimizing ( -O2)?

+4
source share
1 answer

In a word: None.

Consider this code:

__cdecl int foo(int a, int b)
{
   a = 5;
   b = 6;
   return a + b;
}

int main()
{
   return foo(1, 2);
}

This created this asm output (compiled with -O0):

movl    $5, 8(%ebp)
movl    $6, 12(%ebp)
movl    8(%ebp), %edx
movl    12(%ebp), %eax
addl    %edx, %eax
popl    %ebp
ret

Thus, it is quite possible that the __cdecl function stomps on the stack values.

This is not even considered the possibility of embedding or other optimization magic, where, in the first place, not everything can be on the stack.

+4
source

Source: https://habr.com/ru/post/1650633/


All Articles