Using the Development Studio or Visual Workbench with MASM

Last reviewed: July 22, 1997
Article ID: Q106399
4.00 WINDOWS kbenv

The information in this article applies to:

  • Microsoft Visual C++, 32-bit Edition, version 4.0
  • Microsoft Macro Assembler for MS-DOS, versions 5.1x, 6.0x, and 6.1x
  • Microsoft Macro Assembler for Windows NT, version 6.11

SUMMARY

The Developer Studio that ships with Visual C++, 32-bit Edition, version 4.0, does not support assembler source files by default. That is, the Developer Studio does not associate any special significance to .ASM files without being informed otherwise. However, there are several viable options, namely custom build rules, which enable the creation of projects that depend directly upon assembler source files.

The remainder of this article discusses four methods for adding .ASM files to a Visual C++ project. The methods are:

   1. Using custom build rules;

   2. Modifying a VWB makefile and using it as an external makefile;

   3. Using an external makefile to assemble the assembly modules
      and then using an internal makefile for the main program, and

   4. Creating an option on the VWB Tools menu to assemble each
      assembly module as needed.

Of these methods, the use of custom build rules receives special attention. This powerful construct debuts in Visual C++ version 4.0. By utilizing a custom build rule, a project can invoke MASM on a per file basis to assemble .ASM files. The resulting object modules can then be linked into the desired target.

MORE INFORMATION

Method 1

Using Visual C++ 4.0 Developer Studio, the following provides sample code and a step-by-step example of how to create a simple console application, CAPP, that requires MASM (ML.EXE) to assemble one of its source files. Note that the sample code for this example was borrowed from KB article Q104644, Passing C Arrays to MASM by Reference.

Method 1: Sample Code - C Module

/* Filename: CMAIN.C */

#include <stdio.h>

#ifdef __cplusplus extern "C" { #endif

void MasmSub (char *, short *, long *);

#ifdef __cplusplus } #endif

char chararray[4] = "abc";
short shortarray[3] = {1, 2, 3}; long longarray[3] = {32768, 32769, 32770};

void main( void )
{
   printf ("%s\n", chararray);
   printf ("%d %d %d\n", shortarray[0], shortarray[1], shortarray[2]);
   printf ("%ld %ld %ld\n", longarray[0], longarray[1], longarray[2]);
   MasmSub (chararray, shortarray, longarray);
   printf ("%s\n", chararray);
   printf ("%d %d %d\n", shortarray[0], shortarray[1], shortarray[2]);
   printf ("%ld %ld %ld\n", longarray[0], longarray[1], longarray[2]);
}

Method 1: Sample Code - ASM Module

; Filename: MASMSUB.ASM ; Assemble options needed for ML: /c /Cx /coff

.386 .MODEL flat, C .CODE

MasmSub PROC uses esi, \

   arraychar:PTR, \
   arrayshort:PTR, \
   arraylong:PTR

   mov esi, arraychar ; Load ESI with the address of the char array.
   mov BYTE PTR [esi], "x"      ; Since a char is 1 byte long, each
   mov BYTE PTR [esi+1], "y"    ; successive element can be accessed
   mov BYTE PTR [esi+2], "z"    ; by adding 1 more to esi.
   mov esi, arrayshort; Load ESI with the address of the short array.
   add WORD PTR [esi], 7        ; Since a short is 2 bytes long, each
   add WORD PTR [esi+2], 7      ; successive element can be accessed
   add WORD PTR [esi+4], 7      ; by adding 2 more to esi.
   mov esi, arraylong ; Load ESI with the address of the long array.
   inc DWORD PTR [esi]          ; Since a long is 4 bytes long, each
   inc DWORD PTR [esi+4]        ; successive element can be accessed
   inc DWORD PTR [esi+8]        ; by adding 4 more to esi.
   ret
MasmSub ENDP END

Method 1: Step-by-step Instructions for Building CAPP

  1. Start the Visual C++ 4.0 Developer Studio, MSDEV.EXE.

  2. Create and save CMAIN.C and MASMSUB.ASM.

  3. From the File menu, select New. In the New dialog box, select

        Project Workspace and press OK. In the New Project Workspace
        dialog box, select Console Application for Type, CAPP for Name,
        Win32 for Platform, and the desired directory for Location,
        and then press Create. The new CAPP project will now be open.
    

  4. From the Insert menu, choose Files into Project. In the Insert

        Files into Project dialog box, select CMAIN.C as a Common File
        Type and press OK.
    

  5. From the Insert menu, choose Files into Project. In the Insert

        Files into Project dialog box, select MASMSUB.ASM as an All
        Files Type and press OK.
    

  6. From the Build menu, choose Settings. In the Settings For list

        box in the Project Settings dialog box, expand the
        "CAPP - Win32 Debug" target such that MASMSUB.ASM is visible.
        Highlight only MASMSUB.ASM.
    

  7. Select the Custom Build tab.

  8. In the first line of the Build command(s) box, enter the full

        path and file name for MASM, the desired build options, and
        the macro for the File Input Path. The latter can be obtained
        by pressing the Files button and selecting Input Path from the
        resulting menu.  If ML.EXE is in the C:\MASM611A\BIN directory,
        the entry would appear as follows:
    

        C:\MASM611A\BIN\ML.EXE /c /Cx /coff $(InputPath)
    

  9. In the first line of the Output files(s) box, enter

        MASMSUB.OBJ.
    

  10. Press OK in the Project Settings dialog box to save your

        changes. (This also selects the default build options for
        CMAIN.C.)
    

  11. The "CAPP - Win32 Debug" target is now ready to be built.

        From the Build menu, select Rebuild All. First, ML.EXE will be
        invoked to compile MASMSUB.ASM, second, CL.EXE will be invoked
        to compile CMAIN.C, and third, LINK.EXE will be invoked to
        generate CAPP.EXE.
    

  12. The following is the console output of CAPP.EXE:

           abc
           1 2 3
           32768 32769 32770
           xyz
           8 9 10
           32769 32770 32771
    

  13. To build the "CAPP - Win32 Release" target, select its name

        in the Target drop-down list box on the Project Workspace
        toolbar. Then, repeat steps 6 through 11 substituting the
        release version of the target for the debug version.
    

For more information on custom build rules, see the Visual C++ verison 4.0 online help for the Custom Build Tab (press the Help button in the Project Settings dialog box when the Custom Build tab is selected).

Method 2

Modify a Visual WorkBench makefile and then use it as an external makefile. To do this, use the following steps:

  1. Open or create a project with the VWB and set it up as desired

        for the C/C++ files in the project.
    

  2. Close the project and modify the .MAK file to include build

        rules for the assembly modules.
    

  3. Put a comment at the start of the file to force the VWB to read

        the file as an external makefile; otherwise, you could lose your
        changes.
    

The primary disadvantage of this method is you cannot use the VWB options to change the way a build is done with an external makefile.

Method 3

Use an external makefile to assemble the assembly modules, and use an internal makefile for the main program, which includes the MASM .OBJs built with the external makefile. Simply add the .OBJ files to the project list.

With this method, remember to load the external makefile and build the .OBJs after changing any MASM code. By doing this, you can still use the VWB to modify options for the main project, including switching from debug to release. The linker will drop the CodeView information if you select release build for the main project, so always having the debug switches on for the assembler files is not a problem.

Method 4

Create an option on the VWB Tools menu to assemble each assembly module as needed. The following setups can be used to create Tools menu items that build the current file:

  1. From the Tools menu, choose the Customize option.

  2. Select the Tools tab in the Customize dialog box. Press Add.

  3. Specify the complete path to ML.EXE in the Command edit field

        of the Add Tool dialog box, or use the Browse button feature,
        and press OK.
    

  4. Set up the ML.EXE menu option as follows:

             Menu Text:          &Assemble Current File
             Command:            <should be set from step 3 above>
             Arguments:          /c /Zi /Fl /Sa $FileName
             Initial Directory:  $FileDir
             Check the box for:  "Prompt for Arguments"
             Check the box for:   "Redirect to Output Window"
    
    

  5. Press the Close button.

The assembly results will appear in the build output window. You can use F4 to jump to lines containing errors in the source window.


Additional reference words: kbinf 4.00 5.10 5.1 5.10a 5.1a 6.00 6.0 6.00a
6.0a 6.10 6.1 6.10a 6.1a 6.11
KBCategory: kbenv
KBSubcategory: VWBIss
Keywords : VWBIss kbenv
Version : 4.00
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.