如何在64位Windows计算机上链接32位Nasm程序集目标代码

时间:2015-11-11 12:38:33

标签: windows assembly nasm x86-64

我从http://www.dreamincode.net/forums/topic/328714-my-program-keeps-crashing/找到了以下代码。

            global  start

            ;~ msvcrt.dll
            extern  _printf
            %define printf _printf

            ;~ kernel32.dll
            extern ExitProcess, GetCommandLineW, LocalFree
            %define GetCommandLine GetCommandLineW

            ;~ shell32.dll
            extern CommandLineToArgvW
            %define CommandLineToArgv CommandLineToArgvW

            SECTION .data
            message     db      'Hello, World', 13, 10, 0
            fmtstr      db      "%s", 0
            fmtstrCL    db      "Arg","%d", " = ", "%S", 13, 10, 0

            section .bss
            pNumArgs    resd    1

            section .text
            start:

                call    GetCommandLine

                push    pNumArgs
                push    eax
                call    CommandLineToArgv
                mov     esi, eax

                mov     ebx, [pNumArgs]
            DisplayArgs:
                dec     ebx
                push    dword[esi + 4 * ebx]
                inc     ebx
                push    ebx
                push    fmtstrCL
                call    printf
                add     esp, 4 * 3
                dec     ebx
                jnz     DisplayArgs

                push    esi
                call    LocalFree   

                push    message                         ; Push address of "Hello, world!" onto the stack
                push    fmtstr                          ; push address of formatter onto the stack
                call    printf                          ; Print the message
                add     esp, 4 * 2                      ; adjust stack pointer

                push    0
                call    ExitProcess

我的目标是通过阅读其他人的代码来学习汇编语言,并最终编写自己的代码。我无法弄清楚如何在我的64位Windows计算机上链接32位汇编程序。

要组装程序,我使用命令:

nasm -f win32 hello32.asm -o hello32.o

要链接我使用的目标文件:

gcc hello32.o -o hello32.exe

发出link命令后,我收到以下错误:

C:/Program Files/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/
gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 arc
hitecture of input file `hello32.o' is incompatible with i386:x86-64 output
hello32.o:hello32.asm:(.text+0x24): undefined reference to `_printf'
hello32.o:hello32.asm:(.text+0x3f): undefined reference to `_printf'
C:/Program Files/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/
gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw3
2.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined refere
nce to `WinMain'
collect2.exe: error: ld returned 1 exit status

我正在使用64位mingw二进制文件,它应该与32位程序兼容。我试过切换到32位mingw二进制文件,我得到了大量未定义的引用错误。我可以使用上面的命令链接简单的骨架文件而没有任何问题。我不知道我做错了什么,我很感激有人可以给我任何指导。

2 个答案:

答案 0 :(得分:1)

i386 architecture of input file `hello32.o' is incompatible with i386:x86-64 output

NASM创建了一个32位目标文件,但您尝试链接64位可执行文件。您可以尝试使用-m32开关创建32位可执行文件,但您已经发现这会导致另一堆错误。我也没有解决方案。

要链接您的可执行文件,请使用 32位MingW 环境。我尝试了MinGW4.6.2 32位,效果很好。 或者,您可以使用Microsoft Visual Studio安装中的链接器(link.exe)。

https://github.com/afester/CodeSamples/tree/master/Asm/nasm_win32显示了一个hello world示例以及一个使用Visual Studio链接器的Makefile。或者,使用MingW32安装中的gcc helloworld.obj -o hello32.exe也可以。

答案 1 :(得分:0)

两个问题:

  1. 您正在使用-f win32选项,但要求*.o扩展名中的目标文件。 .o.obj这两种格式不兼容。但是,当然,您可以自由指定自己的扩展名,因此nasm会乖乖地将您的代码组合成一个包含i386 arc格式.o文件的文件。
  2. 接下来,您要求gcc使用文件hello32.o构建该hello32.exe。实际上,你给了gcc一个arc格式的.o文件,并要求用它构建一个64位的PE格式可执行文件。然后(自然地)gcc抱怨:

    输入文件`hello32.o'的i386架构与i386不兼容:x86-64输出

  3. 这是正确的。

    有两种解决方法:

    1. 与:nasm -fwin32 hello32.asm汇总,然后与gcc -m32 hello32.obj -o hello32.exe
    2. 相关联
    3. 与:nasm -fobj hello32.asm汇总,然后与alink -subsys console -oPE hello32.o相关联。你可以得到from here
    4. 让我知道哪个适合您。

      P.S。我已经概述了我自己面临的问题in this blog,希望有所帮助。

相关问题