Call function in C to Assembly

131 views Asked by At

If we have the following function:

int func(int n, float f, char* s, double* d);

The call to the function in Assembly will be like that:

movl <n>, %ecx
movl <s>, %edx
pushl <d>
pushl <f>
call func

Is this correct? Why it's in this order? It's because of the size of each type?

1

There are 1 answers

0
Devolus On

For known arguments, there is not a strong compelling reason to push right-to-left or the other way around, so it's just a convention to follow.

For variable arguments it's a benefit to push them in the reversed order because the callee doesn't know how many arguments were passed. but the first argument is in a known position, i.e. right after the return address.

So if you consider the example of printf:

 printf(FormatString, a1, a2, a3, a4);

So FormatString is now in a known position and can easily find the other parameters going through the stack.

If it were the other way around, then the callee would need to get some additional information in order to find the first argument, Either a pointer, or a counter or something, that tells it, where the formatstring is found.