Mohit Swami
Mohit Swami

Reputation: 135

Create an exe file in assembly with NASM on 32-bit Windows

I'm making a hello world program in assembly language with NASM on 32-bit Windows 7. My code is:

section .text 
global main ;must be declared for linker (ld) 
main: ;tells linker entry point 
    mov edx,len ;message length 
    mov ecx,msg ;message to write 
    mov ebx,1 ;file descriptor (stdout) 
    mov eax,4 ;system call number (sys_write) 
    int 0x80 ;call kernel 
    mov eax,1 ;system call number (sys_exit) 
    int 0x80 ;call kernel 

section .data 
    msg db 'Hello, world!', 0xa ;our dear string 
    len equ $ - msg ;length of our dear string

I save this program as hello.asm. Next, I created hello.o with:

nasm -f elf hello.asm 

Now I'm trying to create the exe file with this command:

ld -s -o hello hello.o 

But now I receive this error:

ld is not recognized as an internal or external command, operable program or batch

Why am I getting this error, and how can I fix it?

Upvotes: 9

Views: 32995

Answers (3)

Henri Latreille
Henri Latreille

Reputation: 273

Download and install Mingw. Then put nasm in the Mingw bin folder. Create a folder in the bin folder named Hello. In this folder, create a file named main.asm with the following code:

extern _printf
global _main

section .data
msg: db "Hello, world!",10,0

section .text
_main:
    push msg
    call _printf
    add esp,4   
    ret

Open the terminal from inside the folder and compile, first, to object code with nasm:

D:\MinGW\bin\Hello> ..\nasm -fwin32 main.asm

Second, call gcc to link:

D:\MinGW\bin\Hello> ..\gcc main.obj -o main.exe

Finally, test it:

D:\MinGW\bin\Hello> main.exe
Hello, world!

Upvotes: 13

John Burger
John Burger

Reputation: 3672

The OP gave some code that he got from a tutorial, and he assembled it with NASM. When he went to link the output into a Windows executable, he couldn't get it to work.

@Michael Petch noted in the comments on the question (top) that the tutorial source was designed for Linux - the code as given could never work for Windows. He went on to mention that the linker isn't provided by NASM: the OP needed to get it from Microsoft.

Upvotes: 3

0x3h
0x3h

Reputation: 462

It's an old question but i wonder why no one has mentioned the solution with the standard windows link /subsystem:console /entry:_main main.obj

Upvotes: 8

Related Questions