MinGW 32的64位NASM代码链接错误

时间:2019-06-22 15:42:45

标签: gcc assembly mingw x86-64 nasm

我写了这段代码:

    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)

1 个答案:

答案 0 :(得分:2)

从您的gcc -v输出来看,您似乎正在使用mingw-32。

您需要获取mingw-w64

此外,Windows的64位代码中的功能看起来完全不同。重写您的代码,如下所示:

; ----------------------------------------------------------------------------------------
; This is a Win64 console program that writes "Hello" on one line and then exits.  It
; uses puts from the C library.  To assemble and run:
;
;     nasm -fwin64 hello.asm && gcc hello.obj && a
; ----------------------------------------------------------------------------------------

        global  main
        extern  puts
        section .text
main:
        sub     rsp, 28h                        ; Reserve the shadow space and align stack
        mov     rcx, message                    ; First argument is address of message
        call    puts                            ; puts(message)
        add     rsp, 28h                        ; Remove shadow space
        ret
message:
        db      'Hello', 0                      ; C strings need a zero byte at the end

this page底部的讨论