【发布时间】: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