说明
1. 创建VS工程,注意需要选择MASM编译器(见“VS下的编程”第2、3步),否则编译会失败。显示无法链接addem,因为实际上汇编程序没有被编译。
2. 创建文件,如下所示:
3. addem.asm的源代码如下:
.386P
.model flat
public _addem
.code
_addem proc near
push ebp
mov ebp,esp
mov eax,[ebp+16] ; firstargument
add eax,[ebp+12] ; secondargument
add eax,[ebp+8] ; thirdargument
pop ebp
ret
_addem endp
end
需要注意这里的public _addem,包括这里的”_”,和”public”
4. main函数如下:
// Addem Main Program (AddMain.cpp)
#include <iostream>
using namespace std;
extern "C" int addem(int p1, int p2, int p3);
int main()
{
int total = addem( 10, 15, 25 );
cout << "Total= " <<total << endl;
getchar();
return 0;
}
需要注意这里的extern语句。
这里的”C”表示使用C调用惯例,即堆栈传递参数,参数从右往左压栈,调用方清理堆栈。