Using 64-bit Windows Assembly Language from Microsoft Visual C++ 2015

Dr. Orion Lawlor, 2017-09

Step 1: Write C++ Code

First, make a C++ project in 64 bit mode (x64 dropdown, just to the right of the Debug drop down).
Remove the Microsoft junk like precompiled header.
Test out the program to make sure plain C++ works.

Visual Studio 2015, C++
        program open

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.  Set:
Properties
      dialog, setting Custom Build Tool

Step 3: Make Visual Studio call NASM

Now you need to download and install NASM (or YASM, etc.)
Unzip nasm to a handy location, like C:\nasm\nasm.exe

You then give Visual Studio the full Command Line needed to run your assembler, just like you'd type at a command prompt:
	C:\nasm\nasm.exe   -f win64   my_asm.asm
The assembler will output a .obj file, which you list under "Outputs".
Add Command Line and Outputs to point to NASM assembler.

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++!

One annoyance: any build errors show up in "Errors" as "cmd.exe returned error 1", which tells you nothing useful.  Check the "Output" tab to see nasm's line numbers and other helpful error info.

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;
}
64-bit Windows assembly code:
section .text ; makes this executable
bits 64 ; allow 64-bit register names
global foo ; makes this visible to linker
foo:
mov rax,7 ; just return a constant
ret


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