You copied the Doc URL to your clipboard.

Conditional assembly

The conditional assembly mechanism of the assembler works differently from conditional compilation using the C preprocessor.

The C preprocessor performs textual transformations of macro identifiers into their definitions. Transformation occurs at the point at which the identifier is used. The C preprocessor is controlled by the following:

  • Preprocessor directives embedded in the C source file, for example, #define.

  • Compiler command-line options, for example -D and -U. These have the same effect as a #define or #undef directive at the top of each source file.

For example, in the following code, the C preprocessor replaces y with x+1 at the point at which y is used. This becomes 2+1 because the value of x at that point is 2. Therefore, example() returns 0.

#define x 1
#define y x+1
#define x 2

int example()
{
#if y == 2
    return 1;
#else
    return 0;
#endif
}

Conditional assembly is based on variables, and works on each line in turn. Unlike the C preprocessor, the assembler evaluates expressions. It does this for a directive at the point at which the directive occurs. Conditional assembly is controlled by the following:

  • Assembler directives that declare and set the value of variables, for example GBLx, LCLx and SETx.

  • Assembler directives that control the flow of the assembly, for example WHILE, IF and ELSE.

  • Assembler directives that define macros, for example MACRO.

  • The assembler command-line option --predefine, which pre-executes a GBLx and SETx directive.

For example, in the following code, the assembler evaluates x+1 at the point at which the SETA directive occurs. Because the value of x at that point is 1, y is set to 2. Therefore, the MOV instruction sets r0 to 1.

    GBLA    x
    GBLA    y
x   SETA    1
y   SETA    x+1
x   SETA    2

    AREA    example, CODE
    IF y == 2
        MOV     r0, #1
    ELSE
        MOV     r0, #0
    ENDIF

    END
Was this page helpful? Yes No