(exercises/) Cleanup
This commit is contained in:
parent
19172bf049
commit
9a59e9d1d9
|
@ -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 }
|
|0100 ( -> ) @reset
|
||||||
%DEC2 { #0001 SUB2 }
|
|
||||||
%DECr { LITr 01 SUBr }
|
|
||||||
%HALT { #0101 #0e DEO2 }
|
|
||||||
%EMIT { #18 DEO }
|
|
||||||
|
|
||||||
|0100 ( -> )
|
|
||||||
|
|
||||||
;memory
|
;memory
|
||||||
;program
|
;program
|
||||||
&while
|
&while
|
||||||
( Move the pointer to the right )
|
|
||||||
LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
|
LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
|
||||||
( Move the pointer to the left )
|
LDAk LIT '< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
|
||||||
LDAk LIT '< NEQ ,&movl JCN [ SWP2 DEC2 SWP2 ] &movl
|
|
||||||
( Increment the memory cell at the pointer )
|
|
||||||
LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
|
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 #01 SUB STH2r STA ] &decr
|
||||||
LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA DEC STH2r STA ] &decr
|
LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit
|
||||||
( 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 ,&next JCN [ ,goto-next JSR ] &next
|
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
|
LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
|
||||||
INC2 LDAk ,&while JCN
|
INC2 LDAk ,&while JCN
|
||||||
POP2
|
POP2
|
||||||
HALT
|
( halt ) #010f DEO
|
||||||
|
|
||||||
BRK
|
BRK
|
||||||
|
|
||||||
|
@ -40,7 +35,7 @@ BRK
|
||||||
LDAk LIT '[ NEQ JMP INCr
|
LDAk LIT '[ NEQ JMP INCr
|
||||||
LDAk LIT '] NEQ ,&no-end JCN
|
LDAk LIT '] NEQ ,&no-end JCN
|
||||||
STHkr #00 EQU ,&end JCN
|
STHkr #00 EQU ,&end JCN
|
||||||
DECr
|
LITr 01 SUBr
|
||||||
&no-end
|
&no-end
|
||||||
INC2 LDAk ,&loop JCN
|
INC2 LDAk ,&loop JCN
|
||||||
&end
|
&end
|
||||||
|
@ -52,14 +47,14 @@ JMP2r
|
||||||
|
|
||||||
OVR2 LDA #00 NEQ JMP JMP2r
|
OVR2 LDA #00 NEQ JMP JMP2r
|
||||||
( depth ) LITr 00
|
( depth ) LITr 00
|
||||||
DEC2
|
#0001 SUB2
|
||||||
&loop
|
&loop
|
||||||
LDAk LIT '] NEQ JMP INCr
|
LDAk LIT '] NEQ JMP INCr
|
||||||
LDAk LIT '[ NEQ ,&no-end JCN
|
LDAk LIT '[ NEQ ,&no-end JCN
|
||||||
STHkr #00 EQU ,&end JCN
|
STHkr #00 EQU ,&end JCN
|
||||||
DECr
|
LITr 01 SUBr
|
||||||
&no-end
|
&no-end
|
||||||
DEC2 LDAk ,&loop JCN
|
#0001 SUB2 LDAk ,&loop JCN
|
||||||
&end
|
&end
|
||||||
( depth ) POPr
|
( depth ) POPr
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
( The Fibonacci Sequence
|
( Fibonacci:
|
||||||
A series of numbers where the next number is made of the two numbers before it )
|
A series of numbers where the next number
|
||||||
|
is made of the two numbers before it )
|
||||||
|
|
||||||
%HALT { #010f DEO }
|
|0100 ( -> ) @reset
|
||||||
%EMIT { #18 DEO }
|
|
||||||
%PRINT { DUP2 ,print JSR #0a EMIT }
|
|
||||||
|
|
||||||
|0100 ( -> )
|
|
||||||
|
|
||||||
#0000 INC2k ADD2k
|
#0000 INC2k ADD2k
|
||||||
&loop
|
&loop
|
||||||
PRINT ADD2k LTH2k ,&loop JCN
|
( print ) DUP2 ,print JSR
|
||||||
HALT
|
( linebreak ) #0a18 DEO
|
||||||
|
ADD2k LTH2k ,&loop JCN
|
||||||
|
( halt ) #010f DEO
|
||||||
|
|
||||||
BRK
|
BRK
|
||||||
|
|
||||||
|
@ -18,6 +17,6 @@ BRK
|
||||||
|
|
||||||
&short ( short* -- ) SWP ,&byte JSR
|
&short ( short* -- ) SWP ,&byte JSR
|
||||||
&byte ( byte -- ) DUP #04 SFT ,&char 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
|
JMP2r
|
||||||
|
|
|
@ -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 three, print "Fizz"
|
||||||
for multiples of five, print "Buzz"
|
for multiples of five, print "Buzz"
|
||||||
for multiples of both three and five, print "FizzBuzz" )
|
for multiples of both three and five, print "FizzBuzz" )
|
||||||
|
|
||||||
|0100 ( -> ) @program
|
|0100 ( -> ) @reset
|
||||||
|
|
||||||
#6400
|
#6400
|
||||||
&loop
|
&loop
|
||||||
( dec )
|
( dec ) DUPk ,print-dec JSR
|
||||||
DUPk #0a DIV ,print-num JSR
|
( space ) #2018 DEO
|
||||||
#0a ,mod JSR ,print-num JSR
|
|
||||||
( space )
|
|
||||||
#2018 DEO
|
|
||||||
( text )
|
|
||||||
DUP #03 ,mod JSR ,&no3 JCN ;s/fizz ,print-str JSR &no3
|
DUP #03 ,mod JSR ,&no3 JCN ;s/fizz ,print-str JSR &no3
|
||||||
DUP #05 ,mod JSR ,&no5 JCN ;s/buzz ,print-str JSR &no5
|
DUP #05 ,mod JSR ,&no5 JCN ;s/buzz ,print-str JSR &no5
|
||||||
( linebreak ) #0a18 DEO
|
( linebreak ) #0a18 DEO
|
||||||
INC GTHk ,&loop JCN
|
INC GTHk ,&loop JCN
|
||||||
POP2
|
POP2
|
||||||
( halt )
|
( halt ) #010f DEO
|
||||||
#010f DEO
|
|
||||||
|
|
||||||
BRK
|
BRK
|
||||||
|
|
||||||
@mod ( a b -- c ) DIVk MUL SUB JMP2r
|
@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-num ( num -- ) #30 ADD #18 DEO JMP2r
|
||||||
@print-str ( addr* -- ) &loop LDAk #18 DEO INC2 LDAk ,&loop JCN POP2 JMP2r
|
@print-str ( addr* -- ) &loop LDAk #18 DEO INC2 LDAk ,&loop JCN POP2 JMP2r
|
||||||
|
|
||||||
@s
|
@s &fizz "Fizz $1 &buzz "Buzz $1
|
||||||
&fizz "Fizz $1
|
|
||||||
&buzz "Buzz $1
|
|
||||||
|
|
|
@ -1,26 +1,24 @@
|
||||||
(
|
( Primes:
|
||||||
An integer greater than one is called a prime number
|
An integer greater than one is called a prime number
|
||||||
if its only positive divisors are one and itself. )
|
if its only positive divisors are one and itself. )
|
||||||
|
|
||||||
|0100 ( -> ) @main
|
|0100 ( -> ) @reset
|
||||||
|
|
||||||
#0000 #0001
|
#0000 #0001
|
||||||
&loop
|
&loop
|
||||||
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
|
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
|
||||||
DUP2 ,print/short JSR
|
( print ) DUP2 ,print/short JSR
|
||||||
#20 ( emit ) #18 DEO
|
( space ) #2018 DEO
|
||||||
&skip
|
&skip
|
||||||
INC2 NEQ2k ,&loop JCN
|
INC2 NEQ2k ,&loop JCN
|
||||||
POP2 POP2
|
POP2 POP2
|
||||||
#0101 #0e DEO2
|
( halt ) #010f DEO
|
||||||
|
|
||||||
BRK
|
BRK
|
||||||
|
|
||||||
@is-prime ( number* -- flag )
|
@is-prime ( number* -- flag )
|
||||||
|
|
||||||
DUP2 #0001 NEQ2 ,¬-one JCN
|
DUP2 #0001 EQU2 ,&fail JCN
|
||||||
POP2 #00 JMP2r
|
|
||||||
¬-one
|
|
||||||
STH2k
|
STH2k
|
||||||
( range ) #01 SFT2 #0002
|
( range ) #01 SFT2 #0002
|
||||||
&loop
|
&loop
|
||||||
|
@ -33,11 +31,12 @@ BRK
|
||||||
POP2r #01
|
POP2r #01
|
||||||
|
|
||||||
JMP2r
|
JMP2r
|
||||||
|
&fail POP2 #00 JMP2r
|
||||||
|
|
||||||
@print ( short* -- )
|
@print ( short* -- )
|
||||||
|
|
||||||
&short ( short* -- ) SWP ,&byte JSR
|
&short ( short* -- ) SWP ,&byte JSR
|
||||||
&byte ( byte -- ) DUP #04 SFT ,&char 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
|
JMP2r
|
||||||
|
|
|
@ -1,42 +1,43 @@
|
||||||
|
( 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 }
|
@a $2 @b $2 @c $2
|
||||||
%HALT { #0101 #0e DEO2 }
|
|
||||||
%RTN { JMP2r }
|
|
||||||
%GET { #10 SFT2 ;program ADD2 LDA2 }
|
|
||||||
%SET { #10 SFT2 ;program ADD2 STA2 }
|
|
||||||
|
|
||||||
|0000 @a $2 @b $2 @c $2
|
|0100 ( -> ) @reset
|
||||||
|0100
|
|
||||||
|
|
||||||
( pointer ) #0000
|
#0000
|
||||||
&while
|
&while
|
||||||
,eval JSR
|
,eval JSR
|
||||||
DUP2 #8000 LTH2 ,&while JCN
|
DUP2 #8000 LTH2 ,&while JCN
|
||||||
POP2
|
POP2
|
||||||
HALT
|
( halt ) #010f DEO
|
||||||
|
|
||||||
BRK
|
BRK
|
||||||
|
|
||||||
@eval ( ip* -- ip* )
|
@eval ( ip* -- ip* )
|
||||||
|
|
||||||
DUP2 GET .a STZ2
|
DUP2 ,&get JSR .a STZ2
|
||||||
INC2 DUP2 GET .b STZ2
|
INC2 DUP2 ,&get JSR .b STZ2
|
||||||
INC2 DUP2 GET .c STZ2
|
INC2 DUP2 ,&get JSR .c STZ2
|
||||||
INC2
|
INC2
|
||||||
( I/O )
|
( I/O )
|
||||||
.a LDZ2 #ffff NEQ2 ,&noin JCN
|
.a LDZ2 #ffff EQU2 ,&input JCN
|
||||||
( nothing. ) ,&end JMP2 &noin
|
.b LDZ2 #ffff EQU2 ,&output JCN
|
||||||
.b LDZ2 #ffff NEQ2 ,&noout JCN
|
|
||||||
.a LDZ2 GET NIP EMIT ,&end JMP &noout
|
|
||||||
( SUBLEQ )
|
( 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 )
|
( SET )
|
||||||
.b LDZ2 GET #0001 SUB2 #8000 LTH2 ,&end JCN
|
.b LDZ2 ,&get JSR #0001 SUB2 #8000 LTH2 ,&end JCN POP2 .c LDZ2 &end
|
||||||
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 )
|
@program ( hello world )
|
||||||
000f 0011 ffff 0011 ffff ffff 0010 0001
|
000f 0011 ffff 0011 ffff ffff 0010 0001
|
||||||
|
|
Loading…
Reference in New Issue