flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > Calling Convention Confusion

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 46
I'm trying to determine which calling convention to use in my simple programs which are using msvcrt functions. For example:

Code:
format PE console
entry main
include 'macro/import32.inc'

section '.rdata' data readable
msg db 'Hello world.', 0
p db 'pause>nul', 0
section '.code' code readable executable

main:
push msg
call [printf]
add esp, 4

push p
call [system]
add esp, 4
push 0
call [exit]

section '.idata' import data readable
library msvcrt, 'msvcrt.dll'
import msvcrt, \
printf, 'printf', \
system, 'system', \
exit, 'exit'    


You can see here how I attempt to clean up the stack with add esp, 4 and I typically do this after any of the msvcrt function calls. Is this necessary? I ask because I know stdcall is usually used in Microsoft libs and I believe the callee is supposed to clean it up but there have been times I built programs where it didn't seem to do so...

So is this actually cdecl or stdcall? Thanks.

_________________
-moveax41h
Post 07 Mar 2018, 09:17
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15912
Location: SDSS J140821.67+025733.2
MSVCRT is 100% C-call. You must restore the stack in your code.

Most of the Windows 32-bit API is std-call. The API will restore the stack. The only exception here is the vararg functions (wsprintf), which are c-call.

Note that MSVCRT is not part of the official Windows API.
Post 07 Mar 2018, 09:22
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 305
Location: Belarus
Also remember that there are handy macros for calling functions of different calling conventions in the FASM package. MACRO\PROC32.inc is your choice. Saves a few lines of code and makes manual parameter counting unnecessary.
Post 07 Mar 2018, 10:49
View user's profile Send private message Visit poster's website 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-2018, Tomasz Grysztar.

Powered by rwasa.