
 1000         .LIF
 1010  *---------------------------------
 1020  *      SIEVE PROGRAM:
 1030  *      CALCULATES FIRST 1899 PRIMES IN 1.39 SECONDS!
 1040  *
 1050  *      INSPIRED BY JIM GILBREATH, BYTE, 9/81
 1060  *
 1070  *      WRITTEN BY WILLIAM ROBERT SAVOIE
 1080  *                 4405 DELASHMITT RD. APT 15
 1090  *                 HIXSON, TENN  37343
 1100  *---------------------------------
 1110  BUFF   .EQ $3500    START OF BUFFER (#BUFF=0)
 1120  SIZE   .EQ 8189     SIZE OF FLAG ARRAY
 1130  *---------------------------------
 1140  *      PAGE-ZERO VARIABLES
 1150  *---------------------------------
 1160  INDEX  .EQ $06      PAGE ZERO INDEX (LOCATION FOR I)
 1170  PRIME  .EQ $08      PRIME LOCATION
 1180  KVAR   .EQ $19      K VARIABLE 
 1190  CVAR   .EQ $1B      COUNT OF PRIME
 1200  ARRAY  .EQ $1D      ARRAY POINTER
 1210  SAVE   .EQ $1F      COUNT LOOP
 1220  *---------------------------------
 1230  *      ROM ROUTINES
 1240  *---------------------------------
 1250  HOME   .EQ $FC58    CLEAR VIDEO
 1260  CR     .EQ $FD8E    CARRIAGE RETURN
 1270  LINE   .EQ $FD9E    PRINT "-"
 1280  PRINTN .EQ $F940    PRINT 2 BYTE NUMBER IN HEX
 1290  BELL   .EQ $FBE2    SOUND BELL WHEN DONE
 1300  *---------------------------------
 1310  * RUN PROGRAM 100 TIMES FOR ACCURATE TIME MEASUREMENTS!
 1320  *---------------------------------
 1330  START  JSR HOME     CLEAR SCREEN
 1340         JSR CR       CARRIAGE RETURN
 1350         LDA #100     LOOP 100 TIMES
 1360         STA SAVE     SET COUNTER
 1370  .01    JSR GO       RUN PRIME
 1380         DEC SAVE     DECREASE SAVE
 1390         BNE .01      LOOP
 1400         JSR PRINT    PRINT COUNT
 1410         JSR BELL     READ WATCH!
 1420         RTS
 1430  *---------------------------------
 1440  *      RESET VARIABLES
 1450  *---------------------------------
 1460  GO     LDY #00      CLEAR INDEX
 1470         STY CVAR     CLEAR COUNT VARIABLE
 1480         STY CVAR+1   HI BYTE TOO
 1490         STY INDEX    CLEAR INDEX
 1500         STY INDEX+1  HI BYTE TOO
 1510         STY ARRAY    LOW BYTE OF ARRAY
 1520         LDA /BUFF    GET BUFFER LOCATION
 1530         STA ARRAY+1  SET ARRAY POINTER
 1540         LDA #$01     LOAD WITH ONE
 1550         LDX /SIZE    LOAD STOP BYTE
 1560         INX          MAKE PAGE LARGER
 1570  *---------------------------------
 1580  *      SET EACH ELEMENT IN ARRAY TO ONE
 1590  *---------------------------------
 1600  SET    STA (ARRAY),Y  SET MEMORY
 1610         DEY          NEXT LOCATION
 1620         BNE SET      GO 256 TIMES
 1630         INC ARRAY+1  MOVE ARRAY INDEX
 1640         DEX          TEST END
 1650         BNE SET      GO TELL END
 1660  
 1670  * SET ARRAY INDEX AT START OF BUFFER
 1680         LDA #BUFF    SET BUFFER LOCATION
 1690         STA ARRAY    IN ARRAY POINTER LOW
 1700         LDA /BUFF    SET BUFFER LOCATION
 1710         STA ARRAY+1  IN ARRAY POINTER
 1720         JMP FORIN    ENTER SIEVE ALGORITHM
 1730  
 1740  * SCAN ENTIRE ARRAY AND PROBAGATE LAST PRIME
 1750  FORNXT INC INDEX    INCREASE LOW BYTE
 1760         BNE FORIN    GO IF < 256
 1770         INC INDEX+1  INCREASE HI BYTE
 1780  FORIN  LDA INDEX    GET INDEX TO ARRAY
 1790         CLC          READY ADD
 1800         STA ARRAY    SAVE LOW BYTE
 1810         LDA INDEX+1  GET HI BYTE
 1820         ADC /BUFF    ADD BUFFER LOCATION
 1830         STA ARRAY+1  SET POINTER
 1840         LDY #00      CLEAR Y REGISTER
 1850         LDA (ARRAY),Y  GET ARRAY VALUE 
 1860         BEQ FORNXT   GO IF FLAG=0 SINCE NOT PRIME
 1870  * CALCULATE NEXT PRIME NUMBER WITH P=I+I+3
 1880         LDA INDEX    MAKE P=I+3
 1890         ADC #03      ADD THREE
 1900         STA PRIME
 1910         LDA INDEX+1
 1920         ADC #00      ADD CARRY
 1930         STA PRIME+1
 1940  * NOW P=I+3
 1950         LDA PRIME
 1960         ADC INDEX    MAKE P=P+I 
 1970         STA PRIME
 1980         LDA PRIME+1
 1990         ADC INDEX+1  ADD HI BYTE
 2000         STA PRIME+1  SAVE P
 2010  
 2020  * NOW CALCULATE K=I+PRIME (CLEAR BEYOND PRIME)
 2030         LDA INDEX    ADD I TO P
 2040         ADC PRIME
 2050         STA KVAR     SAVE IN K
 2060         LDA INDEX+1
 2070         ADC PRIME+1  ADD HI BYTE TOO
 2080         STA KVAR+1   SAVE K VALUE
 2090  
 2100  * SEE IF K > SIZE AND MODIFY ARRAY IF NOT
 2110  .02    LDA KVAR     GET K VAR
 2120         SEC          SET CARRY FOR SUB
 2130         SBC #SIZE    SUBTRACT SIZE
 2140         LDA KVAR+1   GET HI BYTE
 2150         SBC /SIZE    SUBTRACT TOO
 2160         BCS .03      GO IF K < SIZE
 2170  * ASSIGN ARRAY(K)=0 SINCE PRIME CAN BE ADDED TO MAKE NUMBER
 2180  * THEREFORE THIS CANNOT BE PRIME! (PROBAGATE THROUGH ARRAY)
 2190         LDA KVAR     GET INDEX TO ARRAY
 2200         STA ARRAY    SAVE LOW BYTE
 2210         LDA KVAR+1   GET HI BYTE
 2220         ADC /BUFF    ADD BUFFER OFFSET
 2230         STA ARRAY+1  SAVE ARRAY INDEX
 2240         LDA #00      CLEAR A
 2250         TAY          AND Y REGISTER
 2260         STA (ARRAY),Y  CLEAR ARRAY LOCATION
 2270  * CREATE NEW K FROM K=K+PRIME (MOVE THROUGH ARRAY)
 2280         LDA KVAR     GET K LOW
 2290         ADC PRIME    ADD PRIME
 2300         STA KVAR     SAVE K
 2310         LDA KVAR+1   NOW ADD HI BYTES
 2320         ADC PRIME+1
 2330         STA KVAR+1
 2340         JMP .02      LOOP TELL ARRAY DONE
 2350  * NOW COUNT PRIMES FOUND  (C=C+1)
 2360  .03
 2370  * --NOTE-- DELETE NEXT LINE TO TIME PROGRAM (JSR PRINTP)
 2380         JSR PRINTP   PRINT PRIME
 2390         INC CVAR     ADD ONE
 2400         BNE .04      GO IF NO OVERFLOW
 2410         INC CVAR+1   HI BYTE COUNTER
 2420  .04    LDA INDEX    GET INDEX
 2430  * TEST TO SEE IF WE HAVE INDEXED THROUGH ENTIRE ARRAY
 2440         SBC #SIZE    SUBTRACT SIZE
 2450         LDA INDEX+1  GET HI BYTE TOO
 2460         SBC /SIZE    SUBTRACT HI BYTE
 2470         BCC FORNXT   CONTINUE?
 2480         RTS
 2490  *---------------------------------
 2500  * PRINT THE NUMBER OF PRIMES FOUND
 2510  *---------------------------------
 2520  PRINT  LDY CVAR+1   GET HI BYTE OF COUNT
 2530         LDX CVAR
 2540         JSR PRINTN   PRINT PRIMES FOUND
 2550         RTS          JOB DONE, RETURN
 2560  *---------------------------------
 2570  *      PRINT THE PRIME NUMBER (OPTIONAL)
 2580  *---------------------------------
 2590  PRINTP LDY PRIME+1  HI BYTE 
 2600         LDX PRIME
 2610         JSR PRINTN
 2620         JSR LINE     VIDEO "-" OUT
 2630         SEC
 2640         RTS

