Assembler supports the use of Conditional Statement in Assembly Language Program. It also allows to use them in macro sequences. These conditional statements control the flow of the program execution. Let us see the conditional statements in the assembly language program and conditional statements used in macros.
Conditional Statement in Assembly Language Program .IF – .ELSE – .ENDIF Statement
The conditional statements are implemented in the assembly language program using .IF, ELSE, ENDIF structure found in higher level language. Only MASM version 6-X supports this. The earlier versions of the assembler does not support IF statement. Here is the general format for the IF conditional statement.
As shown above every .IF directive must have a matching ENDIF to terminate a tested condition. ELSE is optional. It provides an alternate action. The assembly also allows to use relational operators with .IF statement.
.WHILE – .ENDW Statement
Like DO-WHILE statement in higher level language, the assembler supports .WHILE- .ENDW statement. The WHILE statement is used with a condition to begin the loop, and the .ENDW statement ends the loop. This statement is also supported by only 6.X versions of MASM.
.BREAK and .CONTINUE Statements
.BREAK and .CONTINUE statements function in the same manner in a C-language program. The .BREAK statement is used to break out of the .WHILE loop. The program 13 shows how .BREAK statement is used to terminate the WHILE loop if character does not match. This avoids further iterations which are not necessary when any earlier character is not matched.
.REPEAT – .UNTIL Statement
.REPEAT – .UNTIL statements allow to execute series of instructions repeatedly until some condition occurs. The .REPEAT defines the start of the loop and .UNTIL defines the end of loop. A .UNTIL statement has a condition. When condition is true loop is terminated. It is important to note that .REPEAT and .UNTIL statements are available to version 6.X of MASM. Program 14 uses .REPEAT and .UNTIL statements. This program lists all alphabets.
Conditional Assembly Statements in Macros IF-ELSE-ENDIF Statement
The conditional assembly statements are implemented in macros using IF – ELSE – ENDIF structure found in higher level languages. Here is the general format for the IF family of conditional statements.
As shown above every IF directive must have a matching ENDIF to terminate a tested condition. ELSE is optional. It is use to provide an alternate action. ‘XX’ after IF defines various forms used for IF statement.
In macro the REPEAT statement is used to repeat macro sequence for a fix number of time. The repetition count is specified immediately after the REPEAT statement as shown in the program. The program shows a macro definition which sends 26 ASCII characters from A through Z to the video screen. The statements within the REPEAT and the first ENDM are repeated 26 times. It is important to note that this macro has two ENDM statements. The first represents end of REPEAT and second represents end of MACRO.
In macro, the WHILE statement is used to repeat macro sequence until the expression specified with it is true. Like REPEAT, end of loop is specified by ENDM statement. The WHILE statement allows to use relational operators in its expression. The Table 8.6 shows the relational operators used with WHILE statements.
A FOR statement in the macro repeats the macro sequence for a list of data. For example, if we pass two arguments to the macro then in the first iteration the FOR statement gives the macro sequence using first argument and in the second iteration it gives the macro sequence using second argument. Like WHILE statement, end of FOR is indicated by ENDM statement. The program shows the use of FOR statement in the macro.