在汇编

时间:2017-04-12 21:49:31

标签: c++ visual-studio assembly macros

我在完成涉及添加宏的作业时遇到问题。在将Param1和Param2推入M5(第五个宏)作为分割函数后,我在运行它后遇到了错误的结果和余数:"结果为4,余数为-858993460。&#34 ;除了我到目前为止所做的工作,我不知道为完成该计划还需要采取哪些其他措施。

以下是我为作业提供的说明: "修改练习七,以便为每个调用创建一个宏。宏的参数将是:函数名称,参数1,参数2等。宏将允许最多五个参数,但如果全部传递给它,将使用更少的参数。还为每个函数的启动创建宏(这是一个用于执行push ebp和mov ebp,esp语句的宏)以及用于从每个函数返回的宏(pop ebp和ret语句)。"

这是Main.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 a mumber to divide into, then a number to divide by. " << endl;
        cin >> Num1 >> Num2;
        if (Divide(Num1, Num2, Result, Remainder) == Success)
            cout << "The result is " << Result << ", and the 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.asm文件:

.386

.model flat

public _Square
public _Multiply
public _Divide
extern  _PrintResult:proc

Param1 = 4          ; creates a named constant, only works with whole numbers

Param1 = 8          ; names can be redefined
Param2 = 16
Param3 = 24
Param4 = 32
Param5 = 40

M1      macro
        push    ebp
        mov     ebp, esp
        endm

M2      macro   P1
        pop     P1
        ret
        endm

M3      macro   P1:REQ, P2          ;; :REQ means the parameter is required
IFB <P2>
P1      BYTE    0, 1, 2, 3, 4, 5
ELSE
P1      P2      0, 1, 2, 3, 4, 5
ENDIF
        endm

M4      macro   P1, P2
LOCAL   L1, L2
        cmp     P1, P2
        jl      L1
        inc     P1
        jmp     L2
L1:     inc     P2
L2:
        endm

M5      macro   Param1, Param2
        push [Param1]
        push [Param2]
    ;   push [Param3]
    ;   push [Param4]
    ;   push [Param5]
        Call    Param1
        Call    Param2
    ;   Call    Param3
    ;   Call    Param4
    ;   Call    Param5
        add ebp, 8
        endm

.code

_Square proc
       M1
       finit
       mov eax, [ebp + 8]
       imul eax
       M2 ebp
       ret
_Square endp

_Multiply proc
       M1
       finit
       mov eax, [ebp + 8]
       mov ebx, [ebp + 12]
       imul ebx
       M2 ebp
       ret
_Multiply endp

_Divide proc
       M1
       finit
       mov eax, [ebp + 8]
       mov ebx, [ebp + 12]
       mov ecx, [ebp + 16]
       mov edx, 0
       idiv ebx
       mov ecx, eax
       mov ebx, [ebp + 20]
       mov ebx, edx
    ;   M5 ebp, esp
    ;   Call    _PrintResult
    ;   add ebp, 8
       M2 ebp
       M5 ebp, esp
       ret
_Divide endp

.data

db  255

end

0 个答案:

没有答案