Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assemble and link .asm files to a Win32 executable?

I have NASM and Dev-Cpp installed on my system. Dev-cpp comes with the LD (GNU Linker). I am new to assembly code and the processes to create a 32-bit Windows executable from an assembler file. I tried using this:

nasm -f win32 ass.asm
nasm -o ass ass.o

I had no success using these commands to create an executable. What is the proper way to assemble (with NASM) and link to generate an executable that will run on 32-bit Windows?

like image 763
Lesta Crenay Avatar asked Dec 26 '15 00:12

Lesta Crenay


People also ask

How do I compile and run an ASM file in Linux?

How To Compile And Link Asm File In Linux? Your NASM assembly file needs either nasm -f elf32 myfile. Asm -o myfile.o; then if you are using GCC, link it there too with an executable like gcc -m32 -o myfile Myfile. Replace 32 with 64 if you are using 64-bit code. NASM files can never be assembled with GUIC. how do i compile and run an asm?

How do I access the Internet from an ASM file?

To access the Internet, enter one of the following two commands: I will type nasm -f win32 assembly and use test-o to do it. You can use ld test.o -o assembly.exe to do this. How Do I Run An Asm File In Linux? There is no point opening the terminal or console without the console open.

How to assemble the assembly source files to object code?

Assemble the assembly source files to object code. Compile but not link C source files to object code. In gcc: gcc -c file.c -o file.o

How do I assemble and run Masm32?

The simplest way to quickly assemble and run the program is to use QEditor, which is included with the MASM32 package. Run C:\masm32\qeditor.exe.


1 Answers

One of your comments that doesn't seem to exist anymore did mention that you had Dev-Cpp on installed on your Windows. If you have the Dev-Cpp MinGW bin directory on your path, then the GNU Linker LD is available to you.

I don't know if you are using a 32-bit or 64 GCC with Dev-Cpp, but this should work for both to generate a 32-bit executable:

nasm -f win32 ass.asm -o ass.obj
ld -mi386pe -o ass.exe ass.obj

That will tell NASM to generate a 32-bit Win32 object, and LD will link the object to an i386pe 32-bit Windows executable.

Alternatively you could download the the GoLink linker. Once you extract it onto your path you could do something like:

nasm -f win32 ass.asm -o ass.obj
GoLink.exe  /console ass.obj kernel32.dll user32.dll gdi32.dll 

You may have to specify your code entry point (label) with something like:

GoLink.exe  /console /entry _start ass.obj kernel32.dll user32.dll gdi32.dll 

Where _start could be the label where you expect your program to start. You don't need any of the DLL's listed if you aren't calling any of the Win32 API.

If you aren't creating a console application then you can leave off /console

like image 119
Michael Petch Avatar answered Sep 21 '22 01:09

Michael Petch