diff options
| author | Fuwn <[email protected]> | 2023-04-11 14:29:45 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-11 14:29:45 -0700 |
| commit | 6226bcd7abfe8caeac8d976ffd8ac0727dddb6d7 (patch) | |
| tree | 6f798d62d667b0f268a3a4e8595d7561bd043bf1 | |
| parent | Initial commit (diff) | |
| download | marie-6226bcd7abfe8caeac8d976ffd8ac0727dddb6d7.tar.xz marie-6226bcd7abfe8caeac8d976ffd8ac0727dddb6d7.zip | |
feat: factorial implementation
| -rw-r--r-- | factorial.mas | 77 |
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 |