【问题标题】:How to link 32-bit Nasm assembly object code on a 64-bit windows computer如何在 64 位 Windows 计算机上链接 32 位 Nasm 汇编目标代码
【发布时间】:2016-02-12 13:18:10
【问题描述】:

我从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

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

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

我正在使用应该与制作 32 位程序兼容的 64 位 mingw 二进制文件。我尝试切换到 32 位 mingw 二进制文件,但我得到了大量未定义的参考错误。我可以使用上述命令毫无问题地链接简单的骨架文件。我不知道自己做错了什么,如果有人能给我任何指导,我将不胜感激。

【问题讨论】:

    标签: windows assembly nasm x86-64


    【解决方案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 也可以。

    【讨论】:

    • 尝试将程序转换为 64 位会更好吗?我试过了,但我只得到一个错误,但我也不知道如何纠正它。
    • 你可以试一试,但指令集、寄存器和调用约定有部分不同。例如,不支持 push eax - 您需要推送整个 64 位寄存器 push rax。我将从运行 32 位应用程序开始。
    • 好的,谢谢。我会尝试让 32 位程序运行。
    【解决方案2】:

    两个问题:

    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 输出不兼容

    这是正确的。

    有两种方法可以解决此问题:

    1. nasm -fwin32 hello32.asm 组装,然后与gcc -m32 hello32.obj -o hello32.exe 链接
    2. 与:nasm -fobj hello32.asm 组装,然后与alink -subsys console -oPE hello32.o 链接。你可以得到一个链接from here

    让我知道哪个对你有用。

    附:我已经概述了我自己遇到的问题in this blog,希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多