Accepting the calling convention when combining C and x86 Assembly

I have some build procedures that call and accept arguments from C functions. Right now, I am assuming that these arguments are passed on the stack in cdecl order. Is this a fair guess?

Can the compiler (GCC) detect this and make sure the arguments are passed correctly, or should I manually go over and declare them cdecl? If so, will this attribute be preserved if I specify a higher level of optimization?

+4
source share
3 answers

This is an ABI question for the platform on which you are writing code. Nearly all platforms follow the Unix System V ABI to invoke C call and other ABI problems, which includes both a general ABI document (gABI) describing the general ABI characteristics in all CPU architectures and a processor-specific ABI document (psABI), specific to a particular architecture / processor family. When it comes to x86, it matches what you call "cdecl". Thus, from a practical point of view, an x86 assembly intended to be called from C must be written to accept "cdecl". Basically, the only exception to the universality of this calling convention is the Windows API functions, which use their own custom stdcall calling convention due to legacy WinLL dll thunk compatibility issues; however, the default assignment convention for x86 Windows is still "cdecl".

A more important issue when writing asm for a call from C is whether to have character names with an underscore prefix or not. This varies widely between platforms, with the general trend being that ELF-based platforms do not use a prefix, and most other platforms ...

+1
source

Invoking conventions mean much more than just ordering arguments. There is a good pdf version explaining all the details written by Agner Fog: Invoking conventions for different C ++ compilers and operating systems .

+2
source

A quick and dirty way to do this is to create a dummy C function that matches the asm function you want to implement, do a few things in the dummy C function with the parameters passed so you can tell them apart, then compile to parse. Not sure, but it works often.

0
source

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


All Articles