Creating a Combined C++/Assembly Language Project

 

Updated 08/28/2002

Before beginning this tutorial, you must have already installed a command in your Tools menu in Visual Studio that assembles an ASM source file without linking. In our samples, the tool will be called Assemble 32-bit. Click here to find out how to do it.

Step 1: Create a C++ Project

  1. Select New from the File menu, then select Project.
  2. In the New Project dialog window, select Visual C++ Projects in the Project Types panel.
  3. In the Templates panel, select Win32 Project.
  4. Enter the project Name and Location near the bottom of the window. We will use Test1 as the project name, but you can use any name:

In the Win32 Application Wizard window, select Application Settings. Under Application Type, select Console Application and place a check next to Empty Project:

Step 2: Create the C++ Source Module

  1. Select Add New Item... from the Project menu.
  2. In the Templates pane of the Add New Item dialog, select C++ File.
  3. In the FileName entry, enter "main" (without the quotes):

Click on Open. When the main.cpp editing window appears, enter the following C++ source code and save the file:

// main.cpp

// Calls the DoubleIt function, written
// in assembly language.


#include <iostream>


using namespace std;

extern "C" int DoubleIt(int n);

void main()
{
	cout << "Value returned by DoubleIt is " 
		 << DoubleIt(56) << endl;
}

Notice how we included the function prototype for DoubleIt, along with the required extern and "C" qualifiers. The qualifiers are discussed in Section 12.3.4 in the book.

Step 3: Add the ASM Source Module

Select Add New Item... from the Project menu.

In the Templates pane of the Add New Item dialog, select C++ File.

In the FileName entry, enter "subr.asm" (without the quotes):

(As you see, we did not actually add a C++ source file. Instead, we fooled the project manager into creating an ASM file by changing the filename extension.)

When the subr.asm editing window opens, enter the following ASM source code and save the file:

; Subr.asm

; ASM function called from C++
 
.586
.model flat,C

.code
;---------------------------------------------
DoubleIt PROC,
	inval:DWORD		; receives an integer
; Returns: double the value of inval, in EAX.	
;----------------------------------------------
	mov  eax,inval		; get parameter
	add  eax,eax		; return its double
	ret
DoubleIt ENDP
END 

We chose not to use the INCLUDE Irvine32.inc directive here because DoubleIt doesn't call any library procedures. But we at least must identify the processor type (.586 = Pentium), the memory model (.model flat), and the language calling convention (C).

Step 4: Assemble the Two Modules

With the subr.asm module as the active edit window, select Assemble 32-bit from the Tools menu. (Remember the cautionary statement at the beginning of this tutorial?) We will assume that the module assembled successfully.

Now select Add Existing Item... from the Project menu. In the File name field of the Add Existing Item dialog window, enter the name subr.obj. Click on Open. The name subr.obj will appear in the list of files in the Solution Explorer window. (In case that window is not visible, select Solution Explorer from the View menu.)

Now make the main.cpp window the active window, and select Build Solution from the Build menu. There should be no errors, but in case you have errors in the main.cpp file, fix them and try again.

Step 5: Run the Program

Select Start Without Debugging from the Debug menu. The following output should appear in a console window:

Value returned by DoubleIt is 112
Press any key to continue

You can also run the program by selecting Start from the Debug menu, but the program flashes its output on the screen so quickly that you cannot read it. A simple workaround is to call the getch() function at the end of main() to make the program wait for a keystroke. If you call getch(), you must #include <conio.h>.

Tutorial finished.