7.6 Inline assembler rules for compiler keywords __asm and asm

There are a number of rule that apply to the __asm and asm keywords.

These rules are as follows:

  • Multiple instructions on the same line must be separated with a semicolon (;).

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

  • For the multiple line format, C and C++ comments are permitted anywhere in the inline assembly language block. However, comments cannot be embedded 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)
    }
    
  • Labels must be followed by a colon, :, like C and C++ labels.

  • An asm statement must be inside a C++ function. An asm statement can be used anywhere a C++ statement is expected.

  • Register names in inline assembly code are treated as C or C++ variables. They do not necessarily relate to the physical register of the same name. If the register is not declared as a C or C++ variable, the compiler generates a warning.

  • Registers must not be saved and restored in inline assembly code. The compiler does this for you. Also, the inline assembler does not provide direct access to the physical registers. However, indirect access is provided through variables that act as virtual registers.

    If registers other than ASPR, 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;
    }
    
Non-ConfidentialPDF file icon PDF versionARM DUI0472M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.