Click here to Skip to main content
13,351,225 members (48,924 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 18 Jul 2005

Using Assembly routines in Visual C++

, 19 Jul 2005
Rate this:
Please Sign up or sign in to vote.
An article on mixed-language programming.


Mixed-language programming allows you to combine the unique strengths of Visual C++ with your assembly-language routines. There are some cases where you want to achieve things using inline assembly, such as improving speed, reducing memory needs and getting more efficiency. However, inline assembler is not as powerful as MASM, it does not support macros or directives. This article explains how to write assembly routines that can be called from Visual C++ modules. But before proceeding, there are two terms you have to be familiar with:

Naming Convention

It specifies how the compiler alters the symbol name as it places the name in a .OBJ file. For example, a C function “void func(void)” is put as “_func@0” in the .OBJ file, whereas in C++, it is decorated to “?func@@YAXXZ” in the .OBJ file.

Calling Convention

It determines how a language implements a call to a procedure and how the procedure returns to the caller. In the C Calling Convention (__cdecl), arguments are pushed onto the stack from right to left, the called procedure returns without removing the arguments from the stack. It is the caller’s responsibility to clean the stack after the call.

In Standard Calling Convention (__stdcall), arguments are pushed onto the stack from right to left, the called procedure cleans the stack if it does not accept a variable number of arguments. In Fast Calling Convention (__fastcall), the first two parameters are loaded into ecx, edx registers, respectively. The other parameters are pushed onto the stack from right to left. The same function is responsible for cleaning the stack.

Here is a summary table for some calling and naming conventions with an example:

Language, Calling ConventionName in .OBJ filevoid func (void)
C, cdecl_name_func
C, __stdcall_name@nn_func@0
C, __fastcall@name@nn@func@0
C++, cdecl?name@@decoration?func@@YAXXZ
C++, __stdcall?name@@decoration?func@@YGXXZ
C++, __fastcall?name@@decoration?func@@YIXXZ

How to use MASM routines in Visual C++ modules:

The following assembly function (Addup) accepts three DWORD (32-bit) values as parameters, and then returns the sum of them. Whenever you write a routine make sure you don't choose it as entry point (i.e. END Addup).

; Addup.asm file
.model flat, stdcall
option casemap :none


Addup PROC, Arg1:DWORD, Arg2:DWORD, Arg3:DWORD

mov eax, Arg1
add eax, Arg2
add eax, Arg3


Addup ENDP

; To compile this file use:
; ml.exe Addup.asm /c /coff
  1. Now copy the resultant .obj file (Addup.obj) into your VC++ application’s project folder.
  2. Add to the C++ source file (.cpp) that will use this routine, the following prototype:
    extern "C" int __stdcall Addup (int, int, int);

    Or you can put this prototype into a new header file and then add it to your project. The extern “C” indicates C naming convention since you are in a C++ environment. MASM uses C naming convention when you specify STDCALL. Finally, the __stdcall indicates that the routine uses standard calling convention.

  3. Now add the .OBJ file name (Addup.obj) into your linker commands window. Or you can add the file manually to your project. Otherwise, you will get an “unresolved external” error.
  4. Now you should compile the project fine without any errors.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nadeem Afana
Chile Chile
Nadeem loves all kinds of programming: Both low-level and high-level.
He started programming at age 14, he started with Visual Basic and then C/C++, MFC, ATL, x86 Assembly and he creates software using .NET (C++/CLI, C#,ASP.NET, and MVC)

Although he has never worked for a company or other, he enjoys programming on his own and likes contributing to others.

You may also be interested in...


Comments and Discussions

GeneralQuestion about LEA and MOV Pin
scorp00720-Apr-08 19:00
memberscorp00720-Apr-08 19:00 
GeneralRe: Question about LEA and MOV Pin
Nadeem Afanah20-Apr-08 20:37
memberNadeem Afanah20-Apr-08 20:37 
GeneralRe: Question about LEA and MOV Pin
scorp00720-Apr-08 20:46
memberscorp00720-Apr-08 20:46 
Generalfastcall question. Pin
scorp0071-Nov-07 15:06
memberscorp0071-Nov-07 15:06 
GeneralRe: fastcall question. Pin
Nadeem Afanah1-Nov-07 17:52
memberNadeem Afanah1-Nov-07 17:52 
GeneralRe: fastcall question. Pin
scorp0071-Nov-07 21:32
memberscorp0071-Nov-07 21:32 
GeneralRe: fastcall question. Pin
Nadeem Afanah2-Nov-07 5:26
memberNadeem Afanah2-Nov-07 5:26 
GeneralReturn values Pin
scorp0071-Nov-07 15:02
memberscorp0071-Nov-07 15:02 
GeneralRe: Return values Pin
Nadeem Afanah1-Nov-07 15:12
memberNadeem Afanah1-Nov-07 15:12 
GeneralRe: Return values Pin
scorp0071-Nov-07 15:34
memberscorp0071-Nov-07 15:34 
GeneralRe: Return values Pin
Nadeem Afanah1-Nov-07 18:02
memberNadeem Afanah1-Nov-07 18:02 
GeneralRe: Return values Pin
scorp0071-Nov-07 21:34
memberscorp0071-Nov-07 21:34 
GeneralRe: Return values Pin
Nadeem Afanah2-Nov-07 5:27
memberNadeem Afanah2-Nov-07 5:27 
GeneralRe: Return values Pin
scorp0072-Nov-07 14:48
memberscorp0072-Nov-07 14:48 
QuestionUsing Inline assembly in Visual studio 2005 Pin
@run10-Sep-07 21:23
member@run10-Sep-07 21:23 
AnswerRe: Using Inline assembly in Visual studio 2005 Pin
Nadeem Afanah11-Sep-07 18:25
memberNadeem Afanah11-Sep-07 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Jul 2005
Article Copyright 2005 by Nadeem Afana
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid