Document Navigation
Table Of Contents
Previous
Next

VI. CONDITIONAL ASSEMBLY

This assembler supports "conditional assembly" or the ability to assemble only certain portions of your source program depending on the conditions at assembly time. Conditional assembly is particularly useful in situations where you might need several versions of a program with only slight changes between versions.

As an example, suppose we required a different version of some program for 4 different systems whose output routines varied. Rather than prepare four different source listings, we could prepare one which would assemble a different set of output routines depending on some variable which was set with an EQU directive near the beginning of the source. Then it would only be necessary to change that one EQU statement to produce any of the four final programs. This would make the software easier to maintain, as besides only needing to keep track of one copy of the source, if a change is required in the body of the program, only one edit is required to update all versions.

THE IF-ENDIF CLAUSE

In its simplest form, conditional assembly is performed with two directives: IF and ENDIF. The two directives are placed in the source listing in the above order with any number of lines of source between. When the assembler comes across the IF statement, it evaluates the expression associated with it (we will discuss this expression in a moment) and if the result is true, assembles all the lines between the IF and ENDIF and then continues assembling the lines after the ENDIF. If the result of the expression is false, the assembler will skip all lines between the IF and ENDIF and resume assembly of the lines after the ENDIF. The proper syntax of these directives is as follows:

IF <expression>
.
. conditional code goes here
.
ENDIF

The ENDIF requires no additional information but the IF requires an expression. This expression is considered FALSE if the 16-bit result is equal to zero. If not equal to zero, the expression is considered TRUE.

A more powerful conditional assembly construct is possible with the ELSE directive. The ELSE directive may be placed between the IF and ENDIF statements. Its effect is to switch the sense of the test for the lines between it and the ENDIF. In effect, the lines of source between the IF and ENDIF are split into two groups by the ELSE statement. Those lines before the ELSE are assembled if the expression is true while those after (up to the ENDIF) are ignored. If the expression is false, the lines before the ELSE are ignored while those after it are assembled. The IF-THEN-ELSE construct appears as follows:

IF <expression>
.
. this code assembled if expression is true
.
ELSE
.
. this code assembled if expression is false
.
ENDIF

The ELSE statement does not require an operand and there may be only one ELSE between an IF-ENDIF pair.

It is possible to nest IF-ENDIF clauses (including ELSE's). That is to say, an IF-ENDIF clause may be part of the lines of source found inside another IF-ENDIF clause. You must be careful, however, to terminate the inner clause before the outer.

There is another form of the conditional directive, namely IFN which stands for "IF Not". This directive functions just like IF except that the sense of the test is reversed. Thus the code immediately following is assembled if the result of the expression is NOT TRUE. An IFN-ELSE-ENDIF clause appears as follows:

IFN <expression>
.
. this code assembled if expression is FALSE
.
ELSE
.
. this code assembled if expression is TRUE
.
ENDIF

The IFC-ENDIF Clause

Another form of conditional assembly is very similar to the IF-ENDIF clause defined above, but depends on a comparison of two strings for its conditionality instead of a true-false expression. This type of conditional assembly is done with the IFC and IFNC directives (for "IF Compare" and "IF Not Compare") as well as the ELSE and ENDIF discussed above. Thus for the IFC directive we have a clause like: IFC <string 1>,<string 2>
.
. this code assembled if strings are equal
.
ELSE
.
. this code assembled if strings are not equal
.
ENDIF

As can be seen, the two strings are separated by a comma. There are two types of strings, one enclosed by delimiters the other not. The delimited type may use either a single quote (') or double quote (") as the delimiter. This type of string is made up of all the characters after the first delimiter until the second delimiter is found. The second type of string is simply a group of characters, starting with a non-space and containing no spaces or commas. Thus if you need spaces or commas in a string, you must use the delimited type of string. It is possible to specify a null string by placing two delimiters in a row or by simply leaving the string out completely. Note that there may be no spaces after string 1 and before the separating comma nor after the comma and before string 2. As with IFN, the IFNC directive simply reverses the sense of the test such that code immediately following an IFNC directive would be assembled if the strings did NOT compare.

A common application of this type of conditional assembly is in macros (defined in the next section) where one or both of the strings might be a parameter passed into the macro.

The IF-SKIP Clause

The IF-skip type of conditional assembly is a method which does not use (in fact does not allow) a related ENDIF or ELSE. Instead, the assembler is caused to skip a specified number of lines of source depending on the result of the expression or string comparison.

IMPORTANT NOTE:
This type of conditional assembly is ONLY allowed within the body of a macro. Any use of it outside a macro will result in an error. Macros are defined in the next section.

As before, the possible directives are: IF, IFN, IFC, and IFNC. This type of conditional assembly is performed with a single instruction. Instead of code being assembled on a true result, the specified number of lines are SKIPPED. This number of lines can be in a forward or reverse direction. The syntax is as shown:

IF <expression>,<skip count>
or
IFC <string 1>,<string 2>,<skip count>

The skip count must be a decimal number between 1 and 255. It may be preceded by a plus or minus sign. A positive number produces a forward skip while a negative number produces a backwards skip. A skip count of zero has no effect (the instruction following the IF directive will be executed next). A skip count of one will cause the second line after the IF statement to be the next one executed (the one line directly following the IF statement is ignored). A skip count of negative one will cause the line just before the skip count to be the next one executed. The assembler will not skip past the end or beginning of the macro which contains the IF-skip statement. If a skip count is specified which is beyond these limits, the assembler will be left to the last statement in the macro or the first, depending on whether the skip count was positive or negative. There can be no spaces before or after the comma which separates the skip count from the expression or from string 2.

IMPORTANT NOTE

In order for conditionals to function properly, they must capable of evaluation in pass one so that the same result will occur in pass two. Thus if labels are used in a conditional expression, they must have been defined in the source before the conditional directive is encountered.


Table Of Contents Previous Next
Document Navigation