You are currently offline, waiting for your internet to reconnect

32-Bit Flat Memory Model MASM Code for Windows NT

This article was previously published under Q94314
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
The Microsoft Macro Assembler (MASM) versions 6.0a and later supportgenerating 32-bit flat memory model code through the .MODEL flatdirective. When combined with the CVTOMF and LINK32 utilities and theKERNEL32.LIB library distributed with the Windows NT softwaredevelopment kit (SDK) or the LINK utility and the KERNEL32.LIB librarydistributed with Microsoft Visual C++ for Windows NT, MASM cangenerate a 32-bit flat memory model application for the Windows NTenvironment.

Because MASM version 6.0 does not properly support the _stdcallconvention, it cannot be used to generate applications for the WindowsNT environment.
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:
  1. 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.
  2. 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.
  3. 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.
  4. 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.

Sample Code

; 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				
kbinf 6.00a 6.00b 6.10 p_w32dev

Article ID: 94314 - Last Review: 11/15/2003 01:33:43 - Revision: 1.1

Microsoft Macro Assembler 6.11 Standard Edition

  • KB94314