Under extended language levels, the compiler provides full support for embedded assembly code fragments among C and C++ source statements. This extension has been implemented for use in general system programming code, and in the operating system kernel and device drivers, which were originally developed with GNU C.
The keyword asm stands for assembly code. When strict language levels are used in compilation, the C compiler recognizes and ignores the keyword asm in a declaration. The C++ compiler always recognizes the keyword.
asm statement syntax — statement in local scope >>-+-asm-----+--+----------+------------------------------------> +-__asm---+ '-volatile-' '-__asm__-' >--(--code_format_string--+------------------------------------------------------+--)->< '-:--+--------+--+-----------------------------------+-' '-output-' '-:--+-------+--+-----------------+-' '-input-' '-:--+----------+-' '-clobbers-' input .-,--------------------------------------------. V | |----+----------+--constraint--(--C_expression--)-+-------------| '-modifier-' output .-,----------------------------------------. V | |----modifier--constraint--(--C_expression--)-+-----------------|
asm statement syntax — statement in global scope >>-+-asm-----+--(--code_format_string--)----------------------->< +-__asm---+ '-__asm__-'
The qualifier volatile instructs the compiler that the assembler instructions may update memory not listed in output, input, or clobbers.
The code_format_string is the source text of the asm instructions and is a string literal similar to a printf format specifier. Depending on the operands taken by the assembly instruction, the string contains zero or more modifiers, each of which corresponds to an input or output operand, separated by commas.
The input consists of zero, one or more input operands, separated by commas. Each operand consists of a constraint(C_expression) pair.
The output consists of zero, one or more output operands, separated by commas. Each operand consists of a constraint(C_expression) pair. The output operand must be constrained by the = or + modifier (described below), and, optionally, by an additional % or & modifier.
The C_expression is a C or C++ expression whose value is used as the operand for the asm instruction. Output operands must be modifiable lvalues. The C_expression must be consistent with the constraint specified on it. For example, if i is specified, the operand must be an integer constant number.