uxn/projects/examples/exercises/primes.tal

44 lines
785 B
Tal
Raw Normal View History

2021-10-17 03:16:54 +00:00
(
An integer greater than one is called a prime number
if its only positive divisors are one and itself. )
|0100 ( -> ) @main
#0000 #0001
&loop
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
2022-03-10 17:37:11 +00:00
DUP2 ,print/short JSR
#20 ( emit ) #18 DEO
2021-10-17 03:16:54 +00:00
&skip
INC2 NEQ2k ,&loop JCN
POP2 POP2
2022-03-10 17:37:11 +00:00
#0101 #0e DEO2
2021-10-17 03:16:54 +00:00
BRK
@is-prime ( number* -- flag )
DUP2 #0001 NEQ2 ,&not-one JCN
2022-03-10 17:37:11 +00:00
POP2 #00 JMP2r
2021-10-17 03:16:54 +00:00
&not-one
STH2k
( range ) #01 SFT2 #0002
&loop
2022-03-10 17:37:11 +00:00
STH2kr OVR2 ( mod2 ) [ DIV2k MUL2 SUB2 ] ORA ,&continue JCN
2021-10-17 03:16:54 +00:00
POP2 POP2
2022-03-10 17:37:11 +00:00
POP2r #00 JMP2r
2021-10-17 03:16:54 +00:00
&continue
INC2 GTH2k ,&loop JCN
POP2 POP2
POP2r #01
2022-03-10 17:37:11 +00:00
JMP2r
2021-10-17 03:16:54 +00:00
2022-03-10 17:37:11 +00:00
@print ( short* -- )
&short ( short* -- ) SWP ,&byte JSR
&byte ( byte -- ) DUP #04 SFT ,&char JSR
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD ( emit ) #18 DEO
JMP2r