The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on September 26, 2008, 08:26:47 PM

Title: Calling convention
Post by: n00b! on September 26, 2008, 08:26:47 PM
Simple reconstruction of post: Whats different when calling a function which uses the c calling convention compared to calling a stdcall function?

I ask since I'm wondering why I can invoke wsprintf normally without caring about the push-order.
Title: Re: Calling convention
Post by: jdoe on September 26, 2008, 08:58:05 PM

n00b!,

The reason is by the CONVENTION / PROTOTYPE / INVOKE. The procedure prototypes tells INVOKE how to push the arguments on the stack. If you are not using INVOKE, you must do the job yourself and push the arguments as the calling convention expect.

http://blogs.msdn.com/oldnewthing/archive/2004/01/08/48616.aspx

INVOKE is a macro directive doing more then just pushing arguments as you typed it.

Title: Re: Calling convention
Post by: fearless on September 26, 2008, 09:00:48 PM
From the masm32.hlp (or .chm) reference:

QuoteINVOKE expression [,arguments]
Automates the call interface to stack-based procedures. Handles all pushing of parameters onto the stack. Cleans up the stack when the procedure returns.
The <invokelist> is passed to the procedure according to the types given in that procedure's prototype. If necessary, the assembler will generate code to convert the elements of <invokelist> to the types specified in the prototype.

Using invoke:

Quote...has many advantages in terms of both coding speed and reliability. A procedure called by INVOKE is type checked against a prototype of the same name for parameter size and number.

Ideally use invoke as it is clearer when coding and easier to use, and you wont run into a problem of accidentally pushing the wrong parameter with push...call syntax.

From Win32.hlp regarding wsprintf:

QuoteNote  Unlike other Windows functions, wsprintf uses the C calling convention (_cdecl), rather than the Pascal calling convention. As a result, it is the responsibility of the calling process to pop arguments off the stack, and arguments are pushed on the stack from right to left. In C-language modules, the C compiler performs this task.

As wsprintf can have multiple optional arguments it has to use the C calling convention, whereas most other api's have a defined amount of parameters.
Title: Re: Calling convention
Post by: n00b! on September 26, 2008, 09:43:04 PM
Simple reconstruction of post: Why can I use wsprintf in TASM as any other function (ignoring the c calling convention) without using prototypes?
Title: Re: Calling convention
Post by: MichaelW on September 27, 2008, 11:11:27 PM
QuoteFor those of you crazy enough to write in assembly language. . .
  :toothy
Title: Re: Calling convention
Post by: Vortex on September 28, 2008, 08:08:10 AM
n00b!,

Agner Fog's work on Calling conventions (http://agner.org/optimize/calling_conventions.pdf) is what you need.