I'm creating a fibonacci program in mic1, which is a little more readable than other assembly languages so even if you don't know the language it shouldn't be hard to figure out what the code is trying to do if you are good with stacks and recursion.

This is the code I have come up with for my best attempt after many other failed attempts. It is still not coming up with the correct answer and I am getting quite frustrated. Any help would be appreciated, even if you can't suggest code. I just kind of need to be pointed in the right direction, or where I am going wrong.

This is also not the whole program, it is just the part which I believe is going wrong.

``````fib:	LODL 1          	; ac = top of stack
JZER done0:	        ; if ac = 0 then return 0
SUBD one:               ; ac != 0 ac = ac-1
JZER done1:	        ; if ac = 0 now then return 1
PUSH                    ; push ac onto stack
CALL fib:		; call fib(n-1)
POP                     ; pop most recent return value off stack into ac
ADDD count:             ; ac = ac + count
STOD count:             ; count = ac
LODL 1                  ; load top of stack into ac
SUBD two:               ; ac = ac-2
PUSH                    ; push (n-2) onto stack
CALL fib:	        ; call fib(n-2)
POP                     ; pop most recent return value off stack into ac
ADDD count:             ; ac = ac + count
PUSH                    ; push ac onto stack
RETN                    ; return value

done0:	PUSH                    ; push ac onto stack (ac = 0)
RETN                    ; return 0
HALT

done1:  LODD one:               ; load value of 1 into ac
PUSH                    ; push ac onto stack
RETN                    ; return 1
HALT

one:	1                       ; variable one = 1
two:	2                       ; variable two = 2
count:	0                       ; variable count starts at 0``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.