14.6 Writing Assembly-Language Code

Assembly-language Windows applications are highly structured assembly-language programs that use high-level-language calling conventions in addition to Windows functions, data types, and programming conventions. Although you assemble assembly-language Windows applications by using the Microsoft Macro Assembler (ML), the goal is to generate object files that are similar to object files generated by using CL. Following are some guidelines designed to help you meet this goal and create assembly-language Windows applications:

1.Include the CMACROS.INC file in the application source files. This file contains high-level-language macros that define the segments, programming models, function interfaces, and data types needed to create Windows applications. For more information about Windows assembly-language macros, see the Microsoft Windows Programmer's Reference, Volume 4.

2.Define the programming model, setting one of the options memS, memM, memC, or memL to 1. One of these options must be set before you specify the statement that includes the CMACROS.INC file.

3.Set the calling convention to Pascal by setting the ?PLM option to 1. This option must be set before you specify the statement that includes the CMACROS.INC file. Pascal calling conventions are required only for functions that Windows calls.

4.Set the Windows prolog and epilog option ?WIN to 1. This option must be set before you specify the statement that includes the CMACROS.INC file. This option is required only for callback functions (or for exported functions in Windows libraries).

5.Create the application entry point, the WinMain function, and declare it as a public function. It should have the following form:

cProc WinMain, <PUBLIC>, <si,di>
              parmW hinst
              parmW hPrevInstance
              parmD lpCmdLine
              parmW nCmdShow
cBegin WinMain
                  .
                  .
                  .
cEnd WinMain

The WinMain function should be defined within the standard code segment CODE.

6.Make sure that your callback functions are declared:

cProc TestWndProc, <FAR,PUBLIC>, <si,di>
              parmW hWnd
              parmW message
              parmW wParam
              parmD lParam
cBegin TestWndProc
                  .
                  .
                  .
cEnd TestWndProc

Callback functions must be defined within a code segment.

7.Link your application with the appropriate C-language library for Windows and C run-time libraries. To link the application properly, you might need to add an external definition for the absolute symbol __acrtused in your application source file.

Note:

Windows functions destroy all registers except DI, SI, BP, and DS.