20.1 Naming and Calling Conventions

The naming convention specifies the way the compiler or assembler alters the name of the routine or identifier before placing it into an object file. Each language alters the name of the identifiers. You must be sure that the naming conventions for mixed-language programming are compatible.

A calling convention specifies the way a language implements a call to a procedure. MASM implements mixed-language calls according to the particular calling convention specified in the procedure declaration or prototype.

MASM supports three different calling conventions. The assembler uses the C calling convention when the langtype is C or SYSCALL; it uses the Pascal calling convention when the langtype is PASCAL, BASIC, or FORTRAN; and it uses the STDCALL calling convention when the langtype is STDCALL. To MASM, BASIC, PASCAL, and FORTRAN are synonymous when specifying the Pascal calling convention for a procedure.

There are several ways to set the calling convention. Using .MODEL with a langtype sets the default for the module. You can also use the OPTION directive to do the same. This is equivalent to the /Gc or /Gd option from the command line. Procedure prototypes and declarations can specify a langtype to override the default.

Summary: You can change the default calling convention.

When you write mixed-language routines, the easiest way to ensure calling convention compatibility is to adopt the calling conventions of the language of the called procedure. However, Microsoft languages (except QuickPascal) can change their calling conventions, so at times you may want to change the calling convention to use a particular argument-passing method instead of the defaults for a particular language. Section 20.4 explains how to change the calling convention. The fastcall calling convention is not directly supported by the assembler. This section provides more detail on the information summarized in Table 20.1:

Table 20.1 Naming and Calling Conventions

Convention dddC SYSCALL STDCALL B BASIC, FORTRAN PASCAL  

Leading underscore dddX   ddddX   ,  
Capitalize all       BA BX FORX PA X
Arguments pushed left to right       BS X FORX, PA X  
Arguments pushed right to left dddX SYSX, ddddX   ,    
Caller stack cleanup dddX   dddd*   ,  
:VARARG allowed dddX SYSX ddddX, ,    

* The STDCALL language type uses caller stack cleanup if the :VARARG parameter is used. Otherwise, the called routine must clean up the stack.