0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-23 06:15:10 +00:00
uxn/projects/examples/exercises/brainfuck.tal

72 lines
1.6 KiB
Tal
Raw Normal View History

2022-03-25 17:29:45 +00:00
( Brainfuck:
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell at the pointer
- Decrement the memory cell at the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell at the pointer is 0
] Jump back to the matching [ if the cell at the pointer is nonzero )
2021-11-13 02:51:39 +00:00
2022-09-04 20:08:38 +00:00
|0100 ( -> )
2021-11-13 02:51:39 +00:00
2022-02-15 18:24:11 +00:00
;memory
2021-11-13 02:51:39 +00:00
;program
&while
2022-09-04 20:08:38 +00:00
LDAk LIT "> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
LDAk LIT "< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
LDAk LIT "+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
LDAk LIT "- NEQ ,&decr JCN [ OVR2 STH2k LDA #01 SUB STH2r STA ] &decr
LDAk LIT ". NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit
LDAk LIT "[ NEQ ,&next JCN [ ,goto-next JSR ] &next
LDAk LIT "] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
2021-11-13 02:51:39 +00:00
INC2 LDAk ,&while JCN
POP2
2022-09-04 20:08:38 +00:00
( halt )
#010f DEO
2021-11-13 02:51:39 +00:00
BRK
@goto-next ( -- )
2022-02-15 18:24:11 +00:00
OVR2 LDA #00 EQU JMP JMP2r
2021-11-13 02:51:39 +00:00
( depth ) LITr 00
INC2
&loop
2022-09-04 20:08:38 +00:00
LDAk LIT "[ NEQ JMP INCr
LDAk LIT "] NEQ ,&no-end JCN
2021-11-13 02:51:39 +00:00
STHkr #00 EQU ,&end JCN
2022-03-25 17:29:45 +00:00
LITr 01 SUBr
2021-11-13 02:51:39 +00:00
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
2022-02-15 18:24:11 +00:00
JMP2r
2021-11-13 02:51:39 +00:00
@goto-back ( -- )
2022-02-15 18:24:11 +00:00
OVR2 LDA #00 NEQ JMP JMP2r
2021-11-13 02:51:39 +00:00
( depth ) LITr 00
2022-03-25 17:29:45 +00:00
#0001 SUB2
2021-11-13 02:51:39 +00:00
&loop
2022-09-04 20:08:38 +00:00
LDAk LIT "] NEQ JMP INCr
LDAk LIT "[ NEQ ,&no-end JCN
2021-11-13 02:51:39 +00:00
STHkr #00 EQU ,&end JCN
2022-03-25 17:29:45 +00:00
LITr 01 SUBr
2021-11-13 02:51:39 +00:00
&no-end
2022-03-25 17:29:45 +00:00
#0001 SUB2 LDAk ,&loop JCN
2021-11-13 02:51:39 +00:00
&end
( depth ) POPr
2022-02-15 18:24:11 +00:00
JMP2r
2021-11-13 02:51:39 +00:00
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
2022-02-15 18:24:11 +00:00
"-.>>+.>++. $1
2022-09-04 20:08:38 +00:00
@memory