Using Assembly from Microsoft Visual C++ 2010

Dr. Orion Lawlor, 2011-10-12

Step 1: Write C++ Code

First, hit File - New Project to make a standard C++ console application:

Where to click in Visual C++ GUI

Next, remove the Microsoft junk, like the procompiled header.

Where to click in Visual C++ GUI

Next, write some test C++ code. It helps to run at this point, to make sure everything works.

Where to click in Visual C++ GUI

Step 2: Write assembly code

Now write an assembly language source file with these critical pieces:

Finally, right click on your new file, and hit properties.

Where to click in Visual C++ GUIVisual doesn't know how to compile the .S file by default, so it ignores it: "Does not participate in build." Change this to "Custom Build Tool": Where to click in Visual C++ GUI Now you need to download and install NASM (or YASM, etc.) You then give Visual Studio the Command Line needed to run your assembler, just like at the DOS prompt:

	"\Program Files\nasm\nasm" -f win32  asmstuff.S
The assembler will output a .obj file, which you list under "Outputs".

Where to click in Visual C++ GUI

Build the project, and you should be able to call assembly functions from your C++, and vice versa!  Don't forget extern "C" from C++!

Where to click in Visual C++ GUI

Simple C++ code:

#include <iostream>
extern "C" int foo(void); // written in assembly!

int main() {
	std::cout<<"Foo returns "<<foo()<<"\n";
system("pause");
return 0;
}
Assembly code:
section .text ; makes this executable
global _foo ; makes this visible to linker
_foo:
mov eax,7 ; just return a constant
ret

Info for CS 301: Assembly Language, by Dr. Lawlor