Assembler statement

The direct embedding of assembler code into Nim code is supported by the unsafe asm statement. Identifiers in the assembler code that refer to Nim identifiers shall be enclosed in a special character which can be specified in the statement’s pragmas. The default special character is ‘`‘:

  1. {.push stackTrace:off.}
  2. proc addInt(a, b: int): int =
  3. # a in eax, and b in edx
  4. asm """
  5. mov eax, `a`
  6. add eax, `b`
  7. jno theEnd
  8. call `raiseOverflow`
  9. theEnd:
  10. """
  11. {.pop.}

If the GNU assembler is used, quotes and newlines are inserted automatically:

  1. proc addInt(a, b: int): int =
  2. asm """
  3. addl %%ecx, %%eax
  4. jno 1
  5. call `raiseOverflow`
  6. 1:
  7. :"=a"(`result`)
  8. :"a"(`a`), "c"(`b`)
  9. """

Instead of:

  1. proc addInt(a, b: int): int =
  2. asm """
  3. "addl %%ecx, %%eax\n"
  4. "jno 1\n"
  5. "call `raiseOverflow`\n"
  6. "1: \n"
  7. :"=a"(`result`)
  8. :"a"(`a`), "c"(`b`)
  9. """