C Language Inline assembly gcc Basic asm support


Basic assembly support with gcc has the following syntax:

asm [ volatile ] ( AssemblerInstructions )

where AssemblerInstructions is the direct assembly code for the given processor. The volatile keyword is optional and has no effect as gcc does not optimize code within a basic asm statement. AssemblerInstructions can contain multiple assembly instructions. A basic asm statement is used if you have an asm routine that must exist outside of a C function. The following example is from the GCC manual:

 /* Note that this code will not compile with -masm=intel */
 #define DebugBreak() asm("int $3")

In this example, you could then use DebugBreak() in other places in your code and it will execute the assembly instruction int $3. Note that even though gcc will not modify any code in a basic asm statement, the optimizer may still move consecutive asm statements around. If you have multiple assembly instructions that must occur in a specific order, include them in one asm statement.