Stack Considerations in Calling Conventions

In the C calling convention, the calling routine always adjusts the stack immediately after the called routine returns control. This produces slightly larger object code because the code that restores the stack must exist at every point a procedure is called. In the STDCALL calling convention, the called procedure controls the stack. The code to restore the stack resides in the called procedure, so the code needs to appear only once.

However, the C calling convention makes calling with a variable number of arguments possible. Since in the C calling convention the caller cleans up the stack, it is possible to write a routine with a variable number of arguments. Therefore, it has the same address relative to the frame pointer, regardless of how many arguments are actually passed. Because of this, when the calling routine controls the stack, it knows how many arguments it passed, how big they are and where they reside in the stack. It can thus skip passing an argument and still keep track.

You can call routines with a variable number of arguments by including the ATTRIBUTES C and VARYING options in your interface to a routine. The VARYING option prevents Fortran from enforcing a matching number of arguments in routines. The VARYING option is not necessary with intrinsic Fortran 90 routines with optional arguments, where argument order and/or keywords determine which arguments are present and which are absent.

In MASM, stack control is also set by the C or STDCALL convention declared for the procedure, but you can write MASM code to control the stack within the procedure any way you wish. In addition, you can specify the USES option in the PROC directive to save and restore certain registers automatically.