; factorial
; Rosemary Francis

; this program calculates r1 <= r0!
;			ie r1 <= 1 * 2 * ... * (r0)

; it dirties r0 r1 r2 r3 and r14

	
		

factorial
	movi r0 15
	mov r14 r15 	;save return address
	
	movi r1 1	; r1 = 1
	mov r3 r0	; r3 = r0

loop	bsr multiply 	; r0 = r1 * r0
	subi r3 1	; r3--
	mov r1 r3	; r1 = r3
	cmpi r3 0	; r3 != 0?
	ifs cc_ne
	br loop
	nop	
	br endfactorial
	nop

#include multiply.s;

endfactorial
	mov r15 r14
	ret 
	nop
