;finonacci
; Rosemary Francis


;f(n) = f(n-1) + f(n-2)
;given an index in r0 calculates the correstponding fibonacci number and places it in r1

; it dirites r0, r1, r2, r3, r4 and r14

fib
	mov r14 r15	;save return address

	movi r1 0 	;n
	movi r2 1 	;n-1
	movi r3 1	;n-2

	cmpi r0 0
	ifs cc_le
	br endfib 	;end if n<=0
	nop
	nop

	movi r1 1

	cmpi r0 1
	ifs cc_eq
	br endfib	;n=1

	cmpi r0 2
	ifs cc_eq
	br endfib 	;n=2

	movi r1 0
	movi r4 2

getNext	
	mov r1 r2	; places the next fibonacci number in r1
	add r1 r3
	mov r3 r2
	addi r4 1
	cmp r4 r0
	ifs cc_n
	br getNext

endfib	mov r2 r1	
	mov r15 r14
	ret
	nop
