9.2.2 Passing Arguments to Macros

Summary: Parameters allow macros to execute variations of a general task.

By defining parameters for macros, you can define a general task and then execute variations of it by passing different arguments each time you call the macro. The complete syntax for a macro procedure includes a parameter list:

name MACRO parameterlist
statements
ENDM

The parameterlist can contain any number of parameters. Use commas to separate each parameter in the list. Parameter names cannot be reserved words unless the keyword has been disabled with OPTION NOKEYWORD, the compatibility modes have been set by specifying OPTION M510 (see Section 1.3.2), or the /Zm command-line option has been set.

To pass arguments to a macro, place the arguments after the macro name when you call the macro:

macroname arglist

All text between matching quotation marks in an arglist is considered one text item.

The beep macro introduced in the last section used the DOS interrupt to write the bell character (ASCII 7). It can be rewritten with a parameter to specify any character to write.

writechar MACRO char

mov ah, 2 ;; Select DOS Print Char function

mov dl, char ;; Select ASCII char

int 21h ;; Call DOS

ENDM

Wherever char appears in the macro definition, the assembler replaces it with the argument in the macro call. Each time you call writechar, you can print a different value:

writechar 7 ; Causes computer to beep

writechar 'A' ; Writes A to screen

If you pass more arguments than there are parameters, the additional arguments generate a warning (unless you use the VARARG keyword; see Section 9.4.3). If you pass fewer arguments than the macro procedure expects, remaining parameters are assigned empty strings (unless default values have been specified). This may cause errors. For example, if you call the writechar macro with no argument, it generates the following:

mov dl,

The assembler generates an error for the expanded statement but not for the macro definition or the macro call.

Macros can be made more flexible by leaving off macro arguments or adding additional ones. The next section tells some of the ways you can handle missing or extra arguments.