Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/asm"

From cppreference.com
< cpp‎ | language
m (Notes: +todo)
 
Line 6: Line 6:
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|notes={{mark until c++17}}|
+
{{sdsc|notes={{mark until c++26}}|
{{ttb|asm (}} {{spar|string-literal}} {{ttb|)}} {{ttb|;}}
+
}}
+
{{sdsc|notes={{mark life|since=c++17|until=c++26|br=yes}}|
+
 
{{spar optional|attr}} {{ttb|asm (}} {{spar|string-literal}} {{ttb|)}} {{ttb|;}}
 
{{spar optional|attr}} {{ttb|asm (}} {{spar|string-literal}} {{ttb|)}} {{ttb|;}}
 
}}
 
}}
Line 18: Line 15:
  
 
{{par begin}}
 
{{par begin}}
{{par|{{spar|attr}}|any number of {{rlp|attributes}}}}
+
{{par|{{spar|attr}}|{{mark since c++11}} any number of {{rlp|attributes}}}}
 
{{par|{{spar|string-literal}}|same as in {{rlp|string literal}}, including raw string literals}}
 
{{par|{{spar|string-literal}}|same as in {{rlp|string literal}}, including raw string literals}}
 
{{par|{{spar|balanced-token-seq}}|a sequence of tokens where parentheses, brackets and braces are balanced; any restrictions on the {{spar|balanced-token-seq}} and its meaning are implementation-defined}}
 
{{par|{{spar|balanced-token-seq}}|a sequence of tokens where parentheses, brackets and braces are balanced; any restrictions on the {{spar|balanced-token-seq}} and its meaning are implementation-defined}}
Line 29: Line 26:
  
 
{{todo|write a note on GCC extended assembly syntax, since it is now supported by Intel, IBM, Sun (as of v12), etc}}
 
{{todo|write a note on GCC extended assembly syntax, since it is now supported by Intel, IBM, Sun (as of v12), etc}}
{{todo|Requires correction. See: [[Talk:cpp/language/asm#Wrong_Syntax|Talk: Wrong Syntax]].}}
 
  
 
===Notes===
 
===Notes===
Before the relaxation of standard requirements in {{wg21|P2361R6}}, {{spar|balanced-token-seq}} was only allowed to be {{spar|string-literal}}, which did not match the practice of some existing implementations.
 
 
 
{{feature test macro|value=201907L|std=C++20|__cpp_constexpr|Trivial {{rlp|default initialization}} and [[#Syntax|asm-declaration]] in {{tt|constexpr}} functions}}
 
{{feature test macro|value=201907L|std=C++20|__cpp_constexpr|Trivial {{rlp|default initialization}} and [[#Syntax|asm-declaration]] in {{tt|constexpr}} functions}}
 
{{todo|Requires correction. See: [[Talk:cpp/language/asm#Wrong_Syntax|Talk: Wrong Syntax]].}}
 
  
 
===Keywords===
 
===Keywords===

Latest revision as of 05:04, 14 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
Inline assembly
 
 

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.

Contents

[edit] 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 attributes
string-literal - same as in string literal, 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

[edit] 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 block declarations, 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.

[edit] Notes

Feature-test macro Value Std Feature
__cpp_constexpr 201907L (C++20) Trivial default initialization and asm-declaration in constexpr functions

[edit] Keywords

asm

[edit] 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.

#include <iostream>
 
extern "C" int func(int x);
// the definition of func is written in assembly language
// raw string literal could be very useful
asm(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
)");
 
int main()
{
    int n = func(0110);
    // formerly non-standard inline assembly, made comforming by P2361R6
    asm ("leal (%0,%0,4),%0"
         : "=r" (n)
         : "0" (n));
    std::cout << "73*5 = " << n << std::endl; // flush is intentional
 
    // standard inline assembly
    asm ("movq $60, %rax\n" // the exit syscall number on Linux
         "movq $2,  %rdi\n" // this program returns 2
         "syscall");
}

Output:

73*5 = 365

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 195 C++98 it was required to support all asm declarations made conditionally-supported
CWG 2262 C++11 attributes could not be applied to asm declarations allowed

[edit] 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]

[edit] See also

C documentation for Inline assembly

[edit] External links

1.  GCC Inline Assembly HOWTO
2.  GCC Inline ASM — Locklessinc.com
3.  IBM XL C/C++ Inline Assembly
4.  Intel C++ Inline Assembly
5.  Visual Studio Inline Assembler
6.  Sun Studio 12 Asm Statements
7.  Inline assembly for Itanium-based HP-UX
8.  X86 calling conventions — Wikipedia