在程序集文件內調用C++函數 [英] Calling C++ Functions Inside an Assembly File
本文介紹了在程序集文件內調用C++函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是給定的.cpp:
#include <iostream>
using namespace std;
enum ResultCode { ShowSquare, ShowMultiply, ShowDivide, ShowRemainder, ShowDivideFailure };
enum SuccessCode { Failure, Success };
extern "C" SuccessCode Divide(long, long, long &, long &);
extern "C" long Multiply(long, long);
extern "C" void PrintResult(ResultCode, long);
extern "C" long Square(long);
void main()
{
long Num1;
long Num2;
long Result;
long Remainder;
do
{
cout << "Enter Number to Square" << endl;
cin >> Num1;
Result = Square(Num1);
cout << "Square is: " << Result << endl;
cout << "Enter two numbers to multiply" << endl;
cin >> Num1 >> Num2;
Result = Multiply(Num1, Num2);
cout << "Result of multiply is: " << Result << endl;
cout << "Enter mumber to divide into then number to divide by" << endl;
cin >> Num1 >> Num2;
if (Divide(Num1, Num2, Result, Remainder) == Success)
cout << "Result is " << Result << " and remainder is " << Remainder << endl;
else
cout << "Attempted division by zero";
} while (Result > 0);
}
void PrintResult(ResultCode PrintCode, long Value)
{
switch (PrintCode)
{
case ShowSquare:
cout << "Display of square is: " << Value << endl;
break;
case ShowMultiply:
cout << "Display of multiply is: " << Value << endl;
break;
case ShowDivide:
cout << "Display of divide is " << Value << endl;
break;
case ShowRemainder:
cout << "Display of remainder is " << Value << endl;
break;
case ShowDivideFailure:
cout << "Display of Division by zero" << endl;
break;
default:
cout << "Error in assembly routines" << endl;
}
}
以下是我到目前為止所擁有的.asm文件:
.386
.model flat
.code
public _Square
public _Multiply
public _Divide
_Square proc
mov eax, [esp + 4]
imul eax, eax
push eax
push eax
push 0
call _PrintResult
add esp, 8
pop eax
ret
_Square endp
_Multiply proc
mov eax, [esp + 8]
mov ebx, [esp + 4]
imul eax, ebx
ret
_Multiply endp
_Divide proc
ret
_Divide endp
end
目前,my_Square函數具有我從此處的另一個答案中提取的內容,但不起作用。它告訴我PrintResult是未定義的。我已經寫出了我的_Multiply,但它當然沒有調用,一旦我知道這樣的格式化會是什么樣子,我就可以在_Divide中寫入。
任何幫助都不勝感激!
推薦答案
您可以使用EXTERN
告訴匯編者有關外部事物的信息。
EXTERN _PrintResult
在這種情況下,使用PROTO
和INVOKE
可能更好
MSDN : proto/invoke
PrintDisplay PROTO C arg1:SWORD, arg2:SWORD
后跟
INVOKE PrintDisplay 0, eax
這篇關于在程序集文件內調用C++函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!
查看全文