1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
# some primitives for moving the cursor without making assumptions about
# raster order
fn cursor-left screen: (addr screen) {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
compare cursor-x, 0
{
break-if->
return
}
cursor-x <- decrement
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, cursor-x, cursor-y, space
}
fn cursor-right screen: (addr screen) {
var _width/eax: int <- copy 0
var dummy/ecx: int <- copy 0
_width, dummy <- screen-size screen
var width/edx: int <- copy _width
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
compare cursor-x, width
{
break-if-<
return
}
cursor-x <- increment
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, cursor-x, cursor-y, space
}
fn cursor-up screen: (addr screen) {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
compare cursor-y, 0
{
break-if->
return
}
cursor-y <- decrement
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, cursor-x, cursor-y, space
}
fn cursor-down screen: (addr screen) {
var dummy/eax: int <- copy 0
var _height/ecx: int <- copy 0
dummy, _height <- screen-size screen
var height/edx: int <- copy _height
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
compare cursor-y, height
{
break-if-<
return
}
cursor-y <- increment
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, cursor-x, cursor-y, space
}
fn draw-grapheme-at-cursor screen: (addr screen), g: grapheme, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
draw-grapheme screen, g, cursor-x, cursor-y, color
}
# draw a single line of text from x, y to xmax
# return the next 'x' coordinate
# if there isn't enough space, return 0 without modifying the screen
fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, xmax: int, y: int, color: int -> _/eax: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write stream, text
# check if we have enough space
var xcurr/ecx: int <- copy x
{
compare xcurr, xmax
break-if->
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
xcurr <- increment
loop
}
compare xcurr, xmax
{
break-if-<=
return 0
}
# we do; actually draw
rewind-stream stream
xcurr <- copy x
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, xcurr, y, color
xcurr <- increment
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, xcurr, y, space # we'll assume it's ok to clear the next grapheme
return xcurr
}
fn draw-text-rightward-from-cursor screen: (addr screen), text: (addr array byte), xmax: int, color: int -> _/eax: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var result/eax: int <- draw-text-rightward screen, text, cursor-x, xmax, cursor-y, color
return result
}
# draw text in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary
# return the next (x, y) coordinate in raster order where drawing stopped
# that way the caller can draw more if given the same min and max bounding-box.
# if there isn't enough space, return 0 without modifying the screen
fn draw-text-wrapping-right-then-down screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write stream, text
# check if we have enough space
var xcurr/edx: int <- copy x
var ycurr/ecx: int <- copy y
{
compare ycurr, ymax
break-if->=
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
compare ycurr, ymax
{
break-if-<
return 0, 0
}
# we do; actually draw
rewind-stream stream
xcurr <- copy x
ycurr <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, xcurr, ycurr, space # we'll assume it's ok to clear the next grapheme
return xcurr, ycurr
}
fn move-cursor-rightward-and-downward screen: (addr screen), xmin: int, xmax: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
cursor-x <- increment
compare cursor-x, xmax
{
break-if-<
cursor-x <- copy xmin
cursor-y <- increment
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, cursor-x, cursor-y, space # we'll assume it's ok to clear the grapheme at the cursor
}
fn draw-text-wrapping-right-then-down-over-full-screen screen: (addr screen), text: (addr array byte), x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0
x2, y2 <- screen-size screen # width, height
x2, y2 <- draw-text-wrapping-right-then-down screen, text, 0, 0, x2, y2, x, y, color
return x2, y2 # cursor-x, cursor-y
}
fn draw-text-wrapping-right-then-down-from-cursor screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var end-x/edx: int <- copy cursor-x
end-x <- increment
compare end-x, xmax
{
break-if-<
cursor-x <- copy xmin
cursor-y <- increment
}
cursor-x, cursor-y <- draw-text-wrapping-right-then-down screen, text, xmin, ymin, xmax, ymax, cursor-x, cursor-y, color
}
fn draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen: (addr screen), text: (addr array byte), color: int {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
draw-text-wrapping-right-then-down-from-cursor screen, text, 0, 0, width, height, color
}
fn draw-int32-hex-wrapping-right-then-down screen: (addr screen), n: int, xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write-int32-hex stream, n
# check if we have enough space
var xcurr/edx: int <- copy x
var ycurr/ecx: int <- copy y
{
compare ycurr, ymax
break-if->=
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
compare ycurr, ymax
{
break-if-<
return 0, 0
}
# we do; actually draw
rewind-stream stream
xcurr <- copy x
ycurr <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, xcurr, ycurr, space # we'll assume it's ok to clear the next grapheme
return xcurr, ycurr
}
fn draw-int32-hex-wrapping-right-then-down-over-full-screen screen: (addr screen), n: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0
x2, y2 <- screen-size screen # width, height
x2, y2 <- draw-int32-hex-wrapping-right-then-down screen, n, 0, 0, x2, y2, x, y, color
return x2, y2 # cursor-x, cursor-y
}
fn draw-int32-hex-wrapping-right-then-down-from-cursor screen: (addr screen), n: int, xmin: int, ymin: int, xmax: int, ymax: int, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var end-x/edx: int <- copy cursor-x
end-x <- increment
compare end-x, xmax
{
break-if-<
cursor-x <- copy xmin
cursor-y <- increment
}
cursor-x, cursor-y <- draw-int32-hex-wrapping-right-then-down screen, n, xmin, ymin, xmax, ymax, cursor-x, cursor-y, color
}
fn draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen: (addr screen), n: int, color: int {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
draw-int32-hex-wrapping-right-then-down-from-cursor screen, n, 0, 0, width, height, color
}
fn draw-int32-decimal-wrapping-right-then-down screen: (addr screen), n: int, xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write-int32-decimal stream, n
# check if we have enough space
var xcurr/edx: int <- copy x
var ycurr/ecx: int <- copy y
{
compare ycurr, ymax
break-if->=
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
compare ycurr, ymax
{
break-if-<
return 0, 0
}
# we do; actually draw
rewind-stream stream
xcurr <- copy x
ycurr <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, xcurr, ycurr, space # we'll assume it's ok to clear the next grapheme
return xcurr, ycurr
}
fn draw-int32-decimal-wrapping-right-then-down-over-full-screen screen: (addr screen), n: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0
x2, y2 <- screen-size screen # width, height
x2, y2 <- draw-int32-decimal-wrapping-right-then-down screen, n, 0, 0, x2, y2, x, y, color
return x2, y2 # cursor-x, cursor-y
}
fn draw-int32-decimal-wrapping-right-then-down-from-cursor screen: (addr screen), n: int, xmin: int, ymin: int, xmax: int, ymax: int, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var end-x/edx: int <- copy cursor-x
end-x <- increment
compare end-x, xmax
{
break-if-<
cursor-x <- copy xmin
cursor-y <- increment
}
cursor-x, cursor-y <- draw-int32-decimal-wrapping-right-then-down screen, n, xmin, ymin, xmax, ymax, cursor-x, cursor-y, color
}
fn draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen: (addr screen), n: int, color: int {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
draw-int32-decimal-wrapping-right-then-down-from-cursor screen, n, 0, 0, width, height, color
}
## Text direction: down then right
# draw a single line of text vertically from x, y to ymax
# return the next 'y' coordinate
# if there isn't enough space, return 0 without modifying the screen
fn draw-text-downward screen: (addr screen), text: (addr array byte), x: int, y: int, ymax: int, color: int -> _/eax: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write stream, text
# check if we have enough space
var ycurr/ecx: int <- copy y
{
compare ycurr, ymax
break-if->
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
ycurr <- increment
loop
}
compare ycurr, ymax
{
break-if-<=
return 0
}
# we do; actually draw
rewind-stream stream
ycurr <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, x, ycurr, color
ycurr <- increment
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, x, ycurr, space # we'll assume it's ok to clear the next grapheme
return ycurr
}
fn draw-text-downward-from-cursor screen: (addr screen), text: (addr array byte), ymax: int, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var result/eax: int <- draw-text-downward screen, text, cursor-x, cursor-y, ymax, color
}
# draw text down and right in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary
# return the next (x, y) coordinate in raster order where drawing stopped
# that way the caller can draw more if given the same min and max bounding-box.
# if there isn't enough space, return 0 without modifying the screen
fn draw-text-wrapping-down-then-right screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write stream, text
# check if we have enough space
var xcurr/edx: int <- copy x
var ycurr/ecx: int <- copy y
{
compare xcurr, xmax
break-if->=
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
ycurr <- increment
compare ycurr, ymax
{
break-if-<
xcurr <- increment
ycurr <- copy ymin
}
loop
}
compare xcurr, xmax
{
break-if-<
return 0, 0
}
# we do; actually draw
rewind-stream stream
xcurr <- copy x
ycurr <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff # end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color
ycurr <- increment
compare ycurr, ymax
{
break-if-<
xcurr <- increment
ycurr <- copy ymin
}
loop
}
var space/esi: grapheme <- copy 0x20
set-cursor-position screen, xcurr, ycurr, space # we'll assume it's ok to clear the next grapheme
return xcurr, ycurr
}
fn draw-text-wrapping-down-then-right-over-full-screen screen: (addr screen), text: (addr array byte), x: int, y: int, color: int -> _/eax: int, _/ecx: int {
var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0
x2, y2 <- screen-size screen # width, height
x2, y2 <- draw-text-wrapping-down-then-right screen, text, 0, 0, x2, y2, x, y, color
return x2, y2 # cursor-x, cursor-y
}
fn draw-text-wrapping-down-then-right-from-cursor screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, color: int {
var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen
var end-y/edx: int <- copy cursor-y
end-y <- increment
compare end-y, ymax
{
break-if-<
cursor-x <- increment
cursor-y <- copy ymin
}
cursor-x, cursor-y <- draw-text-wrapping-down-then-right screen, text, xmin, ymin, xmax, ymax, cursor-x, cursor-y, color
}
fn draw-text-wrapping-down-then-right-from-cursor-over-full-screen screen: (addr screen), text: (addr array byte), color: int {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
draw-text-wrapping-down-then-right-from-cursor screen, text, 0, 0, width, height, color
}
|