1.3.3 Conditional Directives

MASM 6.0 provides conditional-assembly directives and conditional-error directives. You can also use conditional-assembly directives when you want to test for a specified condition and assemble a block of statements if the condition is true. You can use conditional-error directives when you want to test for a specified condition and generate an assembly error if the condition is true.

Both kinds of conditional directives test assembly-time conditions, not run-time conditions. Only expressions that evaluate to constants during assembly can be compared or tested. Predefined symbols are often used in conditional assembly. See Section 1.2.3.

Conditional-Assembly Directives

The IF and ENDIF directives enclose the statements to be considered for conditional assembly. The optional ELSEIF and ELSE blocks follow the IF directive. There are many forms of the IF and ELSE directives. Online help provides a complete list.

The syntax used for the IF directives is shown below. The syntax for other condition-assembly directives follow the same form.

IFexpression1
ifstatements
[[ELSEIFexpression2
elseifstatements]]
[[ELSE
elsestatements]]
ENDIF

The statements following the IF directive can be any valid statements, including other conditional blocks, which in turn can contain any number of ELSEIF blocks. ENDIF ends the block.

The statements following the IF directive are assembled only if the corresponding condition is true. If the condition is not true and an ELSEIF directive is used, the assembler checks to see if the corresponding condition is true. If so, it assembles the statements following the ELSEIF directive. If no IF or ELSEIF conditions are satisfied, the statements following the ELSE directive are assembled.

For example, you may want to assemble a line of code only if a particular variable has been defined. In this example,

IFDEF buffer

buff BYTE buffer DUP(?)

ENDIF

buff is allocated only if buffer has been previously defined.

The following list summarizes the conditional-assembly directives:

Directive Use  
IF and IFE Tests the value of an expression and allows
assembly based on the result.
 
IFDEF and IFNDEF Tests whether a symbol has been defined and allows assembly based on the result.  
IFB and IFNB Tests to see if a specified argument was passed to a macro and allows assembly based on the result.  
IFIDN and IFDIF Compares two macro arguments and allows assembly based on the result. (IFDIFI and IFIDNI perform the same action but are case insensitive.)  

Conditional-Error Directives

You can use conditional-error directives to debug programs and check for assembly-time errors. By inserting a conditional-error directive at a key point in your code, you can test assembly-time conditions at that point. You can also use conditional-error directives to test for boundary conditions in macros.

Like other severe errors, those generated by conditional-error directives cause the assembler to return a nonzero exit code. If a severe error is encountered during assembly, MASM does not generate the object module.

For example, the .ERRNDEF directive produces an error if some label has not been defined. In this example, .ERRNDEF at the beginning of the conditional block makes sure that a publevel actually exists.

.ERRNDEF publevel

IF publevel LE 2

PUBLIC var1, var2

ELSE

PUBLIC var1, var2, var3

ENDIF

These directives use the syntax given in the previous section. The following list summarizes the conditional-error directives.

Directive Use
.ERR Forces an error where the directives occur in the source file. The error is generated unconditionally when the directive is encountered, but the directives can be placed within conditional-assembly blocks to limit the errors to certain situations.
.ERRE and .ERRNZ Tests the value of an expression and conditionally generates an error based on the result.
.ERRDEF and .ERRNDEF Tests whether a symbol is defined and conditionally generates an error based on the result.
.ERRB and .ERRNB Tests whether a specified argument was passed to a macro and conditionally generates an error based on the result.
.ERRIDN and .ERRDIF Compares two macro arguments and conditionally generates an error based on the result. (.ERRIDNI and .ERRDIFI perform the same action but are case sensitive.)