![]() |
Languages »
C / C++ Language »
General
Intermediate
License: The Code Project Open License (CPOL)
Using Assembly routines in Visual C++By Nadeem AfanaAn article on mixed-language programming. |
ASM, VC7.1, WindowsVS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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:
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.
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 Convention | Name in .OBJ file | void 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 |
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
;============================================
.486
.model flat, stdcall
option casemap :none
.code
;--------------------------------------------
Addup PROC, Arg1:DWORD, Arg2:DWORD, Arg3:DWORD
mov eax, Arg1
add eax, Arg2
add eax, Arg3
ret
Addup ENDP
;----------------------------------------------
END
;----------------------------------------------
; To compile this file use:
; ml.exe Addup.asm /c /coff
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Jul 2005 Editor: Smitha Vijayan |
Copyright 2005 by Nadeem Afana Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |