uxn/projects/examples/exercises/subleq.tal

47 lines
1.1 KiB
Tal
Raw Normal View History

2022-03-25 13:29:45 -04: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 13:47:14 -05:00
2022-03-25 13:29:45 -04:00
|0000
2021-12-11 13:47:14 -05:00
2022-03-25 13:29:45 -04:00
@a $2 @b $2 @c $2
2021-12-11 13:47:14 -05:00
2022-03-25 13:29:45 -04:00
|0100 ( -> ) @reset
2021-12-11 13:47:14 -05:00
2022-03-25 13:29:45 -04:00
#0000
2021-12-11 13:47:14 -05:00
&while
,eval JSR
DUP2 #8000 LTH2 ,&while JCN
POP2
2022-03-25 13:29:45 -04:00
( halt ) #010f DEO
2021-12-11 13:47:14 -05:00
BRK
@eval ( ip* -- ip* )
2022-03-25 13:29:45 -04:00
DUP2 ,&get JSR .a STZ2
INC2 DUP2 ,&get JSR .b STZ2
INC2 DUP2 ,&get JSR .c STZ2
2021-12-11 13:47:14 -05:00
INC2
( I/O )
2022-03-25 13:29:45 -04:00
.a LDZ2 #ffff EQU2 ,&input JCN
.b LDZ2 #ffff EQU2 ,&output JCN
2021-12-11 13:47:14 -05:00
( SUBLEQ )
2022-03-25 13:29:45 -04:00
.b LDZ2 STH2k ,&get JSR .a LDZ2 ,&get JSR SUB2 STH2r #10 SFT2 ;program ADD2 STA2
2021-12-11 13:47:14 -05:00
( SET )
2022-03-25 13:29:45 -04:00
.b LDZ2 ,&get JSR #0001 SUB2 #8000 LTH2 ,&end JCN POP2 .c LDZ2 &end
2021-12-11 13:47:14 -05:00
2022-03-25 13:29:45 -04:00
JMP2r
&input ( -- ) JMP2r
&output ( -- ) .a LDZ2 ,&get JSR NIP #18 DEO JMP2r
&get ( a* -- b* ) #10 SFT2 ;program ADD2 LDA2 JMP2r
2021-12-11 13:47:14 -05:00
@program ( hello world )
2022-03-25 13:29:45 -04: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
2021-12-11 13:47:14 -05:00
0077 006f 0072 006c 0064 0021 000a 0000