(primes.tal) Optimized

This commit is contained in:
Devine Lu Linvega 2023-02-27 10:41:35 -08:00
parent 89d13c6b3d
commit 5091d90e77
1 changed files with 12 additions and 15 deletions

View File

@ -6,34 +6,31 @@
#0000 INC2k #0000 INC2k
&loop &loop
DUP2 ,is-prime JSR #00 EQU ,&skip JCN DUP2 not-prime ?&skip
( print ) DUP2 ,print/short JSR DUP2 print/short #2018 DEO
( space ) #2018 DEO
&skip &skip
INC2 NEQ2k ,&loop JCN INC2 NEQ2k ?&loop
POP2 POP2 POP2 POP2
( halt ) #010f DEO ( halt ) #010f DEO
BRK BRK
@is-prime ( number* -- flag ) @not-prime ( number* -- flag )
DUP2 ,&t STR2 DUP2 ,&t STR2
( range ) #01 SFT2 #0002 LTH2k ,&fail JCN ( range ) #01 SFT2 #0002 LTH2k ?&fail
&loop &loop
[ LIT2 &t $2 ] OVR2 [ LIT2 &t $2 ] OVR2 ( mod2 ) DIV2k MUL2 SUB2 ORA ?&continue
( mod2 ) DIV2k MUL2 SUB2 &fail POP2 POP2 #01 JMP2r &continue
ORA #00 EQU ,&fail JCN INC2 GTH2k ?&loop
INC2 GTH2k ,&loop JCN POP2 POP2 #00
POP2 POP2 #01
JMP2r JMP2r
&fail POP2 POP2 #00 JMP2r
@print ( short* -- ) @print ( short* -- )
&short ( short* -- ) SWP ,&byte JSR &short ( short* -- ) SWP print/byte
&byte ( byte -- ) DUP #04 SFT ,&char JSR &byte ( byte -- ) DUP #04 SFT print/char
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO &char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r JMP2r