From cppreference.com
asm-declaration gives the ability to embed assembly language source code within a C++ program. This declaration is conditionally-supported and (since C++11)implementation defined, meaning that it may not be present and, even when provided by the implementation, (since C++11)it does not have a fixed meaning.
Syntax
attr(optional)asm (string-literal);(until C++26)attr(optional)asm (balanced-token-seq);(since C++26)attr- (since C++11) any number of
string-literal- same as in
, including raw string literals balanced-token-seq- a sequence of tokens where parentheses, brackets and braces are balanced; any restrictions on the balanced-token-seq and its meaning are implementation-defined Explanation
The balanced-token-seq is typically a string literal that represents a short program written in assembly language, which is executed whenever this declaration is executed. Different C++ compilers have wildly varying rules for asm-declarations, and different conventions for the interaction with the surrounding C++ code.
As other
, this declaration can appear inside a block (a function body or another compound statement), and, as all other declarations, this declaration can also appear outside a block.
Notes
Feature-test macroValueStdFeature
(C++20)Trivial
and
in constexpr functions Keywords
Example
Demonstrates two kinds of inline assembly syntax offered by the GCC/Clang compilers. This program works correctly only on the x86_64 platform under Linux.
Run this code
#include<iostream>extern"C"intfunc(intx);// the definition of func is written in assembly language// raw string literal could be very usefulasm(R"(.globl func .type func, @function func: .cfi_startproc movl %edi, %eax /* x is in RDI, see x86-64 calling convention */ addl $1, %eax ret .cfi_endproc)");intmain(){intn=func(0110);// formerly non-standard inline assembly, made comforming by P2361R6asm("leal (%0,%0,4),%0":"=r"(n):"0"(n));std::cout<<"73*5 = "<<n<<std::endl;// flush is intentional// standard inline assemblyasm("movq $60, %rax\n"// the exit syscall number on Linux"movq $2, %rdi\n"// this program returns 2"syscall");}Output:
73*5 = 365 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 it was required to support all asm declarations made conditionally-supported
C++11 attributes could not be applied to asm declarations allowed References
C++26 standard (ISO/IEC 14882:2026):
9.10 The asm declaration [dcl.asm]
C++23 standard (ISO/IEC 14882:2024):
9.10 The asm declaration [dcl.asm]
C++20 standard (ISO/IEC 14882:2020):
9.10 The asm declaration [dcl.asm]
C++17 standard (ISO/IEC 14882:2017):
10.4 The asm declaration [dcl.asm]
C++14 standard (ISO/IEC 14882:2014):
7.4 The asm declaration [dcl.asm]
C++11 standard (ISO/IEC 14882:2011):
7.4 The asm declaration [dcl.asm]
C++03 standard (ISO/IEC 14882:2003):
7.4 The asm declaration [dcl.asm]
C++98 standard (ISO/IEC 14882:1998):
7.4 The asm declaration [dcl.asm]
See also
External links
1.
2.
— Locklessinc.com 3.
4.
5.
Visual Studio Inline Assembler
6.
7.
Inline assembly for Itanium-based HP-UX
8.
— Wikipedia