Article ID: 73407 - View products that this article applies to.
This article was previously published under Q73407
With the Microsoft Macro Assembler (MASM) version 6.0, the coding for a procedure call may be simplified by the use of the PROTO and INVOKE directives. These directives handle many of the details, such as pushing the parameters on the stack in the correct order, generating the correct external references, coercing arguments to the correct size, and cleaning up the stack (if required) after the function terminates.
The two sample programs below illustrate how a C function is called from MASM, both with and without the PROTO and INVOKE directives. Sample Code 1 calls the C printf() function using the conventional method of coding. Sample Code 2 shows the simplified call to the same function through the use of PROTO and INVOKE.
PROTO defines a prototype for a procedure much the way a function prototype works in C. This is the syntax for PROTO:
The PROTO statement is used by the assembler to check parameter types and quantity along with indicating the naming convention for the function. Arguments for the function are indicated by listing the type, and optionally, a parameter name. For example,
This indicates that the function myfunc takes two arguments. The first is a signed word, the second is a signed byte. If you need a variable argument list, you use the type VARARG.
INVOKE actually generates the code to call the function. You must have defined the function previously with either a PROC, an EXTERNDEF, a TYPEDEF, or a PROTO statement. This is the syntax for INVOKE:
Because the assembler knows what the function is expecting in the way of arguments and calling convention, it can take the arguments passed in the INVOKE statement and push them on the stack in the correct order, call the function using the required function name, and clean up the stack afterwards (if required by the calling convention used).
If an argument passed by INVOKE is smaller than the type specified in the PROTO statement, MASM does a type conversion. It widens the argument in the PROTO statement to match that in the INVOKE statement (for example, SBYTE to SWORD). These types of conversions use the AX and DX registers on the 8086 and 8088 and the EAX and EDX registers on the 80386/80486. Because these registers are effectively overwritten, you should take care to avoid using these registers to pass arguments.
The language type for the function determines the naming and calling conventions. In addition to the language type in the PROTO statement, the language type can be set by the .MODEL directive, the OPTION LANGTYPE:, or by the command line switches /Gc (for Pascal) and /Gd (for C). There is a table of the various language conventions provided in Help.
Sample Code 1
Sample Code 2
Article ID: 73407 - Last Review: October 20, 2003 - Revision: 2.0
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Contact us for more help