News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

C Calling Conventions

Started by dedndave, December 11, 2010, 03:08:34 AM

Previous topic - Next topic

dedndave

i am curious which conventions are in popular use by C compilers
if i make a static lib to be usable by C programmers, which convention(s) should i support ?
cdecl ???

hutch--

Dave,

Depends where you want to use the library, if its mainly to be called by C code in Windows or an environment mainly writtten in C OR if it has a variable number of parameters then use the C calling convention.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Glenn9999

Quote from: dedndave on December 11, 2010, 03:08:34 AM
i am curious which conventions are in popular use by C compilers
if i make a static lib to be usable by C programmers, which convention(s) should i support ?
cdecl ???

If you're concerned with interoperability with various platforms, stdcall functions associated with DLLs is usually the right choice.  That pretty much assures that anyone who is targeting Windows will be able to use it.

dedndave

well - that partially answers my question - lol
i probably could have provided more information
my primary intention is to support stdcall, of course
and - yes - i am mostly interested in windows platforms, as i am not sure if the library file format is even supported under linux
i know there are a number of popular C compilers - MS - turbo - and so on

Hutch - when you say "C calling convention", do you mean cdecl ? - same as stdcall, except the parms are not popped off the stack
on a side-note - what type of convention does PB use ?

Glenn - it isn't a DLL - it is a LIB

Glenn9999

Quote from: dedndave on December 11, 2010, 04:02:05 AM
Glenn - it isn't a DLL - it is a LIB

Right.  Not everyone can use LIB files.  Which is why I mentioned DLL as the option if you want complete interoperability for anybody who might use something.

dedndave

oh - i see - thank you
i figured most compilers would allow linking with static externals

dedndave

how about naming convention ?

if the stdcall routine is SomeCode
the C wrapper name would be ???

i realize they may do it the other way around and make a stdcall wrapper for the C version   :bg

MichaelW

I would use the C calling (and naming) convention.
eschew obfuscation

dedndave



japheth

Quote from: dedndave on December 11, 2010, 06:13:53 AM
how about naming convention ?

C(decl) is best for C compilers.

Many C compilers will be able to use the Masm output directly, because they use the very same naming convention for CDECL ( the underscore prefix) - MS VC, Borland, PellesC, OW, Digital Mars.

The GNU tools also use Cdecl, but there's often no '_' prefix. Agner's OBJCONV may be used to remove/add those decorations. However, the GNU tools provide 2 additional issues:

- they may have a different understanding of some relocation types, especially of the "PC-relative" ones.
- the "COFF" library format differs slightly from the one used in the "true" Windows world, and usually there's no appropriate error msg.


hutch--

Dave,

Your gain with cdecl (C calling convention) is its capacity to handle variable numbers of parameters but you also gain compaibility with most C compilers from doing so. STDCALL works fine on fixed argument counts.

PB ? STDCALL, C call, Basic call which equals old Pascal calling convention and to complicate matters it can pass data by value, by reference or by copy. It also handles optional parameters in function calls in a manner similar to C but not the same. then you can roll your own with PUSH / CALL and do FASTCALL as well. Clear as mud ?
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

FASTCALL works with JWasm but not with ML. Here is a little demo:
include \masm32\include\masm32rt.inc

.code
FastCallTest proc FASTCALL argx:DWORD,argy:DWORD
mov eax, argx
add eax, argy
ret
FastCallTest endp

CCallTest proc C argx:DWORD,argy:DWORD
mov eax, argx
add eax, argy
ret
CCallTest endp

SlowCallTest proc argx:DWORD,argy:DWORD
mov eax, argx
add eax, argy
ret
SlowCallTest endp

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
NoFrameTest proc argx:DWORD,argy:DWORD
mov eax, [esp+4]
add eax, [esp+8]
ret 8
NoFrameTest endp
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

start:
int 3   ; greetings to Olly
invoke  FastCallTest, ecx, edx
invoke  CCallTest, ecx, edx
invoke  SlowCallTest, ecx, edx
invoke  NoFrameTest, ecx, edx
inkey "Check it in Olly..."
exit
end start


Olly's disassembly:

CPU Disasm
Address      Hex dump                      Command                          Comments
FastCallTest  $  8BC1                      mov eax, ecx                     ; FastCallDemo.FastCallTest(void)
00401032      .  03C2                      add eax, edx
00401034      .  C3                        retn
CCallTest     $  55                        push ebp                         ; FastCallDemo.CCallTest(void)
00401036      .  8BEC                      mov ebp, esp
00401038      .  8B45 08                   mov eax, [ebp+8]
0040103B      .  0345 0C                   add eax, [ebp+0C]
0040103E      .  5D                        pop ebp
0040103F      .  C3                        retn
SlowCallTest  $  55                        push ebp                         ; FastCallDemo.SlowCallTest(void)
00401041      .  8BEC                      mov ebp, esp
00401043      .  8B45 08                   mov eax, [ebp+8]
00401046      .  0345 0C                   add eax, [ebp+0C]
00401049      .  5D                        pop ebp
0040104A      .  C2 0800                   retn 8
NoFrameTest   $  8B4424 04                 mov eax, [esp+4]                 ; FastCallDemo.NoFrameTest(void)
00401051      .  034424 08                 add eax, [esp+8]
00401055      .  C2 0800                   retn 8
<ModuleEntry  .  CC                        int3
00401059      .  8BD2                      mov edx, edx
0040105B      .  8BC9                      mov ecx, ecx
0040105D     Ú.  E8 CEFFFFFF               call FastCallTest                ; [FastCallTest
00401062     ³.  52                        push edx
00401063     ³.  51                        push ecx
00401064     ³.  E8 CCFFFFFF               call CCallTest                   ; [CCallTest
00401069     ³.  83C4 08                   add esp, 8
0040106C     ³.  52                        push edx
0040106D     ³.  51                        push ecx
0040106E     ³.  E8 CDFFFFFF               call SlowCallTest                ; [SlowCallTest
00401073     ³.  52                        push edx
00401074     ³.  51                        push ecx
00401075     ³.  E8 D3FFFFFF               call NoFrameTest                 ; [NoFrameTest
0040107A     ³.  68 00404000               push offset ??0019               ; ÚArg1 = ASCII "Check it in Olly..."
0040107F     ³.  E8 30000000               call StdOut                      ; ÀFastCallDemo.StdOut
00401084     ³.  E8 67000000               call wait_key                    ; [FastCallDemo.wait_key
00401089     ³.  68 14404000               push offset ??001A               ; ÚArg1 = ASCII CR,LF
0040108E     ³.  E8 21000000               call StdOut                      ; ÀFastCallDemo.StdOut
00401093     ³.  6A 00                     push 0                           ; ÚExitCode = 0
00401095     À.  E8 F8000000               call ExitProcess                 ; ÀKERNEL32.ExitProcess

Vortex


FORTRANS

Hi,

   Maybe this would be of interest?

Regards,

Steve

[PDF]
Download - Calling conventions
File Format: PDF/Adobe Acrobat - Quick View
5. Calling conventions for different C++ compilers and operating systems. By ...
www.agner.org/optimize/calling_conventions.pdf