# primes:
# Calculate prime numbers.
# This program uses the rather long and boring process of simply
# testing a number to see if anything divides into it evenly.
line
print "How many to find"
in
store numPrimes
print "Working:"
line
# 2 is the first prime:
# bit of a cheat but we'll calculate the rest
load 2
out
load 3
bigLoop:
store candidate
jsr primeTest
jizero nextCandidate
load candidate
out
wait 1
load numPrimes
sub 1
store numPrimes
jizero done
nextCandidate:
load candidate
add 2 # All primes after 2 are odd
jump bigLoop
done:
line
halt
# primeTest:
# Entry: candiate is acc
# Exit: acc contains 1 if prime
# This is a brute-force way to work out if it has any divisors.
# (Although it's a reasonably clever way to get the remainer)
primeTest:
div 2 # Only check up to half the candidate
store divisor
nextTest:
sub 1 # If divisor got down to 1, then it's prime
jizero gotPrime
load candidate
div divisor
mul divisor
sub candidate # If zero, then it divided evenly = No Prime
jizero notPrime
load divisor
sub 1
store divisor
jump nextTest
notPrime:
load 0
ret
gotPrime:
load 1
ret