20.1.2 The C Calling Convention

Summary: C and SYSCALL are identical as calling conventions.

You must specify the C calling convention for MASM routines that link with C modules using the default calling convention. You can change the default calling convention for FORTRAN, Basic, and Pascal routines to the C calling convention, if you prefer. The characteristics of the C calling convention are summarized below.

Because the C calling convention allows for a variable number of arguments to be passed to the procedure, you may want to use this convention when you need this flexibility.

When you specify SYSCALL for the langtype, the C calling convention is used, but a leading underscore is not added to the name of the global routine (see the next section). SYSCALL is provided for compatibility with system calls in OS/2 version 2.0.

Argument Passing

With the C calling convention, the caller pushes arguments from right to left. The assembler places arguments on the stack in the reverse of the order that they appear in the source code. The first argument is lowest in memory (because it is the last argument to be placed on the stack, and the stack grows downward). The code to remove arguments from the stack follows the procedure call, so the caller pops arguments off the stack.

Register Preservation

The called routine should save BP, SI, DI, DS, and SS if they are modified.

Summary: C and SYSCALL allow a variable number of arguments.

Varying Number of Arguments

Because the first argument is always the last one pushed, it is always on the top of the stack. Thus, it has the same address relative to the frame pointer, regardless of how many arguments were actually passed. Therefore, calling procedures with a variable number of arguments are possible. If the high-level-language procedure uses the C calling convention and expects a variable number of arguments, the prototype for the function must end with :VARARG. See Section 7.3.3, “Declaring Parameters with the PROC Directive,” for information on using PROC and INVOKE with VARARG.