Reputation: 77
I wrote this code:
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World', 10, 0
and tried to run it from cmd. It looks like that:
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
I searched this error in google but nothing was relevant for me. I'm using Windows (10) as you can see. Does someone know how to solve this problem? Thanks.
When I run gcc -v I get this:
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)
Upvotes: 1
Views: 1494
Reputation: 88388
From your gcc -v
output it looks like you are using mingw-32.
You need to get mingw-w64.
Also, functions in 64-bit code for Windows look completely different. Rewrite your code as follows:
; ----------------------------------------------------------------------------------------
; 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
Discussion at the bottom of this page
Upvotes: 4