MASM递归模拟乘法

时间:2017-03-30 23:49:00

标签: recursion assembly masm

我在“Call CalcProd”位置收到堆栈溢出错误(在函数本身内,而不是在main中)。知道为什么吗?

以下是相关的变量声明:

Num1 DWORD 12
Num2 DWORD 7
Prod DWORD 0

这是我的函数链接并在Main中调用:

;----------------------CalcProd LINKAGE------------------
PUSH Num1
PUSH Num2
PUSH OFFSET Prod
CALL CalcProd
;--------------------------------------------------------

这是我的功能:

;-------------------------CalcProd-------------------------
;calculates the product of the Num1 * Num2
;e.g. CalcProd(5,3) = 5+5+5 = 15
;params: Num1 and Num2 are passed in on the stack
;output: the product is put into the Prod variable
;------------------------------------------------------------

CalcProd proc

    N1 EQU 16[ebp]
    N2 EQU 12[ebp]
    P EQU 8[ebp]

    ;necessary
    PUSH ebp
    MOV ebp,esp

    MOV ecx,N2
    ;if (Num2 == 0)
    CMP ecx,0
    JE baseCase

    ;else return CalcProd(Num1,Num2-1)
    DEC ecx
    ;PUSH ecx ; Num2-1
    MOV N2,ecx
    CALL CalcProd

    ;adds number to itself here
    MOV esi,P
    MOV eax,N1
    ADD [esi],eax

baseCase:
    MOV esp,ebp
    POP ebp
    ret 12

CalcProd endp

;the fix:
;else return CalcProd(Num1,Num2-1)
;DEC ecx
;PUSH Num1
;PUSH ecx
;PUSH OFFSET Prod
;CALL CalcProd

0 个答案:

没有答案