Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 518
Mohit Swami Avatar asked May 24 '16 07:05

Mohit Swami


People also ask

How do I use NASM on Windows?

Extract and install nasm into the codeblocks folder, e.g., C:\Program Files\CodeBlocks\MinGW\bin. Check whether the installation is working or not by the source code below for a test run. This is a Win32 console program that writes "Hello, World" on one line and; then exits. It needs to be linked with a C library.

How do I make an ASM file in Windows?

To create a new ASM file press: File → New → File and type in the new file name (ex0. asm in this case) and then press the + inside the example0 directory or select the example0 directory by clicking on it and then press Finish. Note: Make sure to write the . asm extension following your file name.

Are EXE files written in assembly?

A standard Windows . EXE file contains mostly x86 or x86-64 assembly, but it also includes a header. It would be possible to disassemble the assembly within that file into machine code.


2 Answers

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!
like image 182
Henri Latreille Avatar answered Sep 24 '22 18:09

Henri Latreille


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

like image 30
0x3h Avatar answered Sep 23 '22 18:09

0x3h