gcc 基本 asm 支持

使用 gcc 的基本程序集支持具有以下语法:

asm [ volatile ] ( AssemblerInstructions )

其中 AssemblerInstructions 是给定处理器的直接汇编代码。volatile 关键字是可选的,并且没有效果,因为 gcc 不会优化基本 asm 语句中的代码。AssemblerInstructions 可以包含多个汇编指令。如果你有一个必须存在于 C 函数之外的 asm 例程,则使用基本的 asm 语句。以下示例来自 GCC 手册:

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

在这个例子中,你可以在代码中的其他位置使用 DebugBreak(),它将执行汇编指令 int $3。请注意,即使 gcc 不会修改基本 asm 语句中的任何代码,优化器仍然可以移动连续的 asm 语句。如果你有多个必须按特定顺序发生的汇编指令,请将它们包含在一个 asm 语句中。