I am working on an assignment for class and I have one last part of the assignment before I am done. I am given a C++ file that I am not allowed to edit, and I am writing the .asm for it. My last snag in the project is that: 1. My _Divide function when given 2 regular values should access the enum, but I do not know how to call to it and not hit _PrintResult by accident. 2. When dividing by 0, I can get it to display the "Display of Division by zero" but not the "Attempted division by zero."
Here is the .cpp: include
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:
.386
.model flat
.code
public _Square
public _Multiply
public _Divide
extern _PrintResult:proc
ShowSquare = 0
ShowMultiply = 1
ShowDivideFailure = 4
Failure = 0
Success = 1
_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
push eax
push eax
push 1
call _PrintResult
add esp, 12
mov eax, [esp + 8]
mov ebx, [esp + 4]
imul eax, ebx
ret
_Multiply endp
_Divide proc
Start1:
mov eax, [esp+4]
mov ebx, [esp+8]
cmp ebx,0
je Failure1
cdq
idiv ebx
mov ebx, edx
jmp Success1
Success1:
push eax
push eax
push 2
call _PrintResult
add esp, 8
pop eax
push ebx
push ebx
push 3
call _PrintResult
add esp, 8
pop ebx
push eax
push eax
push 0
call _SuccessCode
Failure1:
ret
_Divide endp
end