# Last modified on Wed Nov 4 17:46:34 PST 1992 by heydon # # This program reads integers from the standard input, and then writes out # their factorials. The program exits when the user inputs a number <= 0. # PROCEDURE y := Factorial(x) IS # IF x <= 1.0 -> y := 1.0 # | y := x * Factorial(x - 1.0) # FI # END # # cont, i :: # cont := 1.0 # ; DO Print("> ") # ; i := Atof(Readln()) # ; Print(Unparse(i)) # ; Print("! = ") # ; Print(Unparse(Factorial(i))) # ; Print("\n") # OD # # (The loop exits when Readln() is undefined). # Currently, this program supplies bogus "undefined" offsets for the # instructions that require them. # Global variables V0 0.0 V1 1.0 V2 "> " V3 "! = " V4 "\n" V5 "\n" # Factorial() C1 # x = fp[-1] (IN parameter) # y = fp[-2] (OUT parameter) # x <= 1.0? .PUSHL s-1 .PUSHG c1 .AT-MOST .TJUMP s29 # y := x * Factorial(x - 1.0) .PUSHL s-1 .INCSP b1 .PUSHL s-1 .PUSHG c1 .SUBTRACT s0 .CALL c1 .DECSP b1 .MULTIPLY s0 .JUMP s5 .PUSHG c1 .POPL s-2 .RET # Main Loop C2 # i = fp[1] # t = fp[2] .INCSP b2 .PUSHG c2 .CALLEXT c0 .DECSP b1 # t := Readln() .INCSP b1 .CALLEXT c1 .POPL s2 .TJUMP s13 .PUSHG c5 .CALLEXT c0 .DECSP b1 .RET # i := Atof(t) .INCSP b1 .PUSHL s2 .CALLEXT c3 .DECSP b1 .POPL s1 # Print(Unparse(i)) .INCSP b1 .PUSHL s1 .CALLEXT c2 .DECSP b1 .CALLEXT c0 .DECSP b1 # Print("! = ") .PUSHG c3 .CALLEXT c0 # DECSP (remove space for IN) # INCSP (make space for OUT) # t : = Factorial(i) .PUSHL s1 .CALL c1 .DECSP b1 .POPL s2 # t := Unparse(t) .INCSP b1 .PUSHL s2 .CALLEXT c2 .DECSP b1 # POPL 2 (store result in t) # PUSHL 2 (push IN argument) # Print(t) .CALLEXT c0 .DECSP b1 # Print("\n") .PUSHG c4 .CALLEXT c0 .DECSP b1 # Jump to top of loop .JUMP s-129 END 0 1 2 2.5 3 4 5 10 30 34 35 1000