Trying to perform a factorial in assembly but causing stack overflow

喜你入骨 提交于 2019-12-13 03:24:47

问题


Ok, so I am trying to test out a factorial program from my college book, and when I type it correctly as shown, it gives me a stack overflow error.

It happens at the line push ebp. Can anyone tell me what is wrong?

.code
main PROC
    mov ebp,0
    push 3
    call Factorial
    call WriteDec
    call Crlf
    exit
main ENDP

Factorial PROC

    push ebp
    mov ebp,esp
    cmp eax,0
    ja L1
    mov eax,1
    jmp L2

    L1:
        dec eax
        push eax
        call Factorial

    ReturnFact:
        mov ebx,[ebp+8]
        mul ebx

    L2:
        pop ebp
        ret 4

Factorial ENDP

回答1:


It is just me or anyone else think that you missed a

mov eax, [ebp+8]

at the start of the function (after the prologue)? You are not getting the argument from the stack before comparing it with 0.




回答2:


Perhaps the book had a stack going up (though, this is unusual). At the moment, it subtracts from ebp every time something is pushed, because the stack actually grows down. (This seems a little counterintuitive, but that's how its done in most computers)

So, what's happening, is that you're pushing 3 onto the stack at address 0, subtracting 4 from ebp, and having MAX UNSIGNED INT - 3 now in ebp. Which is out of the stack, so "stack overflow" or, accessing something not in your stack.




回答3:


You probably meant to pop the 3 into eax, otherwise, eax is uninitialized.



来源:https://stackoverflow.com/questions/30987704/trying-to-perform-a-factorial-in-assembly-but-causing-stack-overflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!