"Inline" Assembly: Mixing Assembly and C++

CS 301 Lecture, Dr. Lawlor

The most portable way to include some assembly functions in your code is to compile the assembly in a separate file, then link it with the C++.  For example, in a file name "foo.S":
section .text
global _foo
mov eax,7
(Note the weird underscore in front of the function name--this is a Windows thing!)
You'd assemble this into "foo.obj" on windows with this command line:
    nasm -f win32 foo.S
Then in a file named "main.cpp", we call foo with an extern "C" prototype:
#include <iostream>
extern "C" int foo(void);

int main() {
std::cout<<"Foo returns "<<foo()<<"\n";
return 0;
We compile the C++ and link it to the assembly using the Microsoft Visual C++ compiler like this:
    cl -EHsc main.cpp foo.obj
(You may have to run "vc_vars.bat" to get "cl" into your PATH.)

We now have a functioning C++/Assembly executable!  The same exact command-line trick works on Linux or OS X with gcc.

If you don't like the command line, and few people do, you can integrate NASM with Visual C++ as I explain here.

Mixed Assembly and C++ in One Source File

So it's actually possible on most compilers to include a little bit of assembly code right inside your C or C++ file, called "inline assembly" because the assembly is inside the C/C++.  This is usually a bit faster (because no function call overhead) and simpler (less hassle at build time) than having a separate ".S" file that you run through YASM and then link to the C or C++ code.  However, for long stretches of assembly, a separate file still works better.

Microsoft Inline Assembly:

Here's a simple example in Microsoft Visual C++ inline assembly:
int foo(void) {
mov eax,100
Note that now:
  • The keyword is __asm
  • The assembly code is wrapped in curly braces
  • The destination register is on the *left*, just like yasm.

Note also that I've used the "leave" instruction to clean up foo's stack frame (mov esp,ebp; pop ebp;) before returning.  The compiler secretly generates the corresponding function prologue at the start of the function.

Microsoft Outside Variable Access:

In Microsoft Visual C or C++, you can read and write variables from the program by just giving their names.

Simple example:
   void *frame;
   __asm mov ebp,frame;
Complicated example:
int foo(void) {
int joe=1234, fred;
mov eax,joe ; eax = joe;
add eax,2 ; eax += 2;
mov fred,eax ; fred = eax
return fred;
This is clearly very convenient!  But what happens if we try to do the same thing with a variable named "al"?  (Remember, "al" is a register on x86!)

GCC Inline Assembly:

Here's an example of how to declare a little assembly snippet inside C++ code using the Linux/UNIX/MacOS gcc compiler:
int foo(void) {
__asm__( /* Assembly function body */
" mov $100,%eax\n" /* moves 100 into eax! */
" leave\n"
" ret\n"

Note that:
  • The keyword is __asm__
  • The assembly code is wrapped in parenthesis.
  • The assembly code shows up as a string
  • There are weird symbols in front of constants ($ means constant) and registers (% means register)
  • DYSLEXIA ALERT: GCC sasembly is abckwards.  The destination register goes at the *end* of the instruction.
I've linked the text to the NetRun version of this code.  Note that I've set the NetRun "Mode" to "Whole Subroutine"--this keeps NetRun from pasting in the start and end of the foo subroutine.

The bottom line is just to use the __asm__ keyword, which takes the assembly code as a big string.

GCC Outside Variable Access:

Accessing outside variables is truly hideous in gcc inline assembly.

Simple example:
    void *frame; /* Frame pointer */
   __asm__ ("mov %%ebp,%0":"=r"(frame));
Complicated example:
int foo(void) {
int joe=1234, fred;
" mov %1,%%eax\n"
" add $2,%%eax\n"
" mov %%eax,%0\n"
:"=r" (fred) /* %0: Out */
:"r" (joe) /* %1: In */
:"%eax" /* Overwrite */
return fred;
The __asm__ keyword can take up to four strings, separated by colons:
  • The assembly code.  Now registers need to be prefixed with "%%", not just "%", to distinguish them from arguments.
  • A comma-separated list of output arguments.  These can go into registers ("=r"), memory ("=m"), etc.
  • A comma-separated list of input arguments.
  • A comma-separated list of overwritten registers ("trashed" registers).  The compiler then knows not to put anything important in these registers.
See page 227 of the text for more details, or the gcc manual for so many hideous details you'll want to cry.

GCC Whole Subroutine in Assembly

Partly because GCC's inline assembly syntax is so horrible, it's often easier to just write the whole subroutine (argument access, frame setup, and value return) in assembly.  There doesn't seem to be a way to do this in Visual C++, although (in either case) it's easy enough to separately compile a whole file full of pure assembly code and just link it in.

To write a subroutine in assembly, just:
  • Write a C function prototype.  In C++, make the prototype 'extern "C"' to avoid a link error.
  • Put your code in an "__asm__" block outside any subroutine.
  • Put the subroutine name at the start of the assembly block as a label.
  • If you want to call the subroutine from outside that file, use ".globl my_sub" to make the subroutine's name visible outside.
Here's a complete example, where my assembly subroutine just returns 100:
extern "C" int my_sub(void); /* Prototype */

__asm__( /* Assembly function body */
" mov $100,%eax\n"
" ret\n"

int foo(void) {
return my_sub()+1;

This is actually a pretty clean way to do inline assembly in gcc, although you do have to remember the calling convention (%rdi, %rsi, etc) to find your arguments!