名稱空間
變體
操作

C++ 關鍵字: asm

來自 cppreference.com
< cpp‎ | 關鍵字
 
 
C++ 語言
表示式
替代表示
字面量
布林字面量 - 整數字面量 - 浮點字面量
字元字面量 - 字串字面量 - nullptr (C++11)
使用者定義 (C++11)
工具
屬性 (C++11)
型別
typedef 宣告
類型別名宣告 (C++11)
型別轉換
記憶體分配
類特有的函式屬性
explicit (C++11)
static

特殊成員函式
模板
雜項
 
 

[編輯] 用法

[編輯] 示例

請注意,雖然此示例在 Linux x86_64 平臺上使用 GCC/Clang 執行良好,但在其他情況下並不保證,因為 asm 宣告是有條件支援且(C++11 起) 實現定義的

#include <cstring>
 
int main() noexcept
{
    const char* const c_string = "Hello, world!\n";
    asm
    (R"(
        movq $1, %%rax                 # syscall number for sys_write
        movq $1, %%rdi                 # file descriptor 1 (stdout)
        movq %0, %%rsi                 # pointer to the c‐string
        movq %1, %%rdx                 # length of the c‐string
        syscall                        # invokes an OS system-call handler
    )"
    :                                  // no output operands
    :   "r"(c_string),                 // input: pointer to the c‐string
        "r"(std::strlen(c_string))     // input: size of the c‐string
    :   "%rax", "%rdi", "%rsi", "%rdx" // clobbered registers
    );
}

輸出

Hello, world!