【发布时间】:2019-06-22 15:42:45
【问题描述】:
我写了这段代码:
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World', 10, 0
并尝试从 cmd 运行它。看起来是这样的:
C:\Users\user\AppData\Local\bin\NASM>nasm helloworld.asm -f win64 -o helloworld.obj
C:\Users\user\AppData\Local\bin\NASM>gcc helloworld.obj -m64 -o helloworld.exe
helloworld.obj: file not recognized: File format not recognized
collect2: ld returned 1 exit status
我在 google 中搜索了这个错误,但没有什么与我相关。如您所见,我正在使用 Windows (10)。有人知道如何解决这个问题吗?谢谢。
当我运行 gcc -v 我得到这个:
Reading specs from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs
Configured with: ../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.5 (mingw-vista special r3)
【问题讨论】:
-
你能显示
gcc -v的输出吗 -
你有两个问题。您使用的 GCC 似乎不理解 windows 目标文件,其次,即使链接到可执行文件,您编写的代码也是针对 32 位代码的。 64 位 Windows 调用约定与 32 位不同。
-
@MichaelPetch 我添加了它
-
这看起来像一个 32 位 MinGW 编译器。如果使用
nasm helloworld.asm -f win32 -o helloworld.obj和gcc helloworld.obj -m32 -o helloworld.exe生成32 位可执行文件会发生什么?对于 64 位代码,您需要 MinGW-w64。您的 GCC 版本也很老,可以追溯到 2000 年代中期。 -
它工作在 32 位,因为它是一个 32 位编译器。推荐下载新版MinGW-w64(-w64可以编译32位和64位代码)。您必须使用 Microsoft 64 位调用约定重写汇编代码(第一个参数数量通过寄存器传递,您需要在 16 字节边界上对齐的 32 字节影子空间)。 MS 文档中讨论了 Windows 的 64 位调用约定:docs.microsoft.com/en-us/cpp/build/…
标签: gcc assembly mingw x86-64 nasm