From df6f123c5418d632d3f0b5f7aa2cf952d7a01931 Mon Sep 17 00:00:00 2001 From: neauoire Date: Sat, 16 Oct 2021 20:16:54 -0700 Subject: [PATCH] Added primes.tal exercise --- .../{demos => exercises}/fizzbuzz.tal | 0 projects/examples/exercises/primes.tal | 57 +++++++++++++++++++ 2 files changed, 57 insertions(+) rename projects/examples/{demos => exercises}/fizzbuzz.tal (100%) create mode 100644 projects/examples/exercises/primes.tal diff --git a/projects/examples/demos/fizzbuzz.tal b/projects/examples/exercises/fizzbuzz.tal similarity index 100% rename from projects/examples/demos/fizzbuzz.tal rename to projects/examples/exercises/fizzbuzz.tal diff --git a/projects/examples/exercises/primes.tal b/projects/examples/exercises/primes.tal new file mode 100644 index 0000000..1a241ca --- /dev/null +++ b/projects/examples/exercises/primes.tal @@ -0,0 +1,57 @@ +( + An integer greater than one is called a prime number + if its only positive divisors are one and itself. ) + +%RTN { JMP2r } +%HALT { #0101 #0e DEO2 } +%MOD2 { DIV2k MUL2 SUB2 } +%EMIT { #18 DEO } + +|0100 ( -> ) @main + + #0000 #0001 + &loop + DUP2 ,is-prime JSR #00 EQU ,&skip JCN + DUP2 ,print-hex/short JSR + #20 EMIT + &skip + INC2 NEQ2k ,&loop JCN + POP2 POP2 + HALT + +BRK + +@is-prime ( number* -- flag ) + + DUP2 #0001 NEQ2 ,¬-one JCN + POP2 #00 RTN + ¬-one + STH2k + ( range ) #01 SFT2 #0002 + &loop + STH2kr OVR2 MOD2 #0000 NEQ2 ,&continue JCN + POP2 POP2 + POP2r #00 RTN + &continue + INC2 GTH2k ,&loop JCN + POP2 POP2 + POP2r #01 + +RTN + +@print-hex ( value* -- ) + + &short ( value* -- ) + SWP ,&echo JSR + &byte ( value -- ) + ,&echo JSR + RTN + + &echo ( value -- ) + STHk #04 SFT ,&parse JSR EMIT + STHr #0f AND ,&parse JSR EMIT + RTN + &parse ( value -- char ) + DUP #09 GTH ,&above JCN #30 ADD RTN &above #09 SUB #60 ADD RTN + +RTN \ No newline at end of file