diff --git a/primes32.tal b/primes32.tal index 3f1e1bb..9d49237 100644 --- a/primes32.tal +++ b/primes32.tal @@ -26,18 +26,15 @@ ( Smaller primes also run fairly quickly: 0x17b5d was ) ( determined to be prime in 0.02 seconds. ) -%SP { #2018 DEO } -%NL { #0a18 DEO } -%EXIT { #ff0f DEO BRK } %DUP4 { OVR2 OVR2 } %POP4 { POP2 POP2 } |0100 - ( number to check comes first ) - #ffff #fffb - DUP4 ;is-prime32 JSR2 ( test for primality ) - STH ;emit/long JSR2 SP STHr ;emit/byte JSR2 NL - EXIT + #ffff #fffb ( n** ; number to check ) + DUP4 is-prime32 ( n** is-prime^ ; test for primality ) + STH emit/long #2018 DEO ( [is-prime^] ; emit n ) + STHr emit/byte #0a18 DEO ( ; emit boolean ) + #ff0f DEO BRK ( ; exit ) ( include 32-bit math library ) ~math32.tal @@ -45,37 +42,29 @@ ( return 01 if x is a prime number, else 00 ) ( works for x >= 2 ) @is-prime32 ( x** -> bool^ ) - ,&x1 STR2 ,&x0 STR2 ,&x0 LDR2 ,&x1 LDR2 ( store and reload x ) - DUP4 #0000 LIT2 &two 0002 ;ne32 JSR2 ( x is 2? ) - ,¬-two JCN POP4 #01 JMP2r ( 2 is prime ) - ¬-two DUP #01 AND ( x x&1 ) - ,¬-even JCN POP4 #00 JMP2r ( x is even: not prime ) - ¬-even DUP4 #0000 #0003 ;ne32 JSR2 ( x is 3? ) - ,¬-three JCN POP4 #01 JMP2r ( 3 is prime ) - ¬-three #0000 ,&i0 STR2 #0005 ,&i1 STR2 ( x i<-5 ) - ,&two LDR2 ,&inc STR2 - ,&i0 LDR2 ,&i1 LDR2 ( load our candidate, i ) - ,&loop JMP ( jump over register data to loop label ) - [ &i0 0000 &i1 0000 &x0 0000 &x1 0000 &inc 0000 &mask 0006 ] ( registers ) - &loop ( x i ) - ,&x0 LDR2 ,&x1 LDR2 ,&i0 LDR2 ,&i1 LDR2 ( x i x i ) - DUP4 ;mul32 JSR2 ;lt32 JSR2 ( x i x