summaryrefslogtreecommitdiff
path: root/factorial.mas
blob: 01f7c0078fe8455798a1f0d20276e4cf18a2a0be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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