News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Calling convention

Started by n00b!, September 26, 2008, 08:26:47 PM

Previous topic - Next topic

n00b!

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.

jdoe


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.


fearless

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

n00b!

#3
Simple reconstruction of post: Why can I use wsprintf in TASM as any other function (ignoring the c calling convention) without using prototypes?

MichaelW

QuoteFor those of you crazy enough to write in assembly language. . .
  :toothy
eschew obfuscation