我需要从程序集中调用PrintResult来显示结果。我知道我必须在某处使用extrn _PrintResult,我应该使用call _PrintResult调用该函数,但我不确定如何使用它。任何建议
public _Square
.386
.model flat
.code
_Square proc
mov eax, [esp+4]
imul eax
ret
_Square endp
.............这是我的C ++代码.........
#include <iostream>
using namespace std;
enum ResultCode {ShowSquare};
enum SuccessCode {Failure, Success};
extern "C" long Square (long);
void main ()
{
long Num1;
long Num2;
do
{
cout << "Enter Number to Square" << endl;
cin >> Num1;
Result = Square (Num1);
cout << "Square is: " << Result << endl;
} while (Result > 0);
}
void PrintResult (ResultCode PrintCode, long Value) //PrintCode, long Value)
{
switch (PrintCode)
{
case ShowSquare:
cout << "Display of square is: " << Value << endl;
break;
default:
cout << "Error in assembly routines" << endl;
}
}
答案 0 :(得分:5)
我通常不喜欢发布完整的代码,但尝试一下:
.386
.model flat
.code
_Square proc
mov eax, [esp+4]
imul eax
push eax ; Save the calculated result
; Call PrintResult here
push eax ; value
push 0 ; ShowSquare
call _PrintResult
add esp, 8 ; Clear the stack
pop eax ; Return the calculated result
ret
_Square endp
#include <iostream>
using namespace std;
enum ResultCode {ShowSquare};
enum SuccessCode {Failure, Success};
extern "C" long Square(long);
int main(int argc, char* argv[])
{
long Num1, Num2;
do
{
cout << "Enter number to square" << endl;
cin >> Num1;
Num2 = Square(Num1);
cout << "Square returned: " << Num2 << endl;
}
while (Num2);
return 0;
}
extern "C"
void PrintResult(ResultCode result, long value)
{
switch (result)
{
case ShowSquare:
cout << "Square is: " << value << endl;
break;
default:
cout << "Error calculating square" << endl;
break;
}
}
因为您正在编写C程序,所以默认调用机制是cdecl,这意味着所有参数都在堆栈上传递,返回值在eax
中传回,调用者是负责清理堆栈。
因此,为了调用PrintResult,您必须在调用过程之前将所有参数都压入堆栈。在程序返回后,我们必须清理堆栈(add esp, 8
)。
因为cdecl调用约定允许在调用期间修改eax
,所以当PrintResult返回时可能不会保留eax
,因此我们在调用PrintResult之前保存计算结果然后恢复它在通话结束后返回。
我没有尝试过上面的代码,但我希望它能帮助你走上正确的道路。
注意:因为您使用的是C ++编译器,所以需要在PrintResult之前使用extern "C"
。