汇编Fibonacci程序中抛出的未知错误

时间:2011-04-12 02:52:51

标签: assembly recursion fibonacci

昨天我在Assembly中发布了一个关于我的Recursive Fibonacci程序的问题。我现在得到了正确的输出,感谢这里精彩人士的一些帮助,但是在打印出正确的输出之后,我的程序崩溃了。

以下是调用Fibonacci程序给定次数的Sequence程序(存储在L中)

.386
.model Flat
public Sequence
extrn Fibonacci:proc
include iosmacros.inc
.code
Sequence proc
    MOV L, EAX
    XOR ECX, ECX ;start count at 0
    sequ:
    CMP ECX, L
        JA endseq ;if we have passed the given value (L), end
        putstr MsgOut1
        putint ECX ;number passed to Fibonacci
        putstr MsgOut2
        MOV count, ECX ;preserve count
        PUSH ECX
        CALL Fibonacci ;call Fibonacci
        putint ECX
        putch ' '
        MOV ECX, count ;restore count
        INC ECX ;increment the count
        JMP sequ ;again
    endseq:
        putint ecx
    ret
Sequence endp

.data
    MsgOut1 DB "Fib(", 0, 0 ;first half of output message
    MsgOut2 DB ") = ", 0, 0 ;second half of output message
    L DD, 0, 0 ;number of sequences to carry out
    count DD 0,0 ;for counting
end

以下是调用Sequence过程的代码:

.386
.model flat
extrn Sequence:proc
include Cs266.inc
.data
    Msg DB "Please input the number of sequences you would like carried out", 0Ah, 0 ;input request message
    err DB "reached end"

.code
include Rint.inc
    main:
        putstr Msg
        CALL Rint ;store int in EAX
        CALL Sequence
                putstr err
    ret

end

Fibonacci代码如下:

.386
.model Flat
public Fibonacci
include iosmacros.inc ;includes macros for outputting to the screen

.code
Fibonacci proc

    MOV ECX, [ESP+4]
    CMP ECX, 1
        JA Recurse
        MOV ECX, 1 ;return value in ECX
        JMP exit

Recurse:
    PUSH EBX ;preserve value of EBX
    DEC ECX
    PUSH ECX
    CALL Fibonacci
    MOV EBX, ECX ;EBX is preserved, so safe to use
    DEC [ESP] ;decrement the value already on the stack
    CALL Fibonacci
    ADD ECX, EBX ;return value in ECX
    ADD ESP, 4 ;remove value from stack
    POP EBX ;restore old value of EBX
exit:
ret
Fibonacci endp

.data
end

我在这里发布了一堆代码,但这只是为了方便我指出正确的方向。我相信问题可能在序列中,而我的调试器对我没有帮助。

编辑:我得到的所有错误都是这样的: http://imgur.com/XulTl 如果我确实启用了Visual Studio实时调试,那么它永远不会有帮助。

1 个答案:

答案 0 :(得分:1)

嗯...在Fibonacci中,我看到两个push es,只有一个pop。至少乍一看,这似乎有点问题。