20.2 Using Inline Assembly Language

Within a C/C++ function, the asm statement may be used to insert a line of assembly language code into the assembly language that the compiler generates. Inline assembly has two forms: simple and extended.

In the simple form, the assembler instruction is written using the syntax:

asm ("instruction");

where instruction is a valid assembly-language construct. If you are writing inline assembly in ANSI C programs, write __asm__ instead of asm.

Note: Only a single string can be passed to the simple form of inline assembly.

In an extended assembler instruction using asm, the operands of the instruction are specified using C/C++ expressions. The extended syntax is:

asm("template" [ : [ "constraint"(output-operand) [ , ... ] ]
          [ : [ "constraint"(input-operand) [ , ... ] ]
             [ "clobber" [ , ... ] ]
          ]
        ]);

You must specify an assembler instruction template, plus an operand constraint string for each operand. The template specifies the instruction mnemonic, and optionally placeholders for the operands. The constraint strings specify operand constraints, for example, that an operand must be in a register (the usual case), or that an operand must be an immediate value.

Constraint letters and modifiers supported by the compiler are listed in the following tables.

Table 20-1. Register Constraint Letters Supported by the Compiler
LetterConstraint

r

Any general register

lIn Thumb State, the core registers r0-r7. In Arm state, this is an alias for the 'r' constraint.
hIn Thumb state, the core registers r8-r15.
tIn Arm/Thumb-2 state, the VFP floating-point registers s0-s31.
wIn Arm/Thumb-2 state, the VFP floating-point registers d0-d15, or d0-d31 for VFPv3.
xIn Arm/Thumb-2 state, the VFP floating-point registers d0-d7.
TsIf -mrestrict-it is specified (for Arm-v8), the core registers r0-r7. Otherwise, GENERAL_REGS (r0-r12 and r14).
Table 20-2. Integer Constraint Letters Supported by the Compiler
LetterConstraint
GIn Arm/Thumb-2 state, the floating-point constant 0.
IIn Arm /Thumb-2 state, a constant that can be used as an immediate value in a Data Processing instruction (that is, an integer in the range 0 to 255 rotated by a multiple of 2).

In Thumb-1 state, a constant in the range 0..255.

jIn Arm /Thumb-2 state, a constant suitable for a MOVW instruction.
JIn Arm /Thumb-2 state, a constant in the range -4095..4095.

In Thumb-1 state, a constant in the range -255..-1.

KIn Arm /Thumb-2 state, a constant that satisfies the 'I' constraint if inverted (one's complement).

In Thumb-1 state, a constant that satisfies the 'I' constraint multiplied by any power of 2.

LIn Arm /Thumb-2 state, a constant that satisfies 'I' constraint if negated (two's complement).

In Thumb-1 state, a constant in the range -7..7.

MIn Thumb-1 state, a constant that is a multiple of 4 in the range 0..1020.
NIn Thumb-1 state, a constant in the range 0-31.
OIn Thumb-1 state, a constant that is a multiple of 4 in the range -508..508.
PfMemory models except relaxed, consume or release ones.
Table 20-3. Constraint Modifiers Supported by the Compiler
LetterConstraint
=Means that this operand is write-only for this instruction: the previous value is discarded and replaced by output data.
+Means that this operand is both read and written by the instruction
&Means that this operand is an earlyclobber operand, which is modified before the instruction is finished using the input operands. Therefore, this operand may not lie in a register that is used as an input operand or as part of any memory address