summaryrefslogtreecommitdiff
path: root/factorial.mas
diff options
context:
space:
mode:
Diffstat (limited to 'factorial.mas')
-rw-r--r--factorial.mas77
1 files changed, 77 insertions, 0 deletions
diff --git a/factorial.mas b/factorial.mas
new file mode 100644
index 0000000..01f7c00
--- /dev/null
+++ b/factorial.mas
@@ -0,0 +1,77 @@
+/ get user input and store it
+input
+store user_input
+
+/ only find the factorial if the number is non-zero
+skipcond 400
+jns find_factorial
+
+/ output the final factorial, or zero
+load factorial_result
+output
+
+halt
+
+/ initiate the factorial calculation
+find_factorial, load user_input
+subt one
+store counter
+
+load zero
+add counter
+store loop_counter
+
+skipcond 400
+jump calculate_factorial
+
+jumpi find_factorial
+
+/ calculate the factorial recursively
+calculate_factorial, load user_input
+add product
+store product
+store factorial_result
+
+load counter
+subt one
+store counter
+
+skipcond 400
+jump calculate_factorial
+
+jump reset_product
+
+reset_product, load product
+subt product
+store product
+
+load factorial_result
+store user_input
+
+jump decrement_loop
+
+decrement_loop, load loop_counter
+subt one
+store counter
+
+load zero
+add counter
+store loop_counter
+
+/ continue calculating the factorial if further depth exists
+skipcond 400
+jump calculate_factorial
+
+/ return to factorial calculation initialisation, a final stop
+jumpi find_factorial
+
+/ constants
+zero, hex 0
+one, dec 1
+
+/ mutable variables
+user_input, hex 0
+factorial_result, hex 0
+product, hex 0
+counter, hex 0
+loop_counter, hex 0