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
Letter Constraint
c A register suitable for use in an indirect jump
d An address register. This is equivalent to @code{r} unless generating MIPS16 code
ka Registers that can be used as the target of multiply-accumulate instructions
l The @code{lo} register. Use this register to store values that are no bigger than a word
x The concatenated @code{hi} and @code{lo} registers. Use this register to store double-word values
Table 20-2. Integer Constraint Letters Supported by the Compiler
Letter Constraint
I A signed 32-bit constant (for arithmetic instructions)
J Integer zero
K An unsigned 32-bit constant (for logic instructions)
L A signed 32-bit constant in which the lower 32 bits are zero. Such constants can be loaded using @code{lui}
M A constant that cannot be loaded using @code{lui}, @code{addiu} or @code{ori}
N A constant in the range -65535 to -1 (inclusive)
O A signed 15-bit constant
P A constant in the range 1 to 65535 (inclusive)
Table 20-3. General Constraint Letters Supported by the Compiler
Letter Constraint
R An address that can be used in a non-macro load or store.
Table 20-4. Constraint Modifiers Supported by the Compiler
Letter Constraint
= 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
d Second register for operand number n, that is, %dn..
q Fourth register for operand number n, that is, %qn..
t Third register for operand number n, that is, %tn..