Browse code

(exercises/) Cleanup

neauoire authored on 25/03/2022 17:29:45
Showing 5 changed files
... ...
@@ -1,33 +1,28 @@
1
-( brainfuck interpreter )
1
+( Brainfuck:
2
+	>	Move the pointer to the right
3
+	<	Move the pointer to the left
4
+	+	Increment the memory cell at the pointer
5
+	-	Decrement the memory cell at the pointer
6
+	.	Output the character signified by the cell at the pointer
7
+	,	Input a character and store it in the cell at the pointer
8
+	[	Jump past the matching ] if the cell at the pointer is 0
9
+	]	Jump back to the matching [ if the cell at the pointer is nonzero )
2 10
 
3
-%DEC { #01 SUB }
4
-%DEC2 { #0001 SUB2 }
5
-%DECr { LITr 01 SUBr }
6
-%HALT { #0101 #0e DEO2 }
7
-%EMIT { #18 DEO }
8
-
9
-|0100 ( -> )
11
+|0100 ( -> ) @reset
10 12
 
11 13
 	;memory
12 14
 	;program
13 15
 	&while
14
-		( Move the pointer to the right )
15 16
 		LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
16
-		( Move the pointer to the left )
17
-		LDAk LIT '< NEQ ,&movl JCN [ SWP2 DEC2 SWP2 ] &movl
18
-		( Increment the memory cell at the pointer )
17
+		LDAk LIT '< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
19 18
 		LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
20
-		( Decrement the memory cell at the pointer )
21
-		LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA DEC STH2r STA ] &decr
22
-		( Output the character signified by the cell at the pointer )
23
-		LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA EMIT ] &emit
24
-		( Jump past the matching ] if the cell at the pointer is 0 )
19
+		LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA #01 SUB STH2r STA ] &decr
20
+		LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit
25 21
 		LDAk LIT '[ NEQ ,&next JCN [ ,goto-next JSR ] &next
26
-		( Jump back to the matching [ if the cell at the pointer is nonzero )
27 22
 		LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
28 23
 		INC2 LDAk ,&while JCN
29 24
 	POP2
30
-	HALT
25
+	( halt ) #010f DEO
31 26
 	
32 27
 BRK
33 28
 
... ...
@@ -40,7 +35,7 @@ BRK
40 35
 		LDAk LIT '[ NEQ JMP INCr
41 36
 		LDAk LIT '] NEQ ,&no-end JCN
42 37
 			STHkr #00 EQU ,&end JCN
43
-			DECr
38
+			LITr 01 SUBr
44 39
 			&no-end
45 40
 		INC2 LDAk ,&loop JCN
46 41
 	&end
... ...
@@ -52,14 +47,14 @@ JMP2r
52 47
 
53 48
 	OVR2 LDA #00 NEQ JMP JMP2r
54 49
 	( depth ) LITr 00
55
-	DEC2
50
+	#0001 SUB2
56 51
 	&loop
57 52
 		LDAk LIT '] NEQ JMP INCr
58 53
 		LDAk LIT '[ NEQ ,&no-end JCN
59 54
 			STHkr #00 EQU ,&end JCN
60
-			DECr
55
+			LITr 01 SUBr
61 56
 			&no-end
62
-		DEC2 LDAk ,&loop JCN
57
+		#0001 SUB2 LDAk ,&loop JCN
63 58
 	&end
64 59
 	( depth ) POPr
65 60
 
... ...
@@ -1,16 +1,15 @@
1
-( The Fibonacci Sequence 
2
-	A series of numbers where the next number is made of the two numbers before it )
1
+( Fibonacci:
2
+	A series of numbers where the next number 
3
+	is made of the two numbers before it )
3 4
 
4
-%HALT { #010f DEO }
5
-%EMIT { #18 DEO }
6
-%PRINT { DUP2 ,print JSR #0a EMIT }
5
+|0100 ( -> ) @reset
7 6
 
8
-|0100 ( -> )
9
-
10
-	#0000 INC2k ADD2k 
11
-	&loop 
12
-		PRINT ADD2k LTH2k ,&loop JCN
13
-	HALT
7
+	#0000 INC2k ADD2k
8
+	&loop
9
+		( print ) DUP2 ,print JSR
10
+		( linebreak ) #0a18 DEO
11
+		ADD2k LTH2k ,&loop JCN
12
+	( halt ) #010f DEO
14 13
 
15 14
 BRK
16 15
 
... ...
@@ -18,6 +17,6 @@ BRK
18 17
 
19 18
 	&short ( short* -- ) SWP ,&byte JSR
20 19
 	&byte ( byte -- ) DUP #04 SFT ,&char JSR
21
-	&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD EMIT
20
+	&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
22 21
 
23 22
 JMP2r
... ...
@@ -1,32 +1,27 @@
1
-( FizzBuzz: a program that prints the integers from 1 to 100.
1
+( FizzBuzz: 
2
+	A program that prints the integers from 1 to 100.
2 3
 	for multiples of three, print "Fizz"
3 4
 	for multiples of five, print "Buzz"
4 5
 	for multiples of both three and five, print "FizzBuzz" )
5 6
 
6
-|0100 ( -> ) @program
7
+|0100 ( -> ) @reset
7 8
 
8 9
 	#6400
9 10
 	&loop
10
-		( dec )
11
-		DUPk #0a DIV ,print-num JSR
12
-		#0a ,mod JSR ,print-num JSR
13
-		( space )
14
-		#2018 DEO
15
-		( text )
11
+		( dec ) DUPk ,print-dec JSR
12
+		( space ) #2018 DEO
16 13
 		DUP #03 ,mod JSR ,&no3 JCN ;s/fizz ,print-str JSR &no3
17 14
 		DUP #05 ,mod JSR ,&no5 JCN ;s/buzz ,print-str JSR &no5
18 15
 		( linebreak ) #0a18 DEO
19 16
 		INC GTHk ,&loop JCN
20 17
 	POP2
21
-	( halt ) 
22
-	#010f DEO
18
+	( halt ) #010f DEO
23 19
 
24 20
 BRK
25 21
 
26 22
 @mod ( a b -- c ) DIVk MUL SUB JMP2r
23
+@print-dec ( num -- ) #0a DIV ,print-num JSR #0a ,mod JSR
27 24
 @print-num ( num -- ) #30 ADD #18 DEO JMP2r
28 25
 @print-str ( addr* -- ) &loop LDAk #18 DEO INC2 LDAk ,&loop JCN POP2 JMP2r
29 26
 
30
-@s 
31
-	&fizz "Fizz $1
32
-	&buzz "Buzz $1
27
+@s &fizz "Fizz $1 &buzz "Buzz $1
... ...
@@ -1,43 +1,42 @@
1
-( 
2
-	An integer greater than one is called a prime number 
1
+( Primes:
2
+	An integer greater than one is called a prime number
3 3
 	if its only positive divisors are one and itself. )
4 4
 
5
-|0100 ( -> ) @main
5
+|0100 ( -> ) @reset
6 6
 
7 7
 	#0000 #0001
8 8
 	&loop
9 9
 		DUP2 ,is-prime JSR #00 EQU ,&skip JCN
10
-			DUP2 ,print/short JSR
11
-			#20 ( emit ) #18 DEO
10
+			( print ) DUP2 ,print/short JSR
11
+			( space ) #2018 DEO
12 12
 			&skip
13 13
 		INC2 NEQ2k ,&loop JCN
14 14
 	POP2 POP2
15
-	#0101 #0e DEO2
15
+	( halt ) #010f DEO
16 16
 	
17 17
 BRK
18 18
 
19 19
 @is-prime ( number* -- flag )
20 20
 
21
-	DUP2 #0001 NEQ2 ,&not-one JCN
22
-		POP2 #00 JMP2r
23
-		&not-one
21
+	DUP2 #0001 EQU2 ,&fail JCN
24 22
 	STH2k
25 23
 	( range ) #01 SFT2 #0002
26 24
 	&loop
27 25
 		STH2kr OVR2 ( mod2 ) [ DIV2k MUL2 SUB2 ] ORA ,&continue JCN
28
-			POP2 POP2 
26
+			POP2 POP2
29 27
 			POP2r #00 JMP2r
30 28
 			&continue
31 29
 		INC2 GTH2k ,&loop JCN
32
-	POP2 POP2 
30
+	POP2 POP2
33 31
 	POP2r #01
34 32
 
35 33
 JMP2r
34
+	&fail POP2 #00 JMP2r
36 35
 
37 36
 @print ( short* -- )
38 37
 
39 38
 	&short ( short* -- ) SWP ,&byte JSR
40 39
 	&byte ( byte -- ) DUP #04 SFT ,&char JSR
41
-	&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD ( emit ) #18 DEO
40
+	&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
42 41
 
43 42
 JMP2r
... ...
@@ -1,45 +1,46 @@
1
+( Subleq:
2
+	The subleq instruction subtracts the contents at address a
3
+	from the contents at address b, stores the result at address b,
4
+	and then, if the result is not positive, jumps to address c.
5
+	If the result is positive, execution proceeds to the next instruction
6
+	in sequence. )
1 7
 
2
-( uxnasm subleq.tal subleq.rom && uxncli subleq.rom )
8
+|0000
3 9
 
4
-%EMIT { #18 DEO }
5
-%HALT { #0101 #0e DEO2 }
6
-%RTN  { JMP2r }
7
-%GET  { #10 SFT2 ;program ADD2 LDA2 }
8
-%SET  { #10 SFT2 ;program ADD2 STA2 }
10
+	@a $2 @b $2 @c $2
9 11
 
10
-|0000 @a $2 @b $2 @c $2
11
-|0100
12
+|0100 ( -> ) @reset
12 13
 
13
-	( pointer ) #0000 
14
+	#0000
14 15
 	&while
15 16
 		,eval JSR
16 17
 		DUP2 #8000 LTH2 ,&while JCN
17 18
 	POP2
18
-	HALT
19
+	( halt ) #010f DEO
19 20
 
20 21
 BRK
21 22
 
22 23
 @eval ( ip* -- ip* )
23 24
 
24
-	DUP2 GET .a STZ2
25
-	INC2 DUP2 GET .b STZ2
26
-	INC2 DUP2 GET .c STZ2
25
+	DUP2 ,&get JSR .a STZ2
26
+	INC2 DUP2 ,&get JSR .b STZ2
27
+	INC2 DUP2 ,&get JSR .c STZ2
27 28
 	INC2
28 29
 	( I/O )
29
- 	.a LDZ2 #ffff NEQ2 ,&noin JCN 
30
- 		( nothing. ) ,&end JMP2 &noin
31
-	.b LDZ2 #ffff NEQ2 ,&noout JCN 
32
-		.a LDZ2 GET NIP EMIT ,&end JMP &noout
30
+ 	.a LDZ2 #ffff EQU2 ,&input JCN
31
+	.b LDZ2 #ffff EQU2 ,&output JCN
33 32
 	( SUBLEQ )
34
-	.b LDZ2 GET .a LDZ2 GET SUB2 .b LDZ2 SET
33
+	.b LDZ2 STH2k ,&get JSR .a LDZ2 ,&get JSR SUB2 STH2r #10 SFT2 ;program ADD2 STA2
35 34
 	( SET )
36
-	.b LDZ2 GET #0001 SUB2 #8000 LTH2 ,&end JCN
37
-		POP2 .c LDZ2  &end
35
+	.b LDZ2 ,&get JSR #0001 SUB2 #8000 LTH2 ,&end JCN POP2 .c LDZ2 &end
38 36
 
39
-RTN
37
+JMP2r
38
+	&input ( -- ) JMP2r
39
+	&output ( -- ) .a LDZ2 ,&get JSR NIP #18 DEO JMP2r
40
+	&get ( a* -- b* ) #10 SFT2 ;program ADD2 LDA2 JMP2r
40 41
 
41 42
 @program ( hello world )
42
-	000f 0011 ffff 0011 ffff ffff 0010 0001 
43
-	ffff 0010 0003 ffff 000f 000f 0000 0000 
44
-	ffff 0048 0065 006c 006c 006f 002c 0020 
43
+	000f 0011 ffff 0011 ffff ffff 0010 0001
44
+	ffff 0010 0003 ffff 000f 000f 0000 0000
45
+	ffff 0048 0065 006c 006c 006f 002c 0020
45 46
 	0077 006f 0072 006c 0064 0021 000a 0000