www.riscos.com Technical Support: |
|
This chapter describes the features available within the Assembler for constructing conditional assembly statements and conditional looping statements.
The [ and ] directives mark the start and finish of sections of the source file which are to be assembled only if certain conditions are true. The basic construction is IF... THEN... ENDIF; however, ELSE is also supported, giving the full IF... THEN... ELSE... ENDIF conditional assembly.
The start of the section is known as the IF directive:
[ logical_expression or IF logical_expression
| or ELSE
and this is the ENDIF directive:
] or ENDIF
A block which is being conditionally assembled can contain several [ | ] directives; that is, conditional assembly can be nested.
You can use the IF and ENDIF directives (without the ELSE directive) like this:
[ logical_expression .......... ...code... .......... ]
The code will only be assembled if the logical expression is true; it will be skipped if the logical expression is false.
Alternatively you can use all three directives, thus:
[ logical_expression ......................... ...first piece of code... ......................... | .......................... ...second piece of code... .......................... ]
If the logical expression is true, the first piece of code will be assembled and the second skipped. If the expression is false, the first piece of code will be skipped and the second assembled.
Lines conditionally skipped by these directives are not listed unless ObjAsm is switched from its default terse mode. For desktop assembly, you must choose NoTerse from ObjAsm's menu (see Listings); for command line usage, you must specify the -NoTerse command line option (see Command line options available from the desktop).
An example of a notional data storage routine is given below. This routine can either use a disc or a tape data storage system. To assemble the code for tape operation, the programmer prepares the system by altering just one line of code, the label SWITCH.
DISC * 0 TAPE * 1 SWITCH * DISC ...code... [ SWITCH=TAPE ...tape interface code... ] [ SWITCH=DISC ...disc interface code... ] ...code continues...
or alternatively:
DISC * 0 TAPE * 1 SWITCH * DISC ...code... [ SWITCH=TAPE ...tape interface code... | ...disc interface code... ] ...code continues...
The IF construction can be used inside macro expansions as easily as it is used in the main program.
It is often useful for program segments and macros to produce tables. To do this, they must be able to have a conditional looping statement. The Assembler has the WHILE... WEND construction. This produces an assembly time (not runtime) loop.
The syntax is:
WHILE logical_expression
to start the repetitive block, and:
WEND
to end it.
For example:
GBLA counter counter SETA 100 WHILE counter >0 DCD &$counter counter SETA counter-1 WEND
produces the same result as the following (but is shorter and less prone to typing errors):
DCD 100 DCD 99 DCD 98 DCD 97 : DCD 2 DCD 1
Since the test for the WHILE condition is made at the top of the loop, it is possible that the source within the loop will not generate any code at all.
Listing of conditionally skipped lines is as for conditional assembly.