Browse code

Migrated loops to relative format

neauoire authored on 11/03/2021 22:00:32
Showing 5 changed files
... ...
@@ -24,19 +24,30 @@ evaluxn(u, u->vframe); /* Each frame
24 24
 
25 25
 ## Assembly Syntax
26 26
 
27
-- `ADD`, an opcode.
27
+### Define
28
+
28 29
 - `@label`, assign the current address to a label.
29 30
 - `;variable 2`, assign an address to a label automatically.
30 31
 - `:const 1a2b`, assign an address to a label manually.
31 32
 - `&macro { x 2 y 2 }`, define a macro named `macro`.
33
+
34
+### Program
35
+
36
+- `ADD`, push an opcode.
32 37
 - `.address`, push label address to memory.
33 38
 - `,literal`, push label address to stack, prefixed with `LIT LEN`.
34
-- `#1a`, a literal byte/short.
35
-- `+1a`, a literal signed byte/short.
36
-- `-1a`, a literal signed byte/short(negative).
39
+- `#1a`, push a literal byte/short.
40
+- `+1a`, push a literal signed byte/short.
41
+- `-1a`, push a literal signed byte/short(negative).
42
+- `|0010`, move to position in the program.
43
+
44
+### Helpers
45
+
37 46
 - `=label`, helper to STR, equivalent to `,label STR`, or `label STR2`.
38 47
 - `~label`, helper to LDR, equivalent to `,label LDR2`, or `,label LDR2`.
39
-- `|0010`, move to position in the program.
48
+
49
+### Blocks
50
+
40 51
 - `( comment )`, toggle parsing on/off.
41 52
 - `[ 0123 abcd ]`, write shorts to memory.
42 53
 - `[ Hello World ]`, write text to memory.
... ...
@@ -64,15 +75,15 @@ BRK
64 75
 
65 76
 @print-label ( text )
66 77
 	
67
-	@print-label-loop NOP
68
-		( send ) DUP2 LDR =CNSL.char
69
-		( incr ) #0001 ADD2
70
-		DUP2 LDR #00 NEQ ^print-label-loop MUL JMPS 
78
+	NOP
79
+	( send ) DUP2 LDR =CNSL.char
80
+	( incr ) #0001 ADD2
81
+	( loop ) DUP2 LDR #00 NEQ ^print-label MUL JMPS 
71 82
 	POP2
72 83
 
73 84
 RTS                 
74 85
 
75
-@text1 [ Hello 20 World 0a00 ] ( store text with a linebreak and null byte )
86
+@text1 [ Hello 20 World 0a00 ] ( text with linebreak and null bytes )
76 87
 @text2 [ Welcome 20 to 20 UxnVM 0a00 ]
77 88
 
78 89
 |c000 @FRAME
... ...
@@ -95,14 +106,9 @@ RTS
95 106
 
96 107
 - Includes
97 108
 - Defines
98
-- Jump relative
99 109
 - Local loops
100 110
 - Jump helpers
101 111
 
102
-NOTE: OPCODES should not be relative, but there should be a relative accessor for addresses, like:
103
-
104
-$relative_name JMP
105
-
106 112
 ## Notes
107 113
 
108 114
 ### Conditional Jumping
... ...
@@ -308,6 +308,8 @@ pass1(FILE *f)
308 308
 				break;
309 309
 			case '=': addr += 4; break; /* STR helper (lit addr-hb addr-lb str) */
310 310
 			case '~': addr += 4; break; /* LDR helper (lit addr-hb addr-lb ldr) */
311
+			case '$': addr += 4; break; /* JSR helper (lit addr-hb addr-lb jsr) */
312
+			case '/': addr += 4; break; /* JMP helper (lit addr-hb addr-lb jmp) */
311 313
 			case ',': addr += 3; break;
312 314
 			case '.': addr += 2; break;
313 315
 			case '^': addr += 2; break; /* Relative jump: lit addr-offset */
... ...
@@ -347,15 +349,16 @@ pass2(FILE *f)
347 349
 		else if(w[0] == '^' && (l = findlabel(w + 1))) { 
348 350
 			int off = l->addr - p.ptr - 3;
349 351
 			if(off < -126 || off > 126){ printf("Address %s is too far(%d).\n", w, off); return 0; } 
350
-			printf("relative %s[%d]\n", w, l->addr - p.ptr - 4);
351 352
 			pushbyte((Sint8)(l->addr - p.ptr - 3), 1); l->refs++; 
352 353
 		}
353 354
 		else if(w[0] == ':') fscanf(f, "%s", w);
354 355
 		else if(w[0] == ';') fscanf(f, "%s", w);
355 356
 		else if(w[0] == '.' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 0); l->refs++; }
356 357
 		else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; }
357
-		else if(w[0] == '=' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w+1) == 2 ? "STR2" : "STR"), 0); l->refs++;}
358
-		else if(w[0] == '~' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w+1) == 2 ? "LDR2" : "LDR"), 0); l->refs++;}
358
+		else if(w[0] == '=' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "STR2" : "STR"), 0); l->refs++;}
359
+		else if(w[0] == '~' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "LDR2" : "LDR"), 0); l->refs++;}
360
+		else if(w[0] == '/' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JMP2"), 0); l->refs++;}
361
+		else if(w[0] == '$' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JSR2"), 0); l->refs++;}
359 362
 		else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 2) pushbyte(shex(w + 1), 1); 
360 363
 		else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 4) pushshort(shex(w + 1), 1);
361 364
 		else if(w[0] == '+' && sihx(w + 1) && slen(w + 1) == 2) pushbyte((Sint8)shex(w + 1), 1);
... ...
@@ -4,8 +4,8 @@
4 4
 
5 5
 |0100 @RESET 
6 6
 	
7
-	,text1 ,print-label JSR2
8
-	,text2 ,print-label JSR2
7
+	,text1 $print-label
8
+	,text2 $print-label
9 9
 	#ab =CNSL.byte
10 10
 	#cdef =CNSL.short
11 11
 
... ...
@@ -16,13 +16,15 @@ BRK
16 16
 	@print-label-loop NOP
17 17
 		( send ) DUP2 LDR =CNSL.char
18 18
 		( incr ) #0001 ADD2
19
-		DUP2 LDR #00 NEQ ^print-label-loop MUL JMPS 
19
+		( loop ) DUP2 LDR #00 NEQ ^print-label-loop MUL JMPS 
20 20
 	POP2
21 21
 
22
-RTS                 
22
+RTS    
23 23
 
24
-@text1 [ Hello 20 World 0a00 ] ( store text with a linebreak and null byte )
25
-@text2 [ Welcome 20 to 20 UxnVM 0a00 ]
24
+( store text in memory )
25
+
26
+@text1 [ Welcome 20 to 20 UxnVM 0a00 ]
27
+@text2 [ Hello 20 World 0a00 ] 
26 28
 
27 29
 |c000 @FRAME
28 30
 |d000 @ERROR 
... ...
@@ -211,9 +211,9 @@ RTS
211 211
 
212 212
 	( get file length )
213 213
 	,document.body =document.eof
214
-	@load-file-loop
214
+	@load-file-loop NOP
215 215
 		( incr ) ~document.eof #0001 ADD2 =document.eof
216
-	,load-file-loop ~document.eof LDR #00 NEQ JMP2? POP2
216
+	~document.eof LDR #00 NEQ ^load-file-loop MUL JMPS
217 217
 
218 218
 RTS
219 219
 
... ...
@@ -221,10 +221,10 @@ RTS
221 221
 
222 222
 	=i
223 223
 	~selection.from #0001 SUB2 =j ( start -> end )
224
-	@shift-left-loop
224
+	@shift-left-loop NOP
225 225
 		( move ) ~j ~i ADD2 LDR ~j STR
226 226
 		( incr ) ~j #0001 ADD2 =j
227
-		,shift-left-loop ~j ~document.eof LTH2 JMP2? POP2
227
+		~j ~document.eof LTH2 ^shift-left-loop MUL JMPS
228 228
 	~document.eof ~i SUB2 =document.eof
229 229
 
230 230
 RTS
... ...
@@ -233,10 +233,10 @@ RTS
233 233
 
234 234
 	=i
235 235
 	~document.eof =j ( end -> start )
236
-	@shift-right-loop
236
+	@shift-right-loop NOP
237 237
 		( move ) ~j ~i SUB2 LDR ~j STR
238 238
 		( decr ) ~j #0001 SUB2 =j
239
-		,shift-right-loop ~j ~selection.from GTH2 JMP2? POP2
239
+		~j ~selection.from GTH2 ^shift-right-loop MUL JMPS
240 240
 	~document.eof ~i ADD2 =document.eof
241 241
 
242 242
 RTS
... ...
@@ -250,11 +250,11 @@ RTS
250 250
 
251 251
 @goto-linestart
252 252
 
253
-	@goto-linestart-loop
253
+	@goto-linestart-loop NOP
254 254
 		~selection.from #0001 SUB2 LDR #0a EQU RTS?
255 255
 		~selection.from #0001 SUB2 LDR #0d EQU RTS?
256 256
 		( decr ) ~selection.from DUP2 =selection.to #0001 SUB2 =selection.from
257
-	,goto-linestart-loop ~selection.from LDR #00 NEQ JMP2? POP2
257
+	~selection.from LDR #00 NEQ ^goto-linestart-loop MUL JMPS
258 258
 	( clamp at document body )
259 259
 	~selection.from ,document.body GTH2 RTS?
260 260
 	,document.body DUP2 =selection.from #0001 ADD2 =selection.to
... ...
@@ -263,11 +263,11 @@ RTS
263 263
 
264 264
 @goto-lineend
265 265
 	
266
-	@goto-lineend-loop
266
+	@goto-lineend-loop NOP
267 267
 		~selection.from LDR #0a EQU RTS?
268 268
 		~selection.from LDR #0d EQU RTS?
269 269
 		( incr ) ~selection.from #0001 ADD2 DUP2 #0001 ADD2 =selection.to =selection.from
270
-		,goto-lineend-loop ~selection.from LDR #00 NEQ JMP2? POP2
270
+		~selection.from LDR #00 NEQ ^goto-lineend-loop MUL JMPS
271 271
 	( clamp at document body )
272 272
 	~selection.from ,document.eof LTH2 RTS?
273 273
 	,document.eof #0001 SUB2 DUP2 =selection.from #0001 ADD2 =selection.to
... ...
@@ -277,12 +277,12 @@ RTS
277 277
 @find-wordstart
278 278
 
279 279
 	~selection.to =j
280
-	@find-wordstart-loop
280
+	@find-wordstart-loop NOP
281 281
 		( decr ) ~j #0001 SUB2 =j
282 282
 		,find-wordstart-end ~j LDR #20 EQU JMP2? POP2
283 283
 		,find-wordstart-end ~j LDR #0a EQU JMP2? POP2
284 284
 		,find-wordstart-end ~j LDR #0d EQU JMP2? POP2
285
-		,find-wordstart-loop ~j ,document.body GTH2 JMP2? POP2
285
+		~j ,document.body GTH2 ^find-wordstart-loop MUL JMPS
286 286
 	@find-wordstart-end 
287 287
 	( return ) ~j #0001 SUB2
288 288
 
... ...
@@ -291,12 +291,12 @@ RTS
291 291
 @find-wordend
292 292
 	
293 293
 	~selection.to =j
294
-	@find-wordend-loop
294
+	@find-wordend-loop NOP
295 295
 		( incr ) ~j #0001 ADD2 =j
296 296
 		,find-wordend-end ~j LDR #20 EQU JMP2? POP2
297 297
 		,find-wordend-end ~j LDR #0a EQU JMP2? POP2
298 298
 		,find-wordend-end ~j LDR #0d EQU JMP2? POP2
299
-		,find-wordend-loop ~j ,document.body GTH2 JMP2? POP2
299
+		~j ,document.body GTH2 ^find-wordend-loop MUL JMPS
300 300
 	@find-wordend-end 
301 301
 	( return ) ~j #0001 ADD2
302 302
 
... ...
@@ -305,11 +305,11 @@ RTS
305 305
 @find-lineoffset ( return character offset from linestart )
306 306
 
307 307
 	#0000 =j
308
-	@find-lineoffset-loop
308
+	@find-lineoffset-loop NOP
309 309
 		( incr ) ~j #0001 ADD2 =j
310 310
 		,find-lineoffset-end ~selection.from ~j SUB2 LDR #0a EQU JMP2? POP2
311 311
 		,find-lineoffset-end ~selection.from ~j SUB2 LDR #0d EQU JMP2? POP2
312
-		,find-lineoffset-loop ~selection.from ~j SUB2 ,document.body GTH2 JMP2? POP2
312
+		~selection.from ~j SUB2 ,document.body GTH2 ^find-lineoffset-loop MUL JMPS
313 313
 	@find-lineoffset-end 
314 314
 	( return ) ~j
315 315
 
... ...
@@ -318,13 +318,13 @@ RTS
318 318
 @find-line ( position -> addr )
319 319
 
320 320
 	,document.body =j #0000 =pt.y
321
-	@find-line-loop
321
+	@find-line-loop NOP
322 322
 		,find-line-end ~pt.y ~position.y #0001 SUB2 GTH2 JMP2? POP2
323 323
 		,find-line-no-space ~j LDR #0a NEQ ~j LDR #0d NEQ #0101 EQU2 JMP2? POP2
324 324
 			( incr ) ~pt.y #0001 ADD2 =pt.y
325 325
 		@find-line-no-space
326 326
 		( incr ) ~j #0001 ADD2 =j
327
-	,find-line-loop ~j LDR #00 NEQ JMP2? POP2
327
+	~j LDR #00 NEQ ^find-line-loop MUL JMPS
328 328
 	@find-line-end
329 329
 	( return ) ~j
330 330
 
... ...
@@ -336,11 +336,11 @@ RTS
336 336
 
337 337
 	#0000 =pt.x
338 338
 
339
-	@find-selection-loop
339
+	@find-selection-loop NOP
340 340
 		,find-selection-end ~j ~pt.x ADD2 LDR #0a EQU JMP2? POP2
341 341
 		,find-selection-end ~j ~pt.x ADD2 LDR #0d EQU JMP2? POP2
342 342
 		( incr ) ~pt.x #0001 ADD2 =pt.x
343
-		,find-selection-loop ~pt.x ~position.x #0001 SUB2 LTH2 JMP2? POP2
343
+		~pt.x ~position.x #0001 SUB2 LTH2 ^find-selection-loop MUL JMPS
344 344
 	@find-selection-end
345 345
 	( return ) ~pt.x ADD2
346 346
 
... ...
@@ -351,17 +351,14 @@ RTS
351 351
 	,document.body =selection.from #0000 =pt.x #0000 =pt.y
352 352
 
353 353
 	@select-loop
354
-
355 354
 		,no-space ~selection.from LDR #0a NEQ ~selection.from LDR #0d NEQ #0101 EQU2 JMP2? POP2
356 355
 			( incr ) ~pt.y #0001 ADD2 =pt.y
357 356
 			#0000 =pt.x
358 357
 		@no-space
359
-
360 358
 		,no-reached ~pt.y ~position.y #0001 SUB2 GTH2 ~pt.x ~position.x #0001 SUB2 GTH2 #0101 NEQ2 JMP2? POP2
361 359
 			~selection.from #0001 ADD2 =selection.to
362 360
 			RTS
363 361
 		@no-reached
364
-
365 362
 		( incr ) ~pt.x #0001 ADD2 =pt.x
366 363
 		( incr ) ~selection.from #0001 ADD2 =selection.from
367 364
 	,select-loop ~selection.from LDR #00 NEQ JMP2? POP2
... ...
@@ -457,13 +454,13 @@ RTS
457 454
  
458 455
 	( scroll to position )
459 456
 	#0000 =j ( j is linebreaks )
460
-	@find-scroll-offset
457
+	@find-scroll-offset NOP
461 458
 		,find-scroll-offset-end ~scroll.y ~j EQU2 JMP2? POP2
462 459
 		,no-break ~textarea.addr LDR #0a NEQ ~textarea.addr LDR #0d NEQ #0101 EQU2 JMP2? POP2
463 460
 			( incr ) ~j #0001 ADD2 =j
464 461
 		@no-break
465 462
 		( incr ) ~textarea.addr #0001 ADD2 =textarea.addr
466
-	,find-scroll-offset ~textarea.addr LDR #00 NEQ JMP2? POP2
463
+	~textarea.addr LDR #00 NEQ ^find-scroll-offset MUL JMPS
467 464
 	@find-scroll-offset-end
468 465
 
469 466
 	~textarea.addr #0000 ADD2 =textarea.addr
... ...
@@ -542,11 +539,11 @@ RTS
542 539
 
543 540
 	( load ) =label.addr =label.color =SPRT.y =SPRT.x 
544 541
 	~label.addr
545
-	@draw-titlebar-loop
542
+	@draw-titlebar-loop NOP
546 543
 		( draw ) DUP2 LDR #00 SWP #20 SUB #0008 MUL2 ,font ADD2 =SPRT.addr ~label.color =SPRT.color
547 544
 		( incr ) #0001 ADD2
548 545
 		( incr ) ~SPRT.x #0008 ADD2 =SPRT.x
549
-		DUP2 LDR #00 NEQ ,draw-titlebar-loop ROT JMP2? POP2
546
+		DUP2 LDR #00 NEQ ^draw-titlebar-loop MUL JMPS
550 547
 	POP2
551 548
 
552 549
 	( selection )
... ...
@@ -116,27 +116,27 @@ BRK
116 116
 
117 117
 			,not-copy-mode ~bankview.mode #01 NEQ JMP2? POP2
118 118
 				#00 =i
119
-				@copy-loop
119
+				@copy-loop NOP
120 120
 					( load ) ~tileview.addr ~i ADD LDR 
121 121
 					( get touch addr )
122 122
 					~MOUS.x ~bankview.x SUB2 #0008 DIV2 #0008 MUL2 
123 123
 					~MOUS.y ~bankview.y SUB2 #0008 DIV2 #0008 MUL2 #0010 MUL2 ADD2
124 124
 					~bankview.addr ADD2 #00 ~i ADD2 STR
125 125
 					( incr ) ~i #01 ADD =i
126
-					,copy-loop ~i #08 LTH JMP2? POP2
126
+					~i #08 LTH ^copy-loop MUL JMPS
127 127
 				,redraw JSR2 ,click-end JMP2
128 128
 			@not-copy-mode
129 129
 
130 130
 			,not-erase-mode ~bankview.mode #02 NEQ JMP2? POP2
131 131
 				#00 =i
132
-				@erase-loop
132
+				@erase-loop NOP
133 133
 					#00 
134 134
 					( get touch addr )
135 135
 					~MOUS.x ~bankview.x SUB2 #0008 DIV2 #0008 MUL2 
136 136
 					~MOUS.y ~bankview.y SUB2 #0008 DIV2 #0008 MUL2 #0010 MUL2 ADD2
137 137
 					~bankview.addr ADD2 #00 ~i ADD2 STR
138 138
 					( incr ) ~i #01 ADD =i
139
-					,erase-loop ~i #08 LTH JMP2? POP2
139
+					~i #08 LTH ^erase-loop MUL JMPS
140 140
 				,redraw JSR2 ,click-end JMP2
141 141
 			@not-erase-mode
142 142
 
... ...
@@ -470,17 +470,17 @@ RTS
470 470
 @line-rect ( x1 y1 x2 y2 color )
471 471
 
472 472
 	( load ) =color =rect.y2 =rect.x2 DUP2 =SCRN.y =rect.y1 DUP2 =SCRN.x =rect.x1
473
-	@line-rect-hor
473
+	@line-rect-hor NOP
474 474
 		( incr ) ~SCRN.x #0001 ADD2 =SCRN.x
475 475
 		( draw ) ~rect.y1 =SCRN.y ~color =SCRN.color
476 476
 		( draw ) ~rect.y2 =SCRN.y ~color =SCRN.color
477
-		,line-rect-hor ~SCRN.x ~rect.x2 LTH2 JMP2? POP2
477
+		~SCRN.x ~rect.x2 LTH2 ^line-rect-hor MUL JMPS
478 478
 	~rect.y1 =SCRN.y
479
-	@line-rect-ver
479
+	@line-rect-ver NOP
480 480
 		( draw ) ~rect.x1 =SCRN.x ~color =SCRN.color
481 481
 		( draw ) ~rect.x2 =SCRN.x ~color =SCRN.color
482 482
 		( incr ) ~SCRN.y #0001 ADD2 =SCRN.y
483
-		,line-rect-ver ~SCRN.y ~rect.y2 #0001 ADD2 LTH2 JMP2? POP2
483
+		~SCRN.y ~rect.y2 #0001 ADD2 LTH2 ^line-rect-ver MUL JMPS
484 484
 
485 485
 RTS
486 486