Assembly that is embedded within a source in another, higher language, such as x86 assembly embedded in C or C++.
Inline assembly is used in higher level language to provide access to features not exposed by intrinsics. c and c++ are the most common "host" languages that allow inline asm.
Don't use inline asm without being aware of the potential performance downsides, as well as the obvious maintainability / portability downsides. The compiler can't understand inline asm for constant-propagation and other optimizations. If you can get the compiler to generate equivalent asm from normal source code without inline asm, that is almost always preferable.
Resources
- https://gcc.gnu.org/wiki/DontUseInlineAsm. Step zero in using inline asm is to try to avoid needing it in the first place.
- Visual C++ inline assembly
- GCC inline assembly HOWTO, from 2003 but still correct and useful.
- GNU C inline asm official docs
- Another more recent GNU C tutorial
- A nice step-by-step explanation for beginners of using the right constraints for input and output operands for (
idiv
). The other answer on that question also explains when and why to use early-clobber&
in output operands. - Basics of using "r" and "=r" inputs/outputs, or a "+r" in/out operand. Some background on how GNU C inline asm syntax requires the operands to describe the template to the compiler.
- Why can't local variable be used in GNU C basic inline asm statements? - local vars
- Using dummy
"m"
or"=m"
operands to tell the compiler which memory you read/write via pointers you asked for in"r"
operands, instead of using a"memory"
clobber:- How can I indicate that the memory *pointed* to by an inline ASM argument may be used?
- a simple strlen (telling the compiler a whole block of memory is referenced without an explicit length, using a dummy
struct
with flexible array member). - Looping over arrays with inline assembly (merits of
"m"
vs."r"
constraints)
- GNU C inline asm link collection at the bottom of this answer (guides / advice / info)
- MSVC inline asm vs. GNU C inline asm for wrapping a single instruction
- Referencing memory operands in .intel_syntax GNU C inline assembly - don't use
.intel_syntax
inside an asm statement; use compiler options or dialect alternatives.
The x86 tag wiki has tons of good stuff for that architecture, and the assembly tag wiki also has a few links.
Making a function-call from inline asm: avoid if possible
- Direct C function call using GCC's inline assembly - avoid calling from inline asm if you can (because it's hard to make it safe), but
%P0
can expand to a function name. - Calling printf in extended inline ASM - hopefully-complete clobber list of call-clobbered registers for x86-64, and safely handling the red-zone, if you really insist on making a
call
from inline asm.
Non-x86
GNU C inline asm works the same way for non-x86 architectures (you still use input and output operands with constraints to get data into / out of asm statements).
There are differences: many targets don't have a constraint syntax for requesting a specific register (e.g. x86's "a"
for eax/rax).