Keil Logo

Conditional assembly

6.14 Conditional assembly

Conditional assembly 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 beginning 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, and therefore example() returns 0:
#define x 1
#define y x+1
#define x 2
int example()
#if y == 2
    return 1;
    return 0;
Conditional assembly is based on variables, and works on each line in turn. Unlike the C preprocessor, the assembler evaluates expressions. 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, and therefore MOV 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
        MOV     r0, #0
Non-ConfidentialPDF file icon PDF versionARM DUI0379H
Copyright © 2007, 2008, 2011, 2012, 2014-2016 ARM. All rights reserved. 
  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.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.