Lines 0 to 3 are the assembly code, the logic of your function. Each line of assembly code ends in ;.

Line 4 contains the output variables and line 5 are the input variables. In quotes you can specify constraint for the variables. = indicate that the variables will be written to. r tells compiler that we want to place the value to a register. In ( ) we insert the variable names that will be mapped.

Variables are mapped to %? variables in the template in the order listed and ? is replaced by a number between 0 and 9. If input and output refer to the same variable, then instead of specifying r for constraint, you will list the number that the output variable is mapped to.

Line 6 indicate registers that will be overwritten, so that compiler can take steps to preserve values before entering inline assembly code.

Notes and Restrictions

If temporary variable is required, you need to map it by listing it in the output variables line.

It seems that long blocks of inline assembly code runs slower than smaller blocks of the same code. You should experiment and confirm with the compiler you are using.

Use debugger or compiler flags to generate disassembly so that you can check the results.

If you are getting compiler warnings about AT registers being used, it's probably because you typed $1 instead of %1 in your assembly code, and it is clobbering up a reserved MIPS register used by assembler.

You can specify assembler directives to enforce certain behavior. For example, .set noreorder to make sure code is executed exactly in the order you specify.

If you want to use instructions that is not supported by the compiler, you will need to create a macro (use .macro and .endm directives), and within the macro use .word to map out the opcode. This may not work with inline assembly, so check your results.

Atomic operations can be achieved via special assembly instructions. For ARM, use swp or ldrex/strex; for MIPS, use ll/sc.

This material may contain (biased) opinions,
inappropriate materials for numerous individuals,
work of other authors from the internet,
links that refer to other web documents and resources, or
origial work that cannot be use for personal or commercial purposes.
Please respect the work of original authors.