昨天我在 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
我在这里发布了一堆代码,但这只是为了方便您为我指明正确的方向。我相信问题可能出在序列中,而我的调试器对我没有帮助。
编辑:就错误而言,我得到的只是:http: //imgur.com/XulTl 如果我确实启用了 Visual Studio Just-In-Time 调试,它永远不会有帮助。