如何链接64位Windows计算机上的32位Nasm汇编对象代码

我从http://www.dreamincode.net/forums/topic/328714-my-program-keeps-crashing/find了下面的代码。

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 

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

 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与使用Visual Studio链接器的Makefile一起显示了hello世界示例。 或者,使用gcc helloworld.obj -o hello32.exe从MingW32安装也可以。

两个问题:

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

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

哪个是对的。

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

  1. 使用: nasm -fwin32 hello32.asm汇编,然后使用gcc -m32 hello32.obj -o hello32.exe链接gcc -m32 hello32.obj -o hello32.exe
  2. 使用以下nasm -fobj hello32.asm组装: nasm -fobj hello32.asm ,然后使用链接alink -subsys console -oPE hello32.o 。 你可以从这里起床。

让我知道哪些工作适合你。

PS我已经概述了我在这个博客中遇到的问题,希望有所帮助。