How to use inline assembly with register usage?

When inline assembly is used in a program, a register can be modified. This can cause side effects that may affect the program’s behavior. To avoid unexpected behavior, the register should be clobbered in the inline assembly. This tells the compiler that the register may be modified and that there may be side effects to using it. By doing so, the compiler can make the necessary adjustments to prevent any issues.

For example, the following code may have a side effect—​it overwrites the d0 register which may be used before this statement.

Example 1

__asm__("mov %d0, %d1");

To avoid this side effect, the register should be clobbered in the following way:

Example 2

__asm__("mov %%d0, %%d1"::: "d0");

If a sequence of ASM statements containing accesses to the same register is issued, it is safer to use a local variable instead.

Example 3

	unsigned int tmp;
	__asm__ volatile("mov %d0, 0xF0001400 ": "=r" (tmp):);
	__asm__ volatile("mov %d0, 0xF0001400 ": "r" (tmp): "0" (tmp));
	__asm__ volatile("SET.F [%d0], 7, SIZE=32":: "r" (tmp));
The input and output registers should not be listed in the clobber list. This is because the compiler knows that inline assembly uses them - they are specified explicitly as constraints.