0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-09 15:55:05 +00:00
uxn/projects/examples/exercises/subleq.tal

42 lines
1.1 KiB
Tal
Raw Normal View History

2023-06-27 18:27:27 +00:00
( Subleq: The subleq instruction subtracts the contents at address a from the contents at address b, stores the result at address b, and then, if the result is not positive, jumps to address c. If the result is positive, execution proceeds to the next instruction in sequence. )
2021-12-11 18:47:14 +00:00
2023-06-27 18:27:27 +00:00
|0100
2021-12-11 18:47:14 +00:00
2023-06-27 18:27:27 +00:00
@on-reset ( -> )
2022-03-25 17:29:45 +00:00
#0000
2023-06-27 18:27:27 +00:00
&while ( -- )
eval DUP2 #8000 LTH2 ?&while
2021-12-11 18:47:14 +00:00
POP2
2022-03-25 17:29:45 +00:00
( halt ) #010f DEO
2023-06-27 18:27:27 +00:00
BRK
2021-12-11 18:47:14 +00:00
@eval ( ip* -- ip* )
2023-06-27 18:27:27 +00:00
DUP2 get ,&a STR2
INC2 DUP2 get ,&b STR2
INC2 DUP2 get ,&c STR2
2021-12-11 18:47:14 +00:00
INC2
2023-06-27 18:27:27 +00:00
( | I/O )
[ LIT2 &a $2 ] #ffff EQU2 ?&input
[ LIT2 &b $2 ] #ffff EQU2 ?&output
( | SUBLEQ )
( sub ) ,&b LDR2 get ,&a LDR2 get SUB2 DUP2
( write ) ,&b LDR2 DUP2 ADD2 ;program ADD2 STA2
( leq ) #0001 SUB2 #8000 LTH2 ?&end
POP2 [ LIT2 &c $2 ] &end JMP2r
&input ( -- )
( TODO ) JMP2r
&output ( -- )
,&a LDR2 get NIP #18 DEO
JMP2r
@get ( a* -- b* )
DUP2 ADD2 ;program ADD2 LDA2 JMP2r
2021-12-11 18:47:14 +00:00
@program ( hello world )
2023-06-27 18:27:27 +00:00
[
2022-03-25 17:29:45 +00:00
000f 0011 ffff 0011 ffff ffff 0010 0001
ffff 0010 0003 ffff 000f 000f 0000 0000
ffff 0048 0065 006c 006c 006f 002c 0020
2023-06-27 18:27:27 +00:00
0077 006f 0072 006c 0064 0021 000a 0000 ]