asm

The asm keyword can be used to insert inline assembly, which is needed for a very small set of features such as fiber switching and system calls:

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

An asm expression consists of up to 5 colon-separated sections, and components inside each section are separated by commas. For example:

  1. asm(
  2. # the assembly template string, following the
  3. # syntax for LLVM's integrated assembler
  4. "nop" :
  5. # output operands
  6. "=r"(foo), "=r"(bar) :
  7. # input operands
  8. "r"(1), "r"(baz) :
  9. # names of clobbered registers
  10. "eax", "memory" :
  11. # optional flags, corresponding to the LLVM IR
  12. # sideeffect / alignstack / inteldialect / unwind attributes
  13. "volatile", "alignstack", "intel", "unwind"
  14. )

Only the template string is mandatory, all other sections can be empty or omitted:

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

For more details, refer to the LLVM documentation’s section on inline assembler expressions.