8

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?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Lesta Crenay
  • 101
  • 1
  • 1
  • 3
  • 3
    nasm is an assembler, it produces object files (or flat binaries but that's not applicable). You need to link the object using a linker. – Jester Dec 26 '15 at 00:41
  • 6
    nasm -f win32 outputs an object file, not a Win32 executable. You need a linker. If you don't have the GNU Binutils tools(which has the _LD_ linker) on your system (or Microsoft linker) I recommend downloading [GoLink](http://www.godevtool.com/) and then install it somewhere on your path. Then look at this [Stackoverflow answer](http://stackoverflow.com/a/12575061/3857942) – Michael Petch Dec 26 '15 at 00:41
  • 5
    Upvoting because it's not that bad a question. It's well asked, easy to understand what the OP needs, and easy to answer. It's very trivial, but it's not uncommon for something to be mysterious until you know how things work, and then it's trivial. I'm surprised nobody's found a duplicate for it. Most of the questions I found while searching have an answer to this as part of the question. This one is nice and short: http://stackoverflow.com/questions/21010335/how-to-use-nasm-to-generate-a-dynamic-linked-exe-on-windows. Oh, Michael's comment did link to a good Q&A. dup-voted. – Peter Cordes Dec 26 '15 at 08:47
  • 2
    @Lesta: As a rookie, you're going to want to take a look at the [x86 tag wiki](http://stackoverflow.com/tags/x86/info). You'll need a debugger while learning asm. If you find a tutorial that's really helpful, but not mentioned in the wiki, leave a suggested edit for the wiki. (Or reply to this comment and I'll add it.) – Peter Cordes Dec 26 '15 at 08:53
  • Thanks for the help Jesta, Michael and Peter. Those links really rock. – Lesta Crenay Dec 26 '15 at 22:21
  • 1
    My comment about dev-cpp is based upon a comment that no longer exists, but I have updated the question. I've also voted to reopen it. Even if it becomes marked as a duplicate, it may still help someone else down the line while searching for a similar question. – Michael Petch Dec 26 '15 at 22:51
  • 1
    Voting to reopen. I don't know if it's the OP or @MichaelPetch's edit, but this question is just fine. No clue why it was closed. – David Hoelzer Dec 26 '15 at 23:52
  • I did vote to reopen. We'll see. – David Hoelzer Dec 26 '15 at 23:57
  • Thanks for the vote. ;-) – Lesta Crenay Dec 30 '15 at 00:51

1 Answers1

10

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

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • linked it probably well with devs linker, but throws a warning: "ld: warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000" .What could I have missed??? – Lesta Crenay Dec 28 '15 at 22:42
  • @LestaCrenay Odd, are you using _gcc_ at all by any chance? What commands are you now using to compile/assemble and link? – Michael Petch Dec 28 '15 at 22:48
  • @LestaCrenay : It sounds like you are trying to link with the Microsoft C runtime library. – Michael Petch Dec 28 '15 at 23:05
  • 1
    The warning is gone. Managed to erase it but the program crashes. It shows the hello.exe has stopped working popup window when run ..The linking command is: ld -e _WinMain@16 hello.o -o hello.exe. Atleast, it doesn't show any warning. What could be the flaw? And Thanks for the edit @Michael Petch. The question sounds cooler. – Lesta Crenay Dec 30 '15 at 01:07