October 24, 2016
Hot Topics:

An Introduction to Assembly Language Programming

  • March 29, 2005
  • By David McClarnon (Darwen)
  • Send Email »
  • More Articles »

Adding an Assembler File to Visual C++

This walkthrough is for DevStudio 2002, but equally applies to Visual C++ 6 or DevStudio 2004.

First, you need to include the MASM bin folder in the searched directories for applications.

Go to Tools/Options/Projects/Visual C++ Directories. Add the 'bin' folder, which is under the path where you installed MASM; for instance, if it was installed to C:\MASM32, add C:\MASM32\bin.

Click here for a larger image.

Move this directory to the bottom of the list. This is very imporant. This folder also has a linker application called link.exe that will be used instead of the default linker for Visual Studio if you don't.

Now, create a console application.

Add a file to the project called 'test.asm'. Right-click on the 'source files' folder in Solution Explorer, and go to Add/New Item.

Click here for a larger image.

Enter 'test1.asm', press Return, and a new file called 'test.asm' should have been opened for you.

Enter the following code into the file:

.model flat, stdcall
option casemap :none


TestProc proc dwValue:DWORD

   mov eax, dwValue
   add eax, 100

TestProc endp


This code adds 100 to the input value (dwValue) and returns the result.

Note: All MASM assembler files need to contain the top four lines and end, and then the code goes between the .code and end lines.

Now, you have to configure the build properties for this file. Make sure you're in Debug build and right-click on the file in Solution Explorer and select 'properties'.

Select 'custom build step/general' and enter the following for the 'command line':

ml /c /coff /Zi /Fo"$(OutDir)\$(InputName).asm.obj" "$(InputFileName)"

Now, put the following into the 'Outputs':


For example:

The release build settings are exactly the same except that the compile line doesn't have the /Zi option. This is the 'generate debug info' option, so should be removed for release builds; for example:

ml /c /coff /Fo"$(OutDir)\$(InputName).asm.obj" "$(InputFileName)"

Of course, this can be put into a macro or an addin to automate the process. I have shown the steps involved here so that it can apply equally to all versions of Developer Studio currently in use.

Now, look at how to call functions written in Assembler from C++.

Calling Assembler Code from C++

The above assembler code defines a function called 'TestProc' with an input of a DWORD (in other words, a 32-bit value). You must give C++ a declaration of this function for it to be called. In the .cpp file where your application's Main method exists, put the following at the top, under the #includes:

extern "C" unsigned int __stdcall TestProc(unsigned int dwValue);

Put the following code into the main function

int main(int argc, _TCHAR* argv[])
   unsigned int dwValue = 100;
   unsigned int dwReturn = TestProc(dwValue);

   printf("%d\n", dwReturn);

   return 0;

When running this application, you will see that the result of 200 is returned, as expected. Not only that, but if you put a breakpoint on the line calling TestProc, you will be able to step into this method (F11) and step through the Assembler. In fact, you can even set breakpoints in the Assembler code.


You have seen how to create an Assembler file, compile it, and call it from C++ code. In the next part of this series, I will start to cover the actual instructions that make up assembly language, and cover subjects such as registers.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel