1,885
edits
Changes
no edit summary
In open source software (especially in a Linux context), this is most commonly done using gcc, but inline assembler is also supported by llvm/clang, the Intel C compilers, Microsoft Visual Studio, and various other tools. Here we're going to focus on gcc.
== Basic Syntax ==
Inline assembler is included in a GCC source file in one of these two forms:
On i386 only, specific registers may be selected by using <code>a</code>, <code>b</code>, <code>c</code>, or <code>d</code> in place of <code>r</code> as a register constraint. Using <code>a</code>, for example, will select the rax/eax register.
=== Clobber ===
If your code will clobber (overwrite) registers or memory regions, these must be listed after the third colon within the asm parenthesis. For example:
asm("..."
: "=r"(out)
: "r"(in)
: "rax", "rbx", "rsi" // values in rax, rbx, rsi will be clobbered
);
If memory is altered by the asm code, the string "memory" should be added to the clobber list. In most cases, the <code>volatile</code> keyword should also be addded.