;; This program reads in a nonnegative integer n and prints n!. allocate-registers sp n cont fact factorial after test zero one else afterRec li zero 0 li one 1 li fact factL li after afterL li else elseL li afterRec afterRecL li sp 0 read n li cont afterL j fact afterL: write factorial halt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; input: n ;; return value: factorial ;; return address: cont ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; factL: li factorial 1 seq test n zero jeqz test else j cont elseL: ;; save context st n sp add sp sp one st cont sp add sp sp one ; factorial <- fact(n-1) sub n n one li cont afterRecL j fact afterRecL: ;; retrieve context sub sp sp one ld cont sp sub sp sp one ld n sp ; factorial <- factorial * n mul factorial factorial n ; return j cont