The MASM Forum

General => The Campus => Topic started by: Abdel Hamid on January 23, 2019, 03:16:46 AM

Title: Question about calling conventions
Post by: Abdel Hamid on January 23, 2019, 03:16:46 AM
Hello everyone , i hope you're having a good time
in order to master ASM i need to read first of all , then i need to ask over and over  :bgrin:

i have read about "calling convetions" turns out there is 6 models
cdecl , stdcall, fastcall, thiscall, vectorcall, clrcall
stdcall , arguments are passed from right to left , the callee is responsible for cleaning the stack frame
cdecl is the same as stdcall , but the caller is responsible for cleaning the stack frame
fascall argument are passed from right to left , the first two arguments are passed to register ecx and edx (someone correct me if am wrong)
i don't know about the others (in case you want to explain for me i'll be greatful)
____________
i conclude that fastcall is much faster than others because it's using registers instead of stack !
now how can i use it since i wanna create fast applications ? (if there is an example it will be great)
how can use cdecl (i just wanna know how , i tried to replace it by stdcall and it didn't work)  :icon_confused:

thank you in advance for helping me

Regards
Title: Re: Question about calling conventions
Post by: jj2007 on January 23, 2019, 03:41:32 AM
Fastcall is occasionally a cycle faster, but that gain is negligible. Search the forum for more info. (http://masm32.com/board/index.php?action=search;advanced;search=)
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 23, 2019, 05:10:20 AM
hello friend ,
thank you for replying my question !
i was totally confused about them  :icon_redface: , but i have read that stdcall is the default calling convention for 32bit WinAPI
so i guess other calling conventions are used in 64bit WinAPI  , am i right ?
note : (my system is 32bit)

Regards
Title: Re: Question about calling conventions
Post by: felipe on January 23, 2019, 06:20:23 AM
Quote from: Abdel Hamid on January 23, 2019, 05:10:20 AM
stdcall is the default calling convention for 32bit WinAPI
so i guess other calling conventions are used in 64bit WinAPI  , am i right ?

yes for 64 bits in windows you can look here for more info: https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2017  :idea:

Some (little) functions for the win api use cdecl too. Try to find something of that too, so you feel better with a broader view of calling conventions applied... :idea:
Title: Re: Question about calling conventions
Post by: felipe on January 23, 2019, 06:26:08 AM
Quote from: Abdel Hamid on January 23, 2019, 05:10:20 AM
i was totally confused about them  :icon_redface:

Btw, don't feel bad when you discover you were wrong, think that is better than now you have a chance to be right!  ;)  :icon14:
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 23, 2019, 06:30:12 AM
thank you so much mr felipe , i really appreciate your help and your support   :eusa_clap: :t
Title: Re: Question about calling conventions
Post by: hutch-- on January 23, 2019, 09:35:02 AM
Win32 and Win64 are different, in Win32 most are STDCALL which means pushing the last argument first and correcting the stack before returning to the caller. You also have the C calling convention which pushed the last argument first and the caller corrects the stack. This allows for a variable number of arguments to be passed.

Win64 is a very different animal, 4 registers for the first 4 arguments, if needed, stack space that is called shadow space for those arguments and for more than 4 arguments, a sequence of specific stack addresses.
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 23, 2019, 11:01:37 AM
Quote from: hutch-- on January 23, 2019, 09:35:02 AM
Win32 and Win64 are different, in Win32 most are STDCALL which means pushing the last argument first and correcting the stack before returning to the caller. You also have the C calling convention which pushed the last argument first and the caller corrects the stack. This allows for a variable number of arguments to be passed.

Win64 is a very different animal, 4 registers for the first 4 arguments, if needed, stack space that is called shadow space for those arguments and for more than 4 arguments, a sequence of specific stack addresses.

Hello Mr.Hutch , thank you for the info
you've mentioned that there is C calling convention , but pascal an C calling conventions were used in 16bit only, am i wrong ?
i got fatal error each time i try to link a simple app using "C calling convention"
for now i wanna know just the things which is related to win32 , otherwise i'll get confused again  :icon_mrgreen:
i won't move to win64 programming until i find myself comfortable with win32 am still a newbie   :biggrin:
Title: Re: Question about calling conventions
Post by: TBRANSO1 on January 23, 2019, 11:08:45 AM
Quote from: Abdel Hamid on January 23, 2019, 05:10:20 AM
hello friend ,
thank you for replying my question !
i was totally confused about them  :icon_redface: , but i have read that stdcall is the default calling convention for 32bit WinAPI
so i guess other calling conventions are used in 64bit WinAPI  , am i right ?
note : (my system is 32bit)

Regards

Just remember that you can run 32 bit programs on a 64 bit computer, but not the other way around.
Title: Re: Question about calling conventions
Post by: hutch-- on January 23, 2019, 01:14:42 PM
Win32 had both STDCALL and C calling conventions but almost all API functions are STDCALL. A few API calls are C calling convention but if you use any of the MSVCRT function they are also C calling convention. Which you use is determined by the prototype. STDCALL requires that the called procedure balances the stack, as a mnemonic it is like this "ret 16" where there are 16 bytes offset when the procedure is called.

With C calling convention you can pass a variable number of arguments so the caller must balance the stack after the function returns to the caller.
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 23, 2019, 03:58:30 PM
@TBRANSO1 i totally agree with you my friend   :biggrin:
Well explained Mr.Hutch  :t
Title: Re: Question about calling conventions
Post by: aw27 on January 24, 2019, 06:51:44 PM
Quote from: jj2007 on January 23, 2019, 03:41:32 AM
Fastcall is occasionally a cycle faster, but that gain is negligible. Search the forum for more info. (http://masm32.com/board/index.php?action=search;advanced;search=)
I don't think MASM supports FASTCALL (the 32-bit FASTCALL)

It supports these calling conventions:
PASCAL
BASIC
FORTRAN
SYSCALL
CDECL
STDCALL

Here is an example mixing of 4 calling conventions in the same program (syscall, stdcall. cdecl and basic):


.386

.model flat, syscall

includelib \masm32\lib\msvcrt.lib
_getch proto C
includelib \masm32\lib\kernel32.lib
ExitProcess proto stdcall :dword
includelib \masm32\lib\user32.lib
MessageBoxA PROTO stdcall :ptr, :ptr, :ptr, :dword

.data
myTitleInMain db "Press a Key",0
myStringInMain db "Press OK then any Key to exit",0
myTitleInBasicProc db "My caption in Basic Proc",0
myStringInBasicProc db "My String in Basic Proc",0

.code

basicProc proc basic
invoke MessageBoxA, 0, offset myStringInBasicProc, offset myTitleInBasicProc,  0
ret
basicProc endp

_main proc
invoke basicProc
invoke MessageBoxA, 0, offset myStringInMain, offset myTitleInMain,  0
invoke _getch
invoke ExitProcess,0
_main endp

end _main


In addition you can make a custom calling convention but it is an advanced technique.
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 24, 2019, 08:46:11 PM
this is perfect  :greenclp: that's exactly what i wanted to know
thank you sir 
Title: Re: Question about calling conventions
Post by: daydreamer on January 25, 2019, 04:28:57 AM
there are also advanced calls like COM calls,but there are both code with and without macros to look at, COM is used for example directx code,the reason for this indirect calls are because directx original design was to handle many different hardware,with different drivers with the same call so it works on all systems
for example nvidia and ATI driver function for drawprimitive probably are in different adresses,so directx solves that with COM interface,you load an adress at a specific place and calls that adress to perform drawprimitive independent of what computer it runs on
I wrote it because the information isnt complete without COM calls too
Siekmanski have created macro to handle COM,while felipe does it without macro if you want to check how code calling COM looks like
Title: Re: Question about calling conventions
Post by: Vortex on January 25, 2019, 05:30:43 AM
QuoteI don't think MASM supports FASTCALL (the 32-bit FASTCALL)

AW is right. An attempt (http://masm32.com/board/index.php?topic=7449.0) to solve this problem is to write some macros.
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 25, 2019, 05:37:01 AM
@daydreamer
@Vortex
thank you so much for the support , but am still a newbie and i guess this is advanced for me  :icon_redface:
anyways , am still following iczelion tutorials , i'll get back to this topic and read what you've mentioned carefully then i'll apply it as soon as i feel comfortable using assembler language  :t
Title: Re: Question about calling conventions
Post by: hutch-- on January 25, 2019, 06:24:28 AM
While you are learning, stick to the known calling conventions. You can emulate a version of FASTCALL using EAX, ECX & EDX but it can only be done with algorithms that you write yourself, it cannot be used with API calls. The vast majority are prototyped as STDCALL with C only used by a couple of API calls. If you use any MSVCRT functions, they are done in C calling convention.
Title: Re: Question about calling conventions
Post by: Abdel Hamid on January 25, 2019, 07:25:26 AM
hello Mr.Hutch ,
thank you for the informations  :t