> I would expect that this function below would be naked and just does a add 
> and jmp call

Unless you compile with optimizations on, all C function push the stack base 
pointer on the stack on function entry (ebp), then save the stack pointer (esp) 
in ebp then do the function. Compile with optimizations, in particular 
`-fomit-stack-pointer` if you want to avoid the extra save/restore.

> Nim identifiers in asm

The ASM statement doesn't support string interpolation, you need to build the 
string via a macro or use GCC syntax

Macro 
    
    
    import macros, strutils
    
    macro myASMStmt(myAddr: static int): untyped =
      result = nnkAsmStmt.newTree(
        newEmptyNode(),
        newLit "jmp 0x" & toHex(myAddr)
      )
      echo result.toStrLit
    
    const myaddr = 0x123456
    
    proc foo() =
      myASMStmt(myaddr)
    
    
    Run

GCC, use the immediate syntax
    
    
    proc bar() =
      asm """
        jmp %[offset]
        :
        : [offset] "i" (`myaddr`)
        :
      """
    
    
    Run

If you want concrete standalone example of Nim ASM:

Experiments on bigint addition:

  * 
[https://github.com/mratsim/finite-fields/blob/ab033bb5/add_carry.nim#L73-L113](https://github.com/mratsim/finite-fields/blob/ab033bb5/add_carry.nim#L73-L113)



A macro assembler to ease usage of assembly in Nim (living in a branch for now):

  * 
[https://github.com/mratsim/constantine/blob/5b538d4e/constantine/primitives/macro_assembler_x86.nim](https://github.com/mratsim/constantine/blob/5b538d4e/constantine/primitives/macro_assembler_x86.nim)



Concrete usages of the macro assembler (note like Intel syntax it's (dst, 
operands) order:

  * 
[https://github.com/mratsim/constantine/blob/5b538d4e/constantine/arithmetic/finite_fields_asm_x86.nim](https://github.com/mratsim/constantine/blob/5b538d4e/constantine/arithmetic/finite_fields_asm_x86.nim)


Reply via email to