The Prime Numbers
A prime number is only divisible by itself and one.
Our method is to take each number 2,3,4,5,6,7,8,9,10,11,...
and test it for divisibility by 2,3,5,7,... up to the square root of the number.
e.g. if we are testing 47 we would try 2,3,5 and 7.
We know to stop at 7 because 47 / 7 = 6 remainder 5.
Since 7 > 6 (the quotient Q) we must have passed the square root.
This is a great trick and makes the program much faster.

 LDA #2 start with two
 STA X
MORE: JSR TEST test whether X is prime
 BEQ NEXT is not prime
 OUT X is prime
NEXT: INC X try next number
 JMP MORE

Is X prime? 1=Yes 0=No in the accumulator
TEST: LDA X
 STA M
 LDA #2
 STA N
 JSR DIV test divisibilty by 2
 LDA R
 BEQ TWO
 LDA #1 is not divisible by 2: try 3,5,7,... until > Q
 STA TRY
LOOP: INC TRY
 INC TRY
 LDA TRY
 STA N
 SUB Q
 STA Q
 BPL YES we have tried 2,3,5,7,...,Q with no success so must be prime
 JSR DIV not up to Q yet ... keep trying
 LDA R
 BEQ NO ah but we have a zero remainder so NOT prime
 JMP LOOP
TWO: LDA X  is divisible by 2
 SUB #2 check whether it is 2
 BEQ YES
NO: LDA #0 say "not prime"
 RTS
YES: LDA #1 say "prime"
 RTS

divide M by N and leave the quotient in Q and the remainder in R
DIV: LDA #0
 STA Q
 LDA M
DIV1: SUB N
 STA R
 BMI DIV2
 INC Q
 JMP DIV1
DIV2: ADD N
 STA R
 RTS

----------------------------------------
here is the code without comments:
----------------------------------------
LDA #2
STA X
MORE: JSR TEST
BEQ NEXT
OUT X
NEXT: INC X
JMP MORE
TEST: LDA X
STA M
LDA #2
STA N
JSR DIV
LDA R
BEQ TWO
LDA #1
STA TRY
LOOP: INC TRY
INC TRY
LDA TRY
STA N
SUB Q
STA Q
BPL YES
JSR DIV
LDA R
BEQ NO
JMP LOOP
TWO: LDA X
SUB #2
BEQ YES
NO: LDA #0
RTS
YES: LDA #1
RTS
DIV: LDA #0
STA Q
LDA M
DIV1: SUB N
STA R
BMI DIV2
INC Q
JMP DIV1
DIV2: ADD N
STA R
RTS

Assembler