Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The asm declaration (inline assembly)

Reference for C++ asm: the standard asm-declaration, GCC extended inline assembly with operands, constraints and clobbers, and when to avoid it.

asm embeds a fragment of assembly language in C++ source. The C++ standard only says that asm("text"); is conditionally supported and that its meaning is implementation-defined, so everything useful about it is compiler-specific. On GCC and Clang the extended asm form lets the assembly read and write C++ variables through operands, and tell the compiler which registers and memory it disturbs.

Inline assembly is a last resort: it is not portable between CPU architectures, it blocks many optimizations, and a wrong constraint silently produces wrong code. Reach for compiler intrinsics (<immintrin.h>, __builtin_popcount) or standard functions (std::popcount) first. Use asm for instructions that have no other way in: reading a special register, a memory barrier, or a one-off CPU feature.

C++
int a = 40, b = 2;
asm("addl %1, %0" : "+r"(a) : "r"(b));   // x86: a += b

Try it

The examples are guarded with #if defined(__x86_64__) so that they still compile on other CPUs. Our runner is x86-64.

C++ Extended asm with input and output operands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Compiler barrier and volatile asm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
asm("template");                                   // basic (standard, implementation-defined)
asm [volatile] ("template"
    : output-operands                              // "=r"(var), "+r"(var), "=m"(var)
    : input-operands                               // "r"(expr), "i"(constant), "m"(var)
    : clobbers);                                   // "memory", "cc", "rax", ...
asm goto ("template" : : inputs : clobbers : labels);   // jump to C++ labels
Part Description
Template The assembly text. %0, %1, ... name operands in the order outputs then inputs (or %[name] with [name] "r"(x) operands). Write %% for a literal % register prefix.
Output constraint "=r" write-only in a register, "+r" read and write, "=m" memory. "=&r" early-clobber: the output is written before inputs are read.
Input constraint "r" register, "m" memory, "i" integer constant, "0" same location as operand 0.
Clobbers Registers the code changes beyond its outputs, plus "cc" (condition flags) and "memory" (arbitrary memory writes).
volatile Do not delete or move the statement even if outputs look unused. Basic asm is always volatile.

Errors / undefined behavior: forgetting a clobber or using the wrong constraint is undefined behavior in practice: the compiler may keep a value in a register you overwrote. The assembler, not the C++ compiler, reports syntax errors in the template, often with confusing messages.

Usage notes

  • GCC/Clang use AT&T syntax by default (mov source, destination); MSVC does not support inline asm on x64 at all, and uses intrinsics or separate .asm files.
  • Prefer __builtin_* functions and <bit>, <atomic>, <immintrin.h>. std::popcount(x) does what the popcnt example does, portably.
  • Never assume a register. Let the compiler pick with constraints. Hard-coded registers need clobber entries.
  • Memory barriers: std::atomic_thread_fence is the portable way. A bare asm volatile("" ::: "memory") only stops compiler reordering, not CPU reordering.
  • constexpr functions cannot contain asm.
  • Debugging. Compile with -S (or use a compiler explorer) to see the assembly that your template ends up in.
  • Not portable by design. Guard every block with architecture macros such as __x86_64__ or __aarch64__ and give a portable fallback.