您的位置:首页 > 编程语言 > C语言/C++

C/C++中调用直接用汇编写的函数

2011-05-14 20:31 141 查看
// test1_func.asm

.386
.MODEL flat,stdcall
.STACK 4096

.data
a DWORD 886

.code

MyAsmFunc proc
mov eax,a
ret
MyAsmFunc endp

end
 

 

// test1.cpp

#include <stdio.h>

extern "C" int __stdcall MyAsmFunc(void);

int main(void)
{
printf("[%d]/n", MyAsmFunc());
return 0;
}


 

编译链接方法(需要VC6.0以及Masm6.15):

1、ml -c -coff test1_func.asm

2、cl test1.cpp test1_func.obj

 

第一步生成test1_func.obj,第二步生成test1.obj之后将这两个obj文件进行链接,从而生成最终的test1.exe可执行文件。

 

以下还有一个稍稍复杂些的例子:

 

// test2_func.asm

.386
.model flat,c

.code

addnum proc
push ebp
mov ebp,esp
push esi
mov esi,[ebp+8]
mov eax,[esi]
add eax,[esi+4]
add eax,[esi+8]
pop esi
pop ebp
ret
addnum endp

end


 

// test2.cpp

#include <iostream>
using namespace std;

struct num
{
int a;
int b;
int c;
};

extern "C" int addnum(num *ss);

int main(void)
{
num s;
s.a=1000;
s.b=2000;
s.c=3000;

int d = addnum(&s);
cout<<d<<endl;

return 0;
}


 

编译链接方法:同上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: