asm statement has the following syntax:
asm [ volatile ] ( AssemblerInstructions )
asm keyword is a GNU extension.
When writing code that can be compiled with -ansi and the
various -std options, use
__asm__ instead of
asm (see Alternate Keywords).
volatilequalifier has no effect. All basic
asmblocks are implicitly volatile.
You may place multiple assembler instructions together in a single
string, separated by the characters normally used in assembly code for the
system. A combination that works in most places is a newline to break the
line, plus a tab character (written as ‘\n\t’).
Some assemblers allow semicolons as a line separator. However,
note that some assembler dialects use semicolons to start a comment.
asm typically produces smaller, safer, and more
efficient code, and in most cases it is a better solution than basic
asm. However, there are two situations where only basic
can be used:
asmstatements have to be inside a C function, so to write inline assembly language at file scope (“top-level”), outside of C functions, you must use basic
asm. You can use this technique to emit assembler directives, define assembly language macros that can be invoked elsewhere in the file, or write entire functions in assembly language.
nakedattribute also require basic
asm(see Function Attributes).
Safely accessing C data and calling functions from basic
asm is more
complex than it may appear. To access C data, it is better to use extended
Do not expect a sequence of
asm statements to remain perfectly
consecutive after compilation. If certain instructions need to remain
consecutive in the output, put them in a single multi-instruction
statement. Note that GCC's optimizers can move
relative to other code, including across jumps.
asm statements may not perform jumps into other
GCC does not know about these jumps, and therefore cannot take
account of them when deciding how to optimize. Jumps from
asm to C
labels are only supported in extended
Under certain circumstances, GCC may duplicate (or remove duplicates of) your assembly code when optimizing. This can lead to unexpected duplicate symbol errors during compilation if your assembly code defines symbols or labels.
Since GCC does not parse the AssemblerInstructions, it has no visibility of any symbols it references. This may result in GCC discarding those symbols as unreferenced.
The compiler copies the assembler instructions in a basic
verbatim to the assembly language output file, without
processing dialects or any of the ‘%’ operators that are available with
asm. This results in minor differences between basic
asm strings and extended
asm templates. For example, to refer to
registers you might use ‘%eax’ in basic
‘%%eax’ in extended
On targets such as x86 that support multiple assembler dialects,
asm blocks use the assembler dialect specified by the
-masm command-line option (see x86 Options).
asm provides no
mechanism to provide different assembler strings for different dialects.
Here is an example of basic
asm for i386:
/* Note that this code will not compile with -masm=intel */ #define DebugBreak() asm("int $3")