Guide: Function Calling Conventions
GCC follows certain rules in generating and calling its functions. If you are writing portable C or C++ code, you never need to know about these rules. However, if you are writing assembly language or nonportable code that depends on these rules, you need to know what they are. This document attemps to describe them, and gives some examples.
Notes
This document assumes a familiarity with assembly language. The assembler code used here is written in the AT&T syntax, as used by GNU as. If you’re using an Intel-syntax assembler, like nasm, you’ll have to translate appropriately.
What’s described here are GCC’s standard calling conventions. Many can be changed by using options like -mregparm, but that’s outside the scope of this document.
These conventions apply to C. C++ introduces several additional complications (such as class pointers and name mangling), some of which can change between compiler versions. Thus, I suggest that asm functions called from C++ code be declared as extern "C". This will cause C calling conventions to be used.
Writing Assembly-Language Functions
Naming
In DJGPP, a function’s assembly-language name is the same as its C name, with an underscore (“_“) prepended. Thus, the C function foo would be named _foo in assembly language. (This is in fact true for all symbol names, such as variables.) C++ has some much more complicated rules.
Registers
GCC requires that some registers not change across a function call. If you want to use these registers in an assembly function, you must save and restore their values. They are:
- %ebx
- %esi
- %edi
- %ebp
- The segment registers %ds, %es and %ss
Other registers are available for your use (though some have other special uses; read on).
- Integers (of any size up to 32 bits) and pointers are returned in the %eax register.
- Floating point values are returned in the 387 top-of-stack register, st(0).
- Return values of type long long int are returned in %edx:%eax (the most significant word in %edx and the least significant in %eax).
- Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)
If your function returns void (e.g. no value), the contents of these registers are not used.
Last argument
...
4(%esp) First argument
(%esp) Return address
- Integers up to 32 bits and pointers are pushed as a single longword.
- long long int is pushed as two longwords; the least significant is pushed last (and so is located first in memory).
- float and double are pushed as a double-precision value, occupying 8 bytes.
- long double is pushed as an extended-precision value followed by 2 bytes of padding, totalling 12 bytes.
- As before, structures are more complicated and best avoided.
These rules also apply to functions which take a variable number of arguments (like printf). As with any variadic function, the function must find its own way of determining how many arguments were actually passed (usually based on one of the required args).
The stack below the return address is available for temporary storage, but be sure to decrement %esp appropriately. Memory below %esp may be overwritten asynchronously, by interrupt handlers and such. Restore its value when exiting, so that the return works correctly. You may also push and pop at will.
You may modify your arguments in place if you wish; they will not be reused by the caller. Do not, however, attempt to pop them; the caller handles this.
Calling C Functions From Assembly Language
An assembly language function may wish to call a function written in C, either your own or one from the standard library. The same rules already explained apply; you just see them from the other side.
First, you push the function’s arguments (if any) onto the stack, last argument first. See above for the formats used. (Floating point values are usually most easily handled by making space on the stack and then executing a store instruction; i.e. subl $8,%esp; fstpl (%esp).)
Use a simple call instruction to call the function.
You are responsible for removing the arguments you have pushed. They may have changed, so you may not reuse them. You need not, however, discard them at once; it may be more convenient when calling several functions to leave the arguments on the stack and pop them all together at the end. addl n,%esp is an efficient way to do this. It may also be convenient in this case to use %ebp as a frame pointer, since it need not change all the time. (The C compiler does this.)
The return value may be found as detailed above.
Expect the registers %eax, %ecx, and %edx, as well as the floating-point stack, to have changed. Standard library functions may modify the %gs register, and the_far* functions may modify %fs. Other registers will be preserved.
Conclusion
These are the basic calling conventions used by GCC; however, there are special cases, optional modifications, etc. that can apply in situations not covered here. In this case, gcc -S is your best friend – from assembly output, you can usually figure out the rules. Also helpful is the GCC source: see i386.h and i386.md inconfig/i386. They are well commented.
Examples
(略,见原文)
原文:http://www.delorie.com/djgpp/doc/ug/asm/calling.html
Tags: C, Gcc, Linux