-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3n+1.asm
More file actions
40 lines (38 loc) · 1.19 KB
/
Copy path3n+1.asm
File metadata and controls
40 lines (38 loc) · 1.19 KB
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
; 3n+1 problem in stack S16 assembly
; given a number N, repeat until you reach 1:
; if N is even, replace with N/2
; if N is odd, replace with 3N + 1
; put all the numbers encountered on the way in data memory
; starting at location 1, with location 0 used as a pointer
; an optional interrupt occurs before every number in the sequence
PUSH array ; initialise data pointer to array (1)
STOR index
PUSH #7 ; the original N
start: DUP ; store in data memory
LOAD index
STRS
LOAD index ; increase pointer in data[0]
PUSH #1
ADD
STOR index
; INT ; interrupt - enable if needed
DUP ; make a copy for testing if it's 1
PUSH #1
CEQ
JT end
DUP ; make a copy for the even test
PUSH #1
AND
JT odd
PUSH #1
SHR ; it's even, divide by 2
J start
odd: PUSH #3
MUL
PUSH #1
ADD
J start
end: HALT
.DATA
index:
array: