6.1.1. Inline assembler syntax

The ARM compiler supports an extended inline assembler syntax, introduced by the asm keyword (C++), or the __asm keyword (C and C++). The syntax for these keywords is described in the following sections:

You can use an asm or __asm statement anywhere a statement is expected.

The inline assembler supports the majority of the ARM instruction set, including generic coprocessor instructions. The BX, BLX, and BXJ instructions are not supported.

The inline assembler supports a subset of ARMv6 instructions. This subset comprises the ARMv6 media instructions only. See Restrictions on inline assembly operations for more information.

See the chapter on mixing C, C++, and assembly language in the Developer Guide for more information on inline assembly language in C and C++ sources.

Inline assembly with the __asm keyword

The inline assembler is invoked with the assembler specifier, and is followed by a list of assembler instructions inside braces. You can specify inline assembler code using the following formats:

  • On a single line, for example:

    __asm("instruction[;instruction]"); // Must be a single string
    __asm{instruction[;instruction]}
    

    You cannot include comments.

  • On multiple lines, for example:

    __asm
    {
        ...
        instruction
        ...
    }
    

    You can use C or C++ comments anywhere in an inline assembly language block.

Also, see Rules for using __asm and asm.

Inline assembly with the asm keyword

When compiling C++, the ARM compiler supports the asm syntax proposed in the ISO C++ Standard. You can specify inline assembler code using the following formats:

  • On a single line, for example:

    asm("instruction[;instruction]"); // Must be a single string
    asm{instruction[;instruction]}
    

    You cannot include comments.

  • On multiple lines, for example:

    asm
    {
        ...
        instruction
        ...
    }
    

    You can use C or C++ comments anywhere in an inline assembly language block.

Rules for using __asm and asm

Follow these rules when using the __asm and asm keywords:

  • If you include multiple instructions on the same line, you must separate them with a semicolon (;). If you use double quotes, you must enclose all the instructions within a single set of double quotes (").

  • If an instruction requires more than one line, you must specify the line continuation with the backslash character (\).

  • For the multiple line format, you can use C or C++ comments anywhere in the inline assembly language block. However, you cannot embed comments in a line that contains multiple instructions.

  • The comma (,) is used as a separator in assembly language, so C expressions with the comma operator must be enclosed in parentheses to distinguish them:

    __asm
    {
        ADD x, y, (f(), z)
    }
    
  • An asm statement must be inside a C++ function. An asm statement can be used anywhere a C++ statement is expected.

  • Register names in the inline assembler are treated as C or C++ variables. They do not necessarily relate to the physical register of the same name (see Virtual registers). If you do not declare the register as a C or C++ variable, then the compiler generates a warning.

  • Do not save and restore registers in inline assembler. The compiler does this for you. Also, the inline assembler does not provide direct access to the physical registers. See Virtual registers for more information.

    If registers other than CPSR and SPSR are read without being written to, an error message is issued. For example:

    int f(int x)
    {
        __asm
        {
            STMFD sp!, {r0}    // save r0 - illegal: read before write
            ADD r0, x, 1
            EOR x, r0, x
            LDMFD sp!, {r0}    // restore r0 - not needed.
        }
        return x;
    }
    

    The function must be written as:

    int f(int x)
    {
        int r0;
        __asm
        {
            ADD r0, x, 1
            EOR x, r0, x
        }
        return x;
    }
    

See Restrictions on inline assembly operations for more information.

Copyright © 2002-2007 ARM Limited. All rights reserved.ARM DUI 0205H
Non-Confidential