commit 5091d90e77e5432390627791740a8c0b85a75577
parent 89d13c6b3db43f65d2bc817e24469a6ebab50249
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date: Mon, 27 Feb 2023 10:41:35 -0800
(primes.tal) Optimized
Diffstat:
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/projects/examples/exercises/primes.tal b/projects/examples/exercises/primes.tal
@@ -6,34 +6,31 @@
#0000 INC2k
&loop
- DUP2 ,is-prime JSR #00 EQU ,&skip JCN
- ( print ) DUP2 ,print/short JSR
- ( space ) #2018 DEO
+ DUP2 not-prime ?&skip
+ DUP2 print/short #2018 DEO
&skip
- INC2 NEQ2k ,&loop JCN
+ INC2 NEQ2k ?&loop
POP2 POP2
( halt ) #010f DEO
-
+
BRK
-@is-prime ( number* -- flag )
+@not-prime ( number* -- flag )
DUP2 ,&t STR2
- ( range ) #01 SFT2 #0002 LTH2k ,&fail JCN
+ ( range ) #01 SFT2 #0002 LTH2k ?&fail
&loop
- [ LIT2 &t $2 ] OVR2
- ( mod2 ) DIV2k MUL2 SUB2
- ORA #00 EQU ,&fail JCN
- INC2 GTH2k ,&loop JCN
- POP2 POP2 #01
+ [ LIT2 &t $2 ] OVR2 ( mod2 ) DIV2k MUL2 SUB2 ORA ?&continue
+ &fail POP2 POP2 #01 JMP2r &continue
+ INC2 GTH2k ?&loop
+ POP2 POP2 #00
JMP2r
- &fail POP2 POP2 #00 JMP2r
@print ( short* -- )
- &short ( short* -- ) SWP ,&byte JSR
- &byte ( byte -- ) DUP #04 SFT ,&char JSR
+ &short ( short* -- ) SWP print/byte
+ &byte ( byte -- ) DUP #04 SFT print/char
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r