如何在 64 位 windows 计算机上 link 32 位 Nasm 汇编目标代码

How to link 32-bit Nasm assembly object code on a 64-bit windows computer

我从 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 计算机上 link 32 位汇编程序。

到assemble程序我使用命令:

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

到link我使用的目标文件:

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 二进制文件,但出现大量未定义的引用错误。使用上述命令,我可以 link 简单的框架文件而不会出现任何问题。我不知道我做错了什么,如果有人能给我任何指导,我将不胜感激。

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

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

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

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

两期:

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

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

这是正确的。

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

  1. Assemble 与:nasm -fwin32 hello32.asm 然后,link 与 gcc -m32 hello32.obj -o hello32.exe
  2. Assemble 与:nasm -fobj hello32.asm 然后 link 与 alink -subsys console -oPE hello32.o。可以获得一个linkfrom here

让我知道哪个对你有用。

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