ERASTOSHENES REVISITED
Cast your mind back to the April issue of ZXAppeal and remember. It was then that we introduced old Erastoshenes, and his sieve, to you. It had to do with finding prime numbers,.. ah now you remember. In the mean time Ken Kenny presented us with a program to confirm prime numbers and now Harvey Taylor goes all of us one better with a machine language prime sieve for the ZX81. Are we a prime bunch or what?
PR
Since I got my QL, I have been curious just how it stacks up in terms of speed. In the back of the M68000 Programmer’s Reference Manual there is a complete listing of the number of clock periods for each instruction. Because of the richness & variety of the 68K addressing modes, most of the variation is attributable to the addressing mode; not the instruction itself.
Well my cat was dead & only satisfaction would bring it back: so I ran a few benchmarks. If you do not know about the Sieve of Eratosthenes, you can read up on it in Byte Jan/83,page 283 & Aug/85, page 33. In short the program (see listing below), finds the prime numbers between 3 and 16387. There is a good description of how in the Aug. Byte.
So the results. For comparison I ran the program in the ZX81 too.
MACHINE LANGUAGE TIME (seconds) ZX81 Basic (FAST) 448. ZX81 Basic (Slow) 2852. ZX81 Assembler (Fast) 1.3 ZX81 Assembler (Slow) 8.3 QL Superbasic 276. QL Assembler 0.76 IBM3033 Assembler 0.0078
SIEVE OF ERATOSTHENES
ZX81 ASSEMBLER
START CALL MSG1
CALL FAST
LD HL,(4010) ; VARS
LD DE,2001 ; 8K+1
ADD HL,DE
PUSH HL ;END OF ARRAY
LD HL,(4004)
AND A
LD BC,0080
SBC HL,BC ; STK ROOM
POP DE
JP C NOMEMORY
LD A,0A ; ITERATIONS
LD(403E),A
ILOOP LD HL,(4010)
LD DE,2000
ALOOP LD(HL),A
INC HL
DEC DE
LD A,E
OR D
JR NZ ALOOP
LD HL,2000
LD(403C),HL ;CNTR
LD HL,0000
LD(4040),HL ; INDEX
LD(4042),HL ; PRIME
LD(4044),HL ; K
LD(4046),HL ; COUNT
MLOOP LD HL,(4010)
LD DE,(4040)
ADD HL,DE
XOR A
CP(HL)
JR Z NEXTI
LD HL,(4040)
ADD HL,DE
INC HL
INC HL
INC HL ; ; PRIME=(2*I)+3
LD(4042),HL
ADD HL,DE
LD(4044),HL ; K=I+PRIME
LD BC,2000 ; 8K
WHILE LD HL,(4044)
AND A
SBC HL,BC
JR NC WEXIT
LD HL,(4010)
LD DE,(4044) ; K
ADD HL,DE
LD(HL),0
LD HL,(4042) ; PRIME
ADD HL,DE
LD(4044),HL ; K=K+PRIME
JR WHILE
WEXIT LD HL,(4046)
INC HL
LD(4046),HL
NEXTI LD HL,(4040)
INC HL
LD(4040),HL
LD HL,(403C)
DEC HL
LD(403C),HL
LD A,H
OR L
JR NZ MLOOP
LD A,(403E)
DEC A
LD(403E),A ;NEXT
JR NZ ILOOP ;ITERATION
CALL MSG2
CALL SLOW
LD HL,(4046)
PRTHL PUSH HL
LD A,H
CALL HEXPR
POP HL
LD A,L
CALL HEXPR
JP END
NOMEMORY RST 08
05
SPRINT CALL PRPOS
SPRL0 POP HL
LD A,(HL)
INC HL
PUSH HL
CP FF
RET Z
RST 10
JR SPRL0
PRPOS LD HL,(400C)
ADD HL,DE
LD(400E),HL
RET
HEXPR PUSH AF
PUSH AF
AND F0
RRA
RRA
RRA
RRA
ADD A,1C
RST 10
POP AF
AND 0F
ADD A,1C
RST 10
POP AF
RET
MSG1 LD DE,0024
CALL SPRINT
"SIEVE OF ERATOSTHENES "
FF
RET
MSG2 LD DE,0066
CALL SPRINT
"DONE. COUNT="FF
RET
FAST CALL 0F23
RET
SLOW CALL 0F2B
RET
END NOP
10 REM SIEVE OF ERATOSTHENES
20 REM FOR ZX81-(64K)
30 PRINT AT 10,0;"STARTING NOW "
40 LET SIZE=8192
50 DIM A(SIZE + 1)
60 FOR N=1 TO 8192
70 LET A(N)=1
80 NEXT N
90 LET COUNT=0
100 FOR I=0 TO SIZE
105 IF I=0 THEN GOTO 120
110 IF A(I)=0 THEN GOTO 190
120 LET PRIME=2*I+3
130 LET K=I+PRIME
140 IF K>SIZE THEN GOTO 180
150 LET A(K)=0
160 LET K=K+PRIME
170 GOTO 140
180 LET COUNT=COUNT+1
190 NEXT I
200 PRINT AT 11,0;"FINISHED","COUNT=";COUNT
210 STOP
220 SAVE "SIEVE"
230 LIST
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.