Keil Logo Arm Logo

IF, ELSE, ENDIF, and ELIF

IF, ELSE, ENDIF, and ELIF

The IF directive introduces a condition that is used to decide whether to assemble a sequence of instructions and directives. [ is a synonym for IF.

The ELSE directive marks the beginning of a sequence of instructions or directives that you want to be assembled if the preceding condition fails. | is a synonym for ELSE.

The ENDIF directive marks the end of a sequence of instructions or directives that you want to be conditionally assembled. ] is a synonym for ENDIF.

The ELIF directive creates a structure equivalent to ELSE IF, without the requirement for nesting or repeating the condition.

Show/hideSyntax

    IF logical-expression           …;code
    {ELSE           …;code}     ENDIF

where:

logical-expression

is an expression that evaluates to either {TRUE} or {FALSE}.

Show/hideUsage

Use IF with ENDIF, and optionally with ELSE, for sequences of instructions or directives that are only to be assembled or acted on under a specified condition.

IF...ENDIF conditions can be nested.

Show/hideUsing ELIF

Without using ELIF, you can construct a nested set of conditional instructions like this:

    IF logical-expression
        instructions
    ELSE
        IF logical-expression2
            instructions
        ELSE
            IF logical-expression3
                instructions
            ENDIF
        ENDIF
    ENDIF

A nested structure like this can be nested up to 256 levels deep.

You can write the same structure more simply using ELIF:

    IF logical-expression
        instructions
    ELIF logical-expression2
        instructions
    ELIF logical-expression3
        instructions
    ENDIF

This structure only adds one to the current nesting depth, for the IF...ENDIF pair.

Show/hideExamples

Example 3 assembles the first set of instructions if NEWVERSION is defined, or the alternative set otherwise.

Example 3. Assembly conditional on a variable being defined

        IF :DEF:NEWVERSION
            ; first set of instructions or directives
        ELSE
            ; alternative set of instructions or directives
        ENDIF

Invoking armasm as follows defines NEWVERSION, so the first set of instructions and directives are assembled:

armasm --predefine "NEWVERSION SETL {TRUE}" test.s

Invoking armasm as follows leaves NEWVERSION undefined, so the second set of instructions and directives are assembled:

armasm test.s

Example 4 assembles the first set of instructions if NEWVERSION has the value {TRUE}, or the alternative set otherwise.

Example 4. Assembly conditional on a variable value

        IF NEWVERSION = {TRUE}
            ; first set of instructions or directives
        ELSE
            ; alternative set of instructions or directives
        ENDIF

Invoking armasm as follows causes the first set of instructions and directives to be assembled:

armasm --predefine "NEWVERSION SETL {TRUE}" test.s

Invoking armasm as follows causes the second set of instructions and directives to be assembled:

armasm --predefine "NEWVERSION SETL {FALSE}" test.s

Show/hideSee also

Concepts

Using the Assembler:

Reference
Copyright © 2011-2012 ARM. All rights reserved.ARM DUI 0588B
Non-ConfidentialID062912

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.