# asm declaration

From cppreference.com
< cpp‎ | language

asm-declaration gives the ability to embed assembly language source code within a C++ program. This declaration is conditionally-supported and implementation defined, meaning that it may not be present and, even when provided by the implementation, it does not have a fixed meaning.

### Syntax

attr(optional) `asm (` string-literal `)` `;` |  |

attr | - | (since C++11) any number of attributes
string-literal | - | a string literal

### Explanation

The string-literal is typically 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.

| This section is incomplete Reason: write a note on GCC extended assembly syntax, since it is now supported by Intel, IBM, Sun (as of v12), etc

### Notes

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

### Examples

Demonstrates two kinds of inline assembly syntax offered by the GCC compiler. This program will only work correctly on x86_64 platform under Linux.

Run this code

    #include <iostream>

    extern "C" int func();
    // 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 $7, %eax
        ret
        .cfi_endproc
    )");

    int main()
    {
        int n = func();
        // extended inline assembly
        asm ("leal (%0,%0,4),%0"
             : "=r" (n)
             : "0" (n));
        std::cout << "7*5 = " << n << std::endl; // flush is intentional

        // standard inline assembly
        asm ("movq $60, %rax\n\t" // the exit syscall number on Linux
             "movq $2,  %rdi\n\t" // this program returns 2
             "syscall");
    }

Output:

    7*5 = 35

### 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

### See also

### External links

1. | GCC Inline Assembly HOWTO
2. | IBM XL C/C++ Inline Assembly
3. | Intel C++ Inline Assembly
4. | Visual Studio Inline Assembler
5. | Sun Studio 12 Asm Statements
6. | Inline assembly for Itanium-based HP-UX

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/asm&oldid=146944"