57
|
1 ; ERATOSTHENES SIEVE PRIMES
|
|
2 ; BYTE MAGAZINE 9/1981 BENCHMARK
|
|
3 ; Adapted by Johann Klasek, j AT klasek at
|
|
4 ; Previously implemented for a Dragon 32,
|
|
5 ; later also for a the sim6809 simulator.
|
|
6 ;
|
|
7 org $c000
|
|
8
|
|
9 FLAG EQU $5000 ; array of bytes, length SIZE
|
|
10 SIZE EQU $2000
|
|
11
|
|
12 START
|
|
13
|
|
14 lds #FLAG ; stack below flags array
|
|
15
|
|
16 ; lda #$42
|
|
17 ; jsr >$b54a ; char out Dragon Basic
|
|
18 ldb #'B
|
|
19 swi2
|
|
20
|
|
21 lda #$0a
|
|
22 pshs a
|
|
23
|
|
24 ITER ldx #FLAG ; array
|
|
25 ldu #$ffff ; filled with
|
|
26 ldd #(SIZE/2) ; words
|
|
27 CLEAR stu ,x++ ; word fill
|
|
28 decb ; byte decrement works only
|
|
29 bne CLEAR ; low byte of count is 0
|
|
30 deca
|
|
31 bne CLEAR
|
|
32
|
|
33 leau 1,u ; prime counter to 0
|
|
34 ldy #FLAG ; array
|
|
35
|
|
36 PRIMES tst ,y+ ; is prime?
|
|
37 beq NPRIME
|
|
38 leax -1,y ; prime found
|
|
39 tfr x,d
|
|
40 suba #(FLAG>>8)
|
|
41 lslb
|
|
42 rola
|
|
43 addd #3 ; prime = step
|
|
44 bra STEP
|
|
45
|
|
46 NMARK clr ,x ; mark all non-primes
|
|
47 STEP leax d,x ; step to next
|
|
48 cmpx #(FLAG+SIZE)
|
|
49 bcs NMARK
|
|
50
|
|
51 leau 1,u ; count primes
|
|
52 NPRIME cmpy #(FLAG+SIZE)
|
|
53 bcs PRIMES
|
|
54
|
|
55 ldb #'.
|
|
56 swi2 ; print
|
|
57 ; lda #$2e
|
|
58 ; jsr >$b54a ; char out Dragon Basic
|
|
59
|
|
60 dec ,s
|
|
61 bne ITER
|
|
62
|
|
63 puls a ; drop counter
|
|
64 pshs u ; store count on stack
|
|
65 ; rts
|
|
66 sync
|
|
67
|