Given some MASM code in a file named FLAT32.ASM where its entry pointis _start, to assemble this code into a 32-bit flat memory modelapplication for the Windows NT environment, perform the following foursteps:
- Place the .386 or .486 directive in the code prior to the .MODEL flat, stdcall directive to ensure that the code and data segments are correctly initialized for a 32-bit flat memory model application.
- Assemble the source code using the following command:
ML /c flat32.asm. The /c assembler option prevents the MASM from invoking the 16-bit linker provided with MASM.
- For MASM 6.0a or 6.0b, use the CVTOMF utility provided with the Windows NT SDK to convert the generated object files from OMF format to COFF format. The command is as follows:
cvtomf flat32.obj This step is unnecessary with MASM version 6.1 and later if you use the /coff switch because it will produce a COFF format object file.
- Use the LINK32 utility provided with the Windows NT SDK or LINK utility provided with Visual C++ for Windows NT to link the object modules. Specify a path to KERNEL32.LIB. The command line is as follows:
link32 -subsystem:console -entry:_start -out:flat32.exe flat32.obj kernel32.lib To create an executable module that contains symbolic debugging information, add the -Zi switch to the assembler options (Step 2) and add the following switches to the linker command line: -debug:full -debugtype:cv.
If you are using LINK32 from the Windows NT SDK, do not use theINCLUDELIB directive and do not place the starting label after the ENDdirective in the MASM code. The LINK32 utility provided with theWindows NT SDK ignores the default library names in object files andrequires the -entry switch on the linker command line to specify thestarting address.
Windows NT does not support the MLX driver provided with MASM versions6.0a and 6.0b. Also, Windows NT does not support the MS-DOS-extenderused by MASM versions 6.1 and 6.1a. Therefore, it may be necessary toperform the assembly step on a machine running MS-DOS while the othersteps run on a machine running Windows NT. Another option is toupgrade to MASM version 6.11.
The code below is a "Hello, world" application developed in assemblerto use the 32-bit flat memory model of Windows NT.
; Assemble options needed: /c.386.MODEL flat, stdcallSTD_OUTPUT_HANDLE EQU -11GetStdHandle PROTO NEAR32 stdcall, nStdHandle:DWORDWriteFile PROTO NEAR32 stdcall, hFile:DWORD, lpBuffer:NEAR32, nNumberOfBytesToWrite:DWORD, lpNumberOfBytesWritten:NEAR32, lpOverlapped:NEAR32ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD.STACK 4096.DATAmsg DB "Hello, world.", 13, 10written DW 0hStdOut DD 0.CODE_start: INVOKE GetStdHandle, STD_OUTPUT_HANDLE ; Standard output handle mov hStdOut, eax INVOKE WriteFile, hStdOut, ; File handle for screen NEAR32 PTR msg, ; Address of string LENGTHOF msg, ; Length of string NEAR32 PTR written, ; Bytes written 0 ; Overlapped mode INVOKE ExitProcess, 0 ; Result code for parent processPUBLIC _startEND