Under extended language levels, the compiler provides partial support for embedded assembly code fragments among C and C++ source statements. This extension has been implemented for use in general system programming code, 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 compiler recognizes and ignores the keyword asm in a declaration.
The syntax is as follows:
asm statement syntax >>-+-asm-----+--+----------+------------------------------------> +-__asm---+ '-volatile-' '-__asm__-' .-:------------. V | >--(----code_format_string----+----------+-+----)-------------->< +-output---+ +-input----+ '-clobbers-' input: .-,------------------------------. V | |----constraint--(--C_expression--)-+---------------------------| output: .-,------------------------------. V | |----constraint--(--C_expression--)-+---------------------------|
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. The string consists of a comma-separated list of % specifiers, each of which corresponds to an input or output operand. The % specifier can take either of the following forms:
The input is a comma-separated list of input operands.
The output is a comma-separated list of output operands.
If pointer expressions are used in input or output, the assembly instructions should honor the ANSI aliasing rule (see Type-based aliasing for more information). This means that indirect addressing using values in pointer expression operands should be consistent with the pointer types; otherwise, you must disable the -qalias=ansi option during compilation.
clobbers is a comma-separated list of register names enclosed in double quotes. These are registers that can be updated by the asm instruction.
The constraint is a string literal specifying the constraints for the operand, one character per constraint.
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 following constraints are supported: