3

I wanted to compile Win32 executables using NASM, but I didn't know how. Is there any necessary headers that tells Windows this file is executable?

Also, can anyone tell me how Windows know if this is a form application or a console application?

Joshua Dannemann
  • 2,003
  • 1
  • 14
  • 34
David Xu
  • 169
  • 1
  • 3
  • 14

1 Answers1

8

Question 1:

I wanted to compile Win32 executables using NASM, but I didn't know how.

compile:

nasm -f win32 test.asm -o test.o

ld test.o -o test.exe

Source: http://ccm.net/faq/1559-compiling-an-assembly-program-with-nasm

Question 2:

Is there any necessary headers that tells Windows this file is executable?

The file extension indicates an executable file. By reading the PE file structure of this file "Windows" is able to get all information it needs to properly load and execute the file.

Question 3:

Also, can anyone tell me how Windows know if this is a form application or a console application?

Read this:

On a more technical note, the only difference between a Console and a Windows executable is one byte in the PE header of the exe file. Toggling this byte manually (e.g. using a hex editor) converts the application type. This is a well-published hack that is used to create console applications in VB6 (where this type of application was not explicitly supported).

To determine and change the subsystem type of an application, you need to read parts of the PE header. The address of the subsystem data is not fixed though, because it's part of the optional file header whose position is determined by an address stored in the DOS file header (in the member e_lfanew). This address actually points to the _IMAGE_NT_HEADERS record which, in turn, includes the IMAGE_OPTIONAL_HEADER32 structure. This has an int161) member called Subsystem. The member's value is 2 for a Windows application and 3 for a console application. Other subsystems exist (in particular, POSIX and kernel). I've written a small VB6 application to change the subsystem of an application, which can be downloaded from ActiveVB as source code.

Source: Difference between Windows and Console application

Community
  • 1
  • 1
ibmicroapple
  • 111
  • 1
  • 4
  • I have downloaded NASM 2.11.06 and 2.14.02 as in the link, so did from www.nasm.com. I didn't even find ld.ex file there. I found ldrdf.exe instead. But I use the ldrdf as ldrdf test.o -o test.exe, it is not working. I used a code example written in the link you provided. – AirCraft Lover May 05 '19 at 06:42
  • Is it possible to run this for 64 bit windows env like nasm -f win64-o ArraySum.o ArraySum.asm – rinilnath Sep 11 '19 at 07:38
  • @AirCraftLover - Even I encountered the same issue. You have to download MinGW installation manager and install the mingw32-binutils. This will get you the ld executable. – Guru Pasupathy Jun 07 '20 at 04:23