Arcadiaen
Arcadiaen

Reputation: 33

Calling C++ Functions Inside an Assembly File

This is my first time asking so I am going to do my best here, but I am having trouble on an assembly assignment in which I am given a .cpp file (below) and have to make an .asm file that contains a Multiply, Square, and Divide functions. The part I am having an issue with is that after the Square and Multiply functions are done, I have to call PrintResult to display the answer. For the Division, a successful divide returns a 1, and an unsuccessful one returns a 0 and call PrintResult to display the failure message. I am not allowed to edit his .cpp file, but I don't know how to successfully call PrintResult. I keep getting an error that says that there is an undefined symbol.

Here is the .cpp given:

#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;
    }
}

And here is what I have so far as an .asm file:

.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

Currently my _Square function has what I pulled from another answer on here, but does not work. It tells me that PrintResult is undefined. I have my _Multiply written out but it does not have the call of course and I can write in the _Divide as soon as I know how formatting something like that would look..

Any help is more than appreciated!

Upvotes: 2

Views: 1777

Answers (1)

mksteve
mksteve

Reputation: 13073

You can use EXTERN to tell the assembler about external things.

EXTERN _PrintResult

In this case, you may be better using PROTO and INVOKE MSDN : proto/invoke

 PrintDisplay PROTO C arg1:SWORD, arg2:SWORD

followed by

 INVOKE PrintDisplay 0, eax

Upvotes: 1

Related Questions