The keyword asm stands for assembly code. When compiled
under strict language levels, the compiler recognizes and ignores the keyword
asm in a declaration.
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 kernel and device drivers, which were originally developed with
GNU C.
The syntax is as follows:
>>-+-asm-----+--+----------+------------------------------------>
+-__asm---+ '-volatile-'
'-__asm__-'
.-:--------------.
V |
>--(----code_format_string----+------------+-+----)------------><
+-| output |-+
+-| input |--+
'-clobbers---'
input:
.-,------------------------------.
V |
|----constraint--(--C_expression--)-+---------------------------|
output:
.-,------------------------------.
V |
|----constraint--(--C_expression--)-+---------------------------|
where
- volatile
- Instructs the compiler that the assembler instructions may update memory
not listed in output, input, or
clobbers.
- code_format_string
- Is the source text of the asm instructions and is a string
literal similar to a printf format specifier.
- input
- Is a comma-separated list of input operands.
- output
- Is a comma-separated list of output operands.
- clobbers
- Is a comma-separated list of register names enclosed in double
quotes. These are registers that can be updated by the asm
instruction.
- constraint
- Is a string literal specifying the constraints for the operand, one
character per constraint.
- 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.
Restrictions
The assembler instructions must be self-contained within an
asm statement. The asm statement can only be used to
generate instructions. All connections to the rest of the program must
be established through the output and input operand
list. In particular:
- Branching to a label in another asm statement is not
supported.
- Referencing an external symbol directly, without going through the operand
list, is not supported.
- Pseudo-operators and directives, such as instructions with the suffix
.section, .text, or
.data, are not supported.
- The total number of instructions in one asm statement cannot
exceed 63. The instruction count must also include the instructions
generated by the compiler to handle the operands in the operand list.
Related References
