flat assembler
Message board for the users of flat assembler.

Index > Main > Your preferred calling convention

Author
Thread Post new topic Reply to topic
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
Which calling convention do you use when you design a medium/large library in fasm, in x86 (As x64 uses fastcall anyway), but do you prefer stdcall or cdecl? And why do you prefer it?

Personally, I like cdecl because it allows you to create generalized forms of data in structures and re-use those structures across functions without fixing the stack.

For example, if most functions in a library depend on a, b, c, d, e, f, g data elements and these data elements are needed in 80% of all functions, you can just pass a pointer to that structure and never have to fix the stack when you need to call multiple functions in multiple calls. And if a function needs extra data, you can put a pointer to that extra data inside a reserved dword inside the generalized structure.

I don't know what you think about all of that, let me know your opinion. Teach me to better design functions, or let me know how you do it or prefer it.
Post 22 Jul 2015, 09:59
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
__fastcall.
Post 22 Jul 2015, 14:44
View user's profile Send private message Send e-mail Reply with quote
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
Thanks for your response, I would appreciate an explanation why you prefer one convention over the other. (Hopefully not for creating a debate or to create disagreement between different people, but to see the different perspectives from people)
Post 22 Jul 2015, 14:56
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Well, I write my code in FASM only, no integration with other packages.
Pushing parameters to stack seems non-Assemblery - if that is a word, probably not.
If I'll use any other convention, my code probably would be no better than C/C++ or maybe even worse.
Smile
When I say __fastcall - I do not mean standard, described in MSDN, I mean that parameters passed in registers,
can be any register.
Post 22 Jul 2015, 18:57
View user's profile Send private message Send e-mail Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
Quote:

but do you prefer stdcall or cdecl? And why do you prefer it?

CDECL. Because I can choose when to change ESP.

LOL!
Post 23 Jul 2015, 18:42
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
For library public procedures STDCALL - IMO, it is more flexible than others (especially for x86!) and allows some tricks that help optimizing register use.

Sometimes for very simple and for internal use only procedures I use register parameter passing, but it has some great disadvantages.
Post 25 Jul 2015, 08:20
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
As it's a library, it's less about preference and more about suitability. If it's aimed at HLL usage, mixing conversions can be both advantageous and detrimental, depending on the critical path. In some cases, one won't have a choice due to the target platform or other libraries enforcing the convention (thread entry points/callbacks etc).

If the library is not intended for HLLs (or just the odd top-level do-all-the-things calls), then I normally aim for "native" convention where I avoid common registers, where appropriate, and pass parameters and return values in and out of procedure-specific registers. I typically end up using r15-8 xmm15-8 etc as this avoids both general purpose instructions (think rep cmpsb etc) and calling convention collisions and therefore reduces state swapping overhead.
Post 25 Jul 2015, 08:56
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.