-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_for_prime.ae
102 lines (92 loc) · 2.09 KB
/
check_for_prime.ae
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
. Checks whether a number is prime. The example value (203) celebrates
. the 203rd anniversary of Ada's 1815 birth in 2018.
N001 203 the number to be checked
N000 0 constant 0 - actually the default
N002 1 constant 1
N003 2 constant 2
N004 3 starting test divisor
. column 10 is used as a scratch register; it must be zero before
. storing into it, so always load using Z10
A write in columns
. if target number is < 4, it's prime
+ add 1 to 3, makes 4
L004
L002
S010
- subtract 4 from target number
L001
Z010 leave column 10 zero
CF?1 skip if sign changed (target < 4)
CF+9 continue
A write numbers as ##9
+ report the target is prime
L001
L000
P
A write annotation is prime
A write new line
H
. if target number is divisible by 2, it isn't prime
/
L001
L003
S010 store remainder
- check for zero
Z010 load remainder, leaving column 10 zeroed
L002 subtract 1
CF?1 skip to print/halt if sign changed (i.e. was zero)
CF+8 continue to check next divisor
A write numbers as ##9
+ print the target number
L001
L000
P
A write annotation is divisible by 2
A write new line
H
. main loop
/ divide target by test value
L001
L004
S010 store remainder (column 10 must be zero previously)
- check for zero
Z010 load remainder, leaving column 10 zeroed
L002 subtract 1
CF?1 skip to print/halt if sign changed (i.e. was zero)
CF+15 continue to check next divisor
. divisor found!
A write numbers as ##9
+ print the target number
L001
L000
P
A write annotation is divisible by
A write numbers as ##9
+ print the divisor
L004
L000
P
A write new line
H
+ check next possible factor
Z004 load current candidate, clear store
L003 add 2
S004 store result
* square the candidate
L004
L004
S010
-
L001 load target
Z010 load (candidate squared), leaving column 10 zeroed
CF?1 skip if sign changed (i.e. no divisor found)
CB+39 back to main loop
. no divisor found!
A write numbers as ##9
+ report the target is prime
L001
L000
P
A write annotation is prime
A write new line
H