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
2022-09-04 13:08:38 -07:00

71 lines
1.6 KiB
Tal

( 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 )
|0100 ( -> )
;memory
;program
&while
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
INC2 LDAk ,&while JCN
POP2
( halt )
#010f DEO
BRK
@goto-next ( -- )
OVR2 LDA #00 EQU JMP JMP2r
( depth ) LITr 00
INC2
&loop
LDAk LIT "[ NEQ JMP INCr
LDAk LIT "] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
LITr 01 SUBr
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
JMP2r
@goto-back ( -- )
OVR2 LDA #00 NEQ JMP JMP2r
( depth ) LITr 00
#0001 SUB2
&loop
LDAk LIT "] NEQ JMP INCr
LDAk LIT "[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
LITr 01 SUBr
&no-end
#0001 SUB2 LDAk ,&loop JCN
&end
( depth ) POPr
JMP2r
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
"-.>>+.>++. $1
@memory