| Creating a Combined C++/Assembly Language Project |
|
Updated 08/28/2002
Step 1: Create a C++ Project
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
Click on Open. When the main.cpp editing window appears, enter the following C++ source code and save the file:
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 ModuleSelect 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:
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 ModulesWith 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 ProgramSelect Start Without Debugging from the Debug menu. The following output should appear in a console window:
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.
|