gpt4 book ai didi

windows - 如何在 64 位 Windows 计算机上链接 32 位 Nasm 汇编目标代码

转载 作者:可可西里 更新时间:2023-11-01 10:34:41 25 4
gpt4 key购买 nike

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

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

最佳答案

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 也可以。

关于windows - 如何在 64 位 Windows 计算机上链接 32 位 Nasm 汇编目标代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com