-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollatz.asm
131 lines (109 loc) · 965 Bytes
/
collatz.asm
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// collatz conjecture, until answer is 1,
// if odd multiply by 3 + 1
// if even divide by 2
// first number goes in RAM[32], and each number goes in the next address
@0
D=M
//init address pointer
@32
D=A
@next
M=D
// init starting address with *R0
@0
D=M
@next
A=M
M=D
(collatzify)
//odd or even
@next
A=M
// equal to 1?
@one
0;JMP
// odd or even
(odd_even)
@next
A=M
D=M
@1
M=A
D=D&M
@odd
D;JNE
@even
D;JEQ
(increment)
@next
M=M+1
A=M
M=D
@collatzify
0;JMP
//end of program
(end)
@end
0;JMP
//3n + 1 when number is odd
(odd)
@next
A=M
D=M
@num
M=D
//mult 3 == add 2 * num to num
@2
D=A
@mult_count
M=D
(mult_3)
@next
A=M
D=M
@num
M=M+D
@mult_count
M=M-1
D=M
@mult_3
D;JGT
//plus 1
@num
M=M+1
D=M
@increment
0;JMP
//half number, when number is even
(even)
@divisor
M=0
@next
A=M
D=M
@num
M=D
(half)
@2
D=A
@divisor
M=M+1
@num
M=M-D
D=M
@half
D;JGT
@divisor
D=M
@increment
0;JMP
@increment
0;JMP
(one)
@1
M=A
D=D-M
@end
D;JEQ
@odd_even
0;JMP