0

Everywhere I looked ("Programming from the ground up", Wikipedia x86 Calling Convention, ...), it was said that C passes arguments through the stack. Yet, in the following C code

int func(int);

int main() {
    func(5);
    return 0;
}

int func(int a) {
    return a;
}

Argument a is passed through register %edi (used gcc -S ex.c to compile on Ubuntu 64 bit machine). I passed more arguments, the caller just uses more registers to pass the values.

Is that normal? Are the online resources I visited somehow outdated?

Thanks in advance.

RayaneCTX
  • 573
  • 4
  • 13
  • 4
    "C passes arguments through the stack": wrong: it's implementation defined and depends on the compiler. If there are enough registers, the compiler may choose to pass by register to be more efficient – Jean-François Fabre Aug 30 '18 at 03:33
  • To go further, can we control the compiler's behavior in that regard? I want to call my own assembly routines with a C wrapper. Should I re-post? – RayaneCTX Aug 30 '18 at 03:35
  • You're compiling for the x86-64 System V calling convention. [What are the calling conventions for UNIX & Linux system calls on i386 and x86-64](https://stackoverflow.com/q/2535989)/ (answer also includes the function-calling convention). [Where is the x86-64 System V ABI documented?](https://stackoverflow.com/q/18133812). – Peter Cordes Aug 30 '18 at 03:40
  • related: https://stackoverflow.com/questions/1306414/what-is-the-meaning-and-usage-of-stdcall – Jean-François Fabre Aug 30 '18 at 03:43
  • Oh. A whole new world just opened up... (to me). Question answered. – RayaneCTX Aug 30 '18 at 04:01

0 Answers0