在程序集文件内调用C++函数

2022-09-05 00:00:00 assembly visual-c++ c++ x86 masm
这是我第一次问,所以我将在这里尽我最大的努力,但我在一项汇编任务中遇到了麻烦,在该任务中,我获得了一个.cpp文件(如下所示),并且必须创建一个包含Multiply、Square和Divide函数的.asm文件。我有一个问题,那就是在平方和乘法函数完成后,我必须调用PrintResult来显示答案。对于Divide,成功的除法返回1,不成功的除法返回0并调用PrintResult以显示失败消息。我不被允许编辑他的.cpp文件,但我不知道如何成功调用PrintResult。我一直收到一个错误,说有一个未定义的符号。

这是给定的.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
在这种情况下,使用PROTOINVOKE可能更好 MSDN : proto/invoke

 PrintDisplay PROTO C arg1:SWORD, arg2:SWORD

后跟

 INVOKE PrintDisplay 0, eax

相关文章