Link assembly NASM code to GCC

后端 未结 1 1136
闹比i
闹比i 2021-01-27 05:02

I have a trouble with a compiling assembly code (nasm).

On Linux (elf32) it not fails after compilation using g++, but when I tried to build it with i686-w64-mingw32-g++

相关标签:
1条回答
  • 2021-01-27 05:25

    On Windows the GCC compiler expects a leading underscore in external symbols. So change all wct in the asm file to _wct.

    If you want to test the program in Windows and in Linux you can "globalize" two consecutive labels: wct and _wct:

    ...
    global wct
    global _wct
    ...
    wct:
    _wct:
    ...
    

    Linux gets the wct without underscore and Windows gets it with it.

    BTW: The assembly procedure is a C function and has to follow the CDECL calling convention. The function can freely change the registers EAX, ECX, and EDX (caller saved). The other registers (EBX,ESI,EDI,EBP) have to be returned unchanged. If the function needs to use them, it has to save and restore them (callee saved):

    wct:
    _wct:
    
     push esi                ; sp+= 4
     push edi                ; sp+= 4
     push ebx                ; sp+= 4
                            ; ======
                            ; sp+= 12
     mov esi, [esp+16]
     mov edi, esi
     mov ecx, [esp+20]
    
     ...
    
     pop ebx
     pop edi
     pop esi
     ret
    
    0 讨论(0)
提交回复
热议问题