跳至內容

asm

asm 關鍵字可用於插入內聯組語,這對於一小部分功能(例如 fiber 切換和系統呼叫)是必要的。

# x86-64 targets only
dst = 0
asm("mov $$1234, $0" : "=r"(dst))
dst # => 1234

一個 asm 表達式最多包含 5 個以冒號分隔的區段,且每個區段內的元件以逗號分隔。例如:

asm(
  # the assembly template string, following the
  # syntax for LLVM's integrated assembler
  "nop" :
  # output operands
  "=r"(foo), "=r"(bar) :
  # input operands
  "r"(1), "r"(baz) :
  # names of clobbered registers
  "eax", "memory" :
  # optional flags, corresponding to the LLVM IR
  # sideeffect / alignstack / inteldialect / unwind attributes
  "volatile", "alignstack", "intel", "unwind"
)

只有模板字串是強制性的,所有其他區段都可以是空的或省略的。

asm("nop")
asm("nop" :: "b"(1), "c"(2)) # output operands are empty

更多細節請參考 LLVM 文件中關於內聯組語表達式的章節