##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# asm declaration

From cppreference.com
< cpp‎ | language

It can be used to pass information to the back-end of the compiler.

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

### [edit] Syntax

`asm (` string_literal `)` `;` |  |

### [edit] 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 only appear inside a block (a function body or another compound statement).

| 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

### [edit] 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>
    int main()
    {
        int n = 7;
        // extended inline assembly
        asm ("leal (%0,%0,4),%0"
             : "=r" (n)
             : "0" (n));
        std::cout << "7*5 = " << n << '\n';

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

### [edit] External links

- GCC Inline Assembly HOWTO
- IBM XL C/C++ Inline Assembly
- Intel C++ Inline Assembly
- Visual Studio 2012 Inline Assembler
- Sun Studio 12 Asm Statements

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

- Todo with reason