汇编斐波那契数列中引发未知错误

发布于 2024-11-01 08:12:23 字数 2234 浏览 1 评论 0原文

昨天我在 Assembly 中发布了一个关于我的递归斐波那契程序的问题。我现在得到了正确的输出,这要感谢这里出色的人们的帮助,但是在打印正确的输出后,我的程序立即崩溃了。

这是调用斐波那契程序给定次数的序列程序(存储在 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

这是调用序列程序的代码:

.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

斐波那契代码如下:

.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

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

编辑:我得到的所有错误是: https://i.sstatic.net/cqhcL.jpg 如果我确实启用了 Visual Studio 即时调试,也没有任何帮助。

Yesterday I posted a question about my Recursive Fibonacci program in Assembly. I'm now getting the proper output, thanks to some help from the wonderful folks here, however immediately after the correct output is printed, my program crashes.

Here is the Sequence program that calls the Fibonacci program a given number of times (stored in 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

And here is the code that calls the Sequence procedure:

.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

The Fibonacci code is as follows:

.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

I've posted a bunch of code here, but it is just for your convenience in pointing me in the right direction. I BELIEVE the problem might be in Sequence, and my debugger does not help me.

EDIT: All I get in terms of an error is this:
https://i.sstatic.net/cqhcL.jpg
And if I do enable Visual Studio Just-In-Time debugging, it never helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-11-08 08:12:23

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

Hmm...in your Fibonacci, I see two pushes and only one pop. At least at first glance, that seems like a little bit of a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文