[英]How do I call a C++ Function from Assembly?
我正在编写一个C ++程序,该程序需要一些自定义的汇编代码。
我这样写汇编代码
__asm
{
//Code Here
}
我希望能够在程序集中调用C ++函数,所以就像call
MyFunction(eax, ebp).
不知道您在说什么指令集。 但是只要指令集支持它,您就可以1.在代码中声明并初始化函数指针。2.使用调用或跳转类指令从函数指针指向的内存地址执行
如果遵守调用约定,则可以调用函数。 下面是Visual Studio 2010(32位)的示例:
#include <iostream>
char* txt = "Hello world";
int myfunc2 (char* s)
{
std::cout << s << std::endl;
return 0;
}
int myfunc1 ()
{
char* s = new char[80];
__asm
{
mov ebx, s ; = mov ebx, [ebp -4]
mov byte ptr [ebx], 'H'
mov byte ptr [ebx+1], 'e'
mov byte ptr [ebx+2], 'l'
mov byte ptr [ebx+3], 'l'
mov byte ptr [ebx+4], 'o'
mov byte ptr [ebx+5], 0 ; Don't forget the terminator!
push s
call myfunc2 ; Call the function above
add esp, 4
push txt
push s
call strcpy ; Function of C-library
add esp, 8
push s
call puts ; Function of C-library
add esp, 4
}
delete[] s;
return 0;
}
int main()
{
myfunc1();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.