C++ 關鍵字: asm
來自 cppreference.com
[編輯] 用法
[編輯] 示例
請注意,雖然此示例在 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!