proc addInt(a,b:int):int {.asmNoStackFrame.} =
asm """
mov eax, `a`
add eax, `b`
ret
"""
echo addInt(3,4)
The above results run to 0.,I hope someone can help me. thanks
Look at the assembler code:
@addInt_WYbpHhMOLfd0wzp13vORMw@8 PROC NEAR
mov eax, dword ptr [ebp-4H] ; 0020 _ 8B. 45, FC
add eax, dword ptr [ebp-8H] ; 0023 _ 03. 45, F8
ret ; 0026 _ C3
@addInt_WYbpHhMOLfd0wzp13vORMw@8 ENDP
@NimMainModule@0 PROC NEAR
push ecx ; 0080 _ 51
mov edx, 4 ; 0081 _ BA, 00000004
lea ecx, [edx-1H] ; 0086 _ 8D. 4A, FF
mov dword ptr [esp], 0 ; 0089 _ C7. 04 24, 00000000
call @addInt_WYbpHhMOLfd0wzp13vORMw@8 ; 0090 _ E8, 00000000(rel)
mov ecx, eax ; 0095 _ 8B. C8
call @nimIntToStr@4 ; 0097 _ E8, 00000000(rel)
mov edx, 1 ; 009C _ BA, 00000001
lea ecx, [esp] ; 00A1 _ 8D. 0C 24
mov dword ptr [esp], eax ; 00A4 _ 89. 04 24
call @echoBinSafe@8 ; 00A7 _ E8, 00000000(rel)
pop ecx ; 00AC _ 59
ret ; 00AD _ C3
@NimMainModule@0 ENDP
The proc addInt parameter is treated as __stdcall or __cdecl, the call to addInt is __fastcall.
The following code should work:
proc addInt(a,b:int):int {.asmNoStackFrame.} =
asm """
mov eax, ecx
add eax, edx
ret
"""
echo addInt(3,4)
But I think maybe we can specify calling convention, so the following code may correct:
asm """
mov eax, `a`
"""
Otherwise, we cannot refer the parameter in the assembler code. But it still make sense, because the vcc compiler treat the __declspec(naked) code like this.