News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Question about calling conventions

Started by Abdel Hamid, January 23, 2019, 03:16:46 AM

Previous topic - Next topic

Abdel Hamid

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

jj2007

Fastcall is occasionally a cycle faster, but that gain is negligible. Search the forum for more info.

Abdel Hamid

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

felipe

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:

felipe

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:

Abdel Hamid

thank you so much mr felipe , i really appreciate your help and your support   :eusa_clap: :t

hutch--

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.

Abdel Hamid

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:

TBRANSO1

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.

hutch--

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.

Abdel Hamid

@TBRANSO1 i totally agree with you my friend   :biggrin:
Well explained Mr.Hutch  :t

aw27

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.
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.

Abdel Hamid

this is perfect  :greenclp: that's exactly what i wanted to know
thank you sir 

daydreamer

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
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Vortex

QuoteI don't think MASM supports FASTCALL (the 32-bit FASTCALL)

AW is right. An attempt to solve this problem is to write some macros.