IBM extension

Inline assembly statements

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:

Read syntax diagramSkip visual syntax diagramasm 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:

=
Write-only operand.
+
Read and write operand.
&
An operand may be modified before the instruction is finished using the input operands; a register that is used as input should not be reused here.
b
Use a general register other than zero.
f
Use a floating-point register.
g
Use a general register, memory, or immediate operand.
i
An immediate integer operand.
m
A memory operand supported by the machine.
n
Handle in the same way as i.
o
Handle in the same way as m.
r
Use a general register.
v
Use a vector register.
0, 1, 2, ...66
A matching constraint. Allocate the same register in output as in the corresponding input.
I, J, K, M, N, O, P, G, S, T
Constant values. Fold the expression in the operand and substitute the value into the % specifier.