我需要从我的程序集中调用PrintResult来显示结果.我知道我必须在某个地方使用extrn _PrintResult,我应该使用调用_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;
}
}
解决方法:
我通常不喜欢发布完整的代码,但是可以尝试一下:
32位汇编
.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
C
#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,您必须在调用该过程之前将所有参数压入堆栈.在过程返回之后,我们必须清理堆栈(添加esp,8).
因为cdecl调用约定允许在调用过程中修改eax,所以在PrintResult返回时可能不会保留eax,因此我们在调用PrintResult之前保存计算的结果,然后在调用返回后将其恢复.
我没有尝试上面的代码,但是我希望它可以帮助您正确地进行操作.
注意:因为您使用的是C编译器,所以在PrintResult之前需要extern“ C”.