我正在上课,在完成任务之前,我有最后一部分作业。我得到了一个我不允许编辑的C ++文件,我正在编写.asm文件。我在项目中的最后一个障碍是: 1.当给定2个常规值时,我的_Divide函数应该访问枚举,但我不知道如何调用它而不是意外地击中_PrintResult。 2.除以0时,我可以显示“除以零显示”,但不能显示“尝试除零”。
这是.cpp: 包括
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;
}
}
这是我到目前为止所做的:
.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