I am currently enrolled in an assembly language (Motorola 68K Assembler) course. I have a project where I am tasked with printing the result of a Fibonacci number of a number up to 30. For example, if the user were to enter 4, the result should be 3 (since it is the sum of the previous two numbers). However, my main program (prog4.s) continuously prints out 0. Does the problem have anything to do with the logic of the recursive method? Does the problem lie elsewhere? Here is my code of the recursive method (fib.s)
* fib.s
* long fib(int n) {
* if(n==0) {
* return 0;
* }
* else if(n==1) {
* return 1;
* }
* return fib(n-1) + fib(n-2);
* }
ORG $7000
fib:
link A6,#0
movem.l D1-D2,-(SP)
move.w 8(A6),D1
TST.w D1
BNE next
BRA out
next:
cmpi.w #1,D1
BNE recurse
add.w #1,D0
BRA out
recurse:
move.w D1,D2
subq.w #1,D1
move.w D1,-(SP)
JSR fib
move.w D0,D1 * save copy of fib(n-1)
adda.l #2,SP
subq.w #2,D2
move.w D2,-(SP)
JSR fib
add.w D2,D1
add.w D1,D0 * return fib(n-1) + fib(n-2)
adda.l #2,SP
out:
movem.l (SP)+,D1-D2
unlk A6
rts
end
Here is my code for the program that calls fib.s
fib: EQU $7000
start: initIO
setEVT
lineout title
lineout prompt
linein buffer
cvta2 buffer,D1
* Place parameter on the stack and move the stack pointer
move.w D1,-(SP)
*Jump to the fib subroutine
JSR fib
*Pop starting parameter off the stack
adda.l #2,SP
cvt2a buffer,#6
stripp buffer,#6
lea buffer,A0
adda.l D0,A0
clr.b (A0)
lineout answer
break
prompt: dc.b 'Enter a number between 1 and 30: ',0
answer: dc.b 'The Fibonacci number is: '
buffer: ds.b 80
end
Something to note: The algorithm commented out in fib.s is the one I am required to use. Any help/advice would be much appreciated.