4.11.2 In-line Assembly

Assembly instructions can be directly embedded in-line into C code using the statement asm();. In-line 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, for example:

asm("sei");

You can write several instructions in the one string, but you should put each instruction on a new line and use linefeed and tab characters to ensure they are properly formatted in the assembly listing file.

asm ("nop\n\t"
 "nop\n\t"
 "nop\n\t"
 "nop\n\t");

In an extended assembler instruction using asm(), the operands of the instruction are specified using C expressions. The extended syntax, discussed in the following sections, has the general form:

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

For example,

asm("in %0, %1" : "=r" (value) : "I" (_SFR_IO_ADDR(PORTD)) );

The template specifies the instruction mnemonic and optional placeholders for the input and output operands, specified by a percent sign followed by a single digit and which are described in the following section. The compiler replaces these and other tokens in the template that refer to inputs, outputs, and goto labels, then outputs the resulting string to the assembler.