Creating a Combined C++/Assembly Language Project

时间:2022-09-01 19:12:13

http://kipirvine.com/asm/4th/ide/vs6/cppProject/index.htm

Updated 11/25/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-bitClick here to find out how to do it.

Step 1: Create a C++ Project

  1. Select New from the File menu.
  2. In the New dialog window, select the Project tab, and select Win32 Console Application from the list.
  3. Enter the project name and location as shown in the following image. We have entered Test1 as the project name:

Creating a Combined C++/Assembly Language Project

Step 2: Create the C++ Source Module

  1. Select New from the File menu.
  2. Under the Files tab, select C++ Source File.
  3. Enter a name in the File name entry. We have entered the name main.

Creating a Combined C++/Assembly Language Project

Click on OK. When the empty main.cpp editing window appears, input 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

  1. Select New from the File menu.
  2. Under the Files tab, select C++ Source File.
  3. Enter a name in the File name entry. We have entered the name subr.asm:

Creating a Combined C++/Assembly Language Project

(The Text File category also works, as long as you enter an .ASM extension when you name the file.)

When the subr.asm editing window opens, input the following 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 and Build the Program

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.

Select Add To Project... | Files from the Project menu. In the File name field of the Insert Files Into Projectdialog window, enter the name subr.obj. Click on OK. The name subr.obj will appear in the list of files in theWorkspace window after you click on its File View tab. (In case the window is not visible, select Workspacefrom the View menu.)

Use the mouse to select the window containing main.cpp, and select Build Test1.exe 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 Execute Test1.exe from the Build menu. The following output should appear in a console window:

Value returned by DoubleIt is 112
Press any key to continue

If you try to run the program by selecting Start Debug | Go from the Build menu, the program output disappears so quickly that you cannot read it. On the other hand, if you press the F10 key to single-step through the program in the debugger, you can see the program's output in the MS-DOS window (see the icon on your status bar at the bottom of the screen).

Tutorial finished.