(exercises/) Cleanup

This commit is contained in:
neauoire 2022-03-25 10:29:45 -07:00
parent 19172bf049
commit 9a59e9d1d9
5 changed files with 73 additions and 84 deletions

View File

@ -1,33 +1,28 @@
( brainfuck interpreter )
( 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 )
%DEC { #01 SUB }
%DEC2 { #0001 SUB2 }
%DECr { LITr 01 SUBr }
%HALT { #0101 #0e DEO2 }
%EMIT { #18 DEO }
|0100 ( -> )
|0100 ( -> ) @reset
;memory
;program
&while
( Move the pointer to the right )
LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
( Move the pointer to the left )
LDAk LIT '< NEQ ,&movl JCN [ SWP2 DEC2 SWP2 ] &movl
( Increment the memory cell at the pointer )
LDAk LIT '< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
( Decrement the memory cell at the pointer )
LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA DEC STH2r STA ] &decr
( Output the character signified by the cell at the pointer )
LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA EMIT ] &emit
( Jump past the matching ] if the cell at the pointer is 0 )
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
( Jump back to the matching [ if the cell at the pointer is nonzero )
LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
INC2 LDAk ,&while JCN
POP2
HALT
( halt ) #010f DEO
BRK
@ -40,7 +35,7 @@ BRK
LDAk LIT '[ NEQ JMP INCr
LDAk LIT '] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
LITr 01 SUBr
&no-end
INC2 LDAk ,&loop JCN
&end
@ -52,14 +47,14 @@ JMP2r
OVR2 LDA #00 NEQ JMP JMP2r
( depth ) LITr 00
DEC2
#0001 SUB2
&loop
LDAk LIT '] NEQ JMP INCr
LDAk LIT '[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
LITr 01 SUBr
&no-end
DEC2 LDAk ,&loop JCN
#0001 SUB2 LDAk ,&loop JCN
&end
( depth ) POPr

View File

@ -1,16 +1,15 @@
( The Fibonacci Sequence
A series of numbers where the next number is made of the two numbers before it )
( Fibonacci:
A series of numbers where the next number
is made of the two numbers before it )
%HALT { #010f DEO }
%EMIT { #18 DEO }
%PRINT { DUP2 ,print JSR #0a EMIT }
|0100 ( -> ) @reset
|0100 ( -> )
#0000 INC2k ADD2k
&loop
PRINT ADD2k LTH2k ,&loop JCN
HALT
#0000 INC2k ADD2k
&loop
( print ) DUP2 ,print JSR
( linebreak ) #0a18 DEO
ADD2k LTH2k ,&loop JCN
( halt ) #010f DEO
BRK
@ -18,6 +17,6 @@ BRK
&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
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r

View File

@ -1,32 +1,27 @@
( FizzBuzz: a program that prints the integers from 1 to 100.
( FizzBuzz:
A program that prints the integers from 1 to 100.
for multiples of three, print "Fizz"
for multiples of five, print "Buzz"
for multiples of both three and five, print "FizzBuzz" )
|0100 ( -> ) @program
|0100 ( -> ) @reset
#6400
&loop
( dec )
DUPk #0a DIV ,print-num JSR
#0a ,mod JSR ,print-num JSR
( space )
#2018 DEO
( text )
( dec ) DUPk ,print-dec JSR
( space ) #2018 DEO
DUP #03 ,mod JSR ,&no3 JCN ;s/fizz ,print-str JSR &no3
DUP #05 ,mod JSR ,&no5 JCN ;s/buzz ,print-str JSR &no5
( linebreak ) #0a18 DEO
INC GTHk ,&loop JCN
POP2
( halt )
#010f DEO
( halt ) #010f DEO
BRK
@mod ( a b -- c ) DIVk MUL SUB JMP2r
@print-dec ( num -- ) #0a DIV ,print-num JSR #0a ,mod JSR
@print-num ( num -- ) #30 ADD #18 DEO JMP2r
@print-str ( addr* -- ) &loop LDAk #18 DEO INC2 LDAk ,&loop JCN POP2 JMP2r
@s
&fizz "Fizz $1
&buzz "Buzz $1
@s &fizz "Fizz $1 &buzz "Buzz $1

View File

@ -1,43 +1,42 @@
(
An integer greater than one is called a prime number
( Primes:
An integer greater than one is called a prime number
if its only positive divisors are one and itself. )
|0100 ( -> ) @main
|0100 ( -> ) @reset
#0000 #0001
&loop
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
DUP2 ,print/short JSR
#20 ( emit ) #18 DEO
( print ) DUP2 ,print/short JSR
( space ) #2018 DEO
&skip
INC2 NEQ2k ,&loop JCN
POP2 POP2
#0101 #0e DEO2
( halt ) #010f DEO
BRK
@is-prime ( number* -- flag )
DUP2 #0001 NEQ2 ,&not-one JCN
POP2 #00 JMP2r
&not-one
DUP2 #0001 EQU2 ,&fail JCN
STH2k
( range ) #01 SFT2 #0002
&loop
STH2kr OVR2 ( mod2 ) [ DIV2k MUL2 SUB2 ] ORA ,&continue JCN
POP2 POP2
POP2 POP2
POP2r #00 JMP2r
&continue
INC2 GTH2k ,&loop JCN
POP2 POP2
POP2 POP2
POP2r #01
JMP2r
&fail POP2 #00 JMP2r
@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
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r

View File

@ -1,45 +1,46 @@
( 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. )
( uxnasm subleq.tal subleq.rom && uxncli subleq.rom )
|0000
%EMIT { #18 DEO }
%HALT { #0101 #0e DEO2 }
%RTN { JMP2r }
%GET { #10 SFT2 ;program ADD2 LDA2 }
%SET { #10 SFT2 ;program ADD2 STA2 }
@a $2 @b $2 @c $2
|0000 @a $2 @b $2 @c $2
|0100
|0100 ( -> ) @reset
( pointer ) #0000
#0000
&while
,eval JSR
DUP2 #8000 LTH2 ,&while JCN
POP2
HALT
( halt ) #010f DEO
BRK
@eval ( ip* -- ip* )
DUP2 GET .a STZ2
INC2 DUP2 GET .b STZ2
INC2 DUP2 GET .c STZ2
DUP2 ,&get JSR .a STZ2
INC2 DUP2 ,&get JSR .b STZ2
INC2 DUP2 ,&get JSR .c STZ2
INC2
( I/O )
.a LDZ2 #ffff NEQ2 ,&noin JCN
( nothing. ) ,&end JMP2 &noin
.b LDZ2 #ffff NEQ2 ,&noout JCN
.a LDZ2 GET NIP EMIT ,&end JMP &noout
.a LDZ2 #ffff EQU2 ,&input JCN
.b LDZ2 #ffff EQU2 ,&output JCN
( SUBLEQ )
.b LDZ2 GET .a LDZ2 GET SUB2 .b LDZ2 SET
.b LDZ2 STH2k ,&get JSR .a LDZ2 ,&get JSR SUB2 STH2r #10 SFT2 ;program ADD2 STA2
( SET )
.b LDZ2 GET #0001 SUB2 #8000 LTH2 ,&end JCN
POP2 .c LDZ2 &end
.b LDZ2 ,&get JSR #0001 SUB2 #8000 LTH2 ,&end JCN POP2 .c LDZ2 &end
RTN
JMP2r
&input ( -- ) JMP2r
&output ( -- ) .a LDZ2 ,&get JSR NIP #18 DEO JMP2r
&get ( a* -- b* ) #10 SFT2 ;program ADD2 LDA2 JMP2r
@program ( hello world )
000f 0011 ffff 0011 ffff ffff 0010 0001
ffff 0010 0003 ffff 000f 000f 0000 0000
ffff 0048 0065 006c 006c 006f 002c 0020
000f 0011 ffff 0011 ffff ffff 0010 0001
ffff 0010 0003 ffff 000f 000f 0000 0000
ffff 0048 0065 006c 006c 006f 002c 0020
0077 006f 0072 006c 0064 0021 000a 0000