From 6226bcd7abfe8caeac8d976ffd8ac0727dddb6d7 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 11 Apr 2023 14:29:45 -0700 Subject: feat: factorial implementation --- factorial.mas | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 factorial.mas 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 -- cgit v1.2.3