# Project: VAM (Virtual Assembler Machine) # Description: A VAM program the faculty function # Input: read a number # Output: n! = (n-1)! * n if n > 1 # 1! = 1 if n <= 1 # do it recursively # Author: Dr. Jürgen Vollmer, # $Id: fac.vam,v 1.1 2005/02/24 15:27:45 vollmer Exp $ s_in: "Please input an integer: " s_out: "! = " NL: "\n" N: DATA 1 # the read number start: write_s s_in read_i R4, R4 write_s NL store_l N, R4 push R1, R4 # call fac(n) --> R10 call R1, fac pop R1, 1 load_l R4, N write_i R4 write_s s_out write_i R10 write_s NL end s_fac1: "Start fac with: " s_fac2: "End fac result=: " fac: # fac (n) = (as described above) # n is passed on the stack # the result is returned in register R10 # write_s s_fac1 # write_i R10 # write_s NL load_c R10, R1, +1 # R10: n cmp_c R11, R10, 1 # if n <= 1 ifle R11, fac_base # then recursion base reached # else ... sub_c R10, r10, 1 # ... call fac(n-1) --> R10 push R1, R10 call R1, fac pop R1, 1 load_c R11, R1, +1 # R11: n mult_i R10, R10, R11 # R10: fac(n-1) * n goto fac_end fac_base: cload_i R10, 1 # result = 1 fac_end: # write_s s_fac2 # write_i R10 # write_s NL return R1