Unknown Error Thrown in Assembly Fibonacci Program
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 cra开发者_开发知识库shes.
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: http://imgur.com/XulTl And if I do enable Visual Studio Just-In-Time debugging, it never helps.
Hmm...in your Fibonacci
, I see two push
es and only one pop
. At least at first glance, that seems like a little bit of a problem.
精彩评论