Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

asm — assembly declaration

The asm keyword introduces a declaration typically used to pass text to an assembler. Support is conditional and its meaning is implementation-defined, so it is not a portable way to execute processor instructions.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

asm ( string-literal ) ;

asm introduces an assembly declaration, not a function call. For C++23, the basic form is asm ( string-literal ) ;; the semicolon is required. The declaration can appear in a function block or at namespace scope, among other declaration contexts.

This is a conditionally supported construct: an implementation need not support it. Its restrictions and meaning are implementation-defined. Typically, the text is passed to an assembler, but ISO C++ defines neither a portable instruction set nor a mechanism for accessing C++ variables from that text.

Do not treat a compiler's syntax for operands, registers, or clobber lists as universal C++23 syntax. In practice, check the documentation for the specific compiler and target platform. Using asm alone does not establish a portable synchronization barrier.

The example uses asm("") only under the assumption that the implementation accepts an empty assembly fragment with no runtime effect. Under that assumption, the expected output is Reached followed by a newline. This is neither a standard guarantee nor a reported test result.

The supplied source is an evolving working draft, not a frozen C++23 text; it currently shows the more general balanced-token-seq syntax. Do not automatically transfer that difference to C++23 mode. A repository compiler flag does not attest operation in a production runtime.

Examples

#include <iostream>

int main() {
    asm("");
    std::cout << "Reached\n";
}

Result

Reached

The expected output was checked against the code under the assumption described above. This does not mean the program was run.

Sources