# 【C语言常识】mdk:Inline assembler rules for compiler keywords __asm and asm

The following rules apply to the __asm and asm keywords:

• 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
{
}

• 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 the inline assembler 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 assembler. 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 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
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
{