-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathstdlib_hex.asm
More file actions
786 lines (786 loc) · 21.3 KB
/
stdlib_hex.asm
File metadata and controls
786 lines (786 loc) · 21.3 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
org 32768
.core.__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld (.core.__CALL_BACK__), sp
ei
call .core.__MEM_INIT
jp .core.__MAIN_PROGRAM__
.core.__CALL_BACK__:
DEFW 0
.core.ZXBASIC_USER_DATA:
; Defines HEAP SIZE
.core.ZXBASIC_HEAP_SIZE EQU 4768
.core.ZXBASIC_MEM_HEAP:
DEFS 4768
; Defines USER DATA Length in bytes
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
ld hl, 0
ld b, h
ld c, l
.core.__END_PROGRAM:
di
ld hl, (.core.__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
_hex:
#line 30 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
push namespace core
PROC
LOCAL SUB_CHAR
LOCAL SUB_CHAR2
LOCAL END_CHAR
LOCAL DIGIT
push hl
push de
ld bc,10
call __MEM_ALLOC
ld a, h
or l
pop de
pop bc
ret z
push hl
ld (hl), 8
inc hl
ld (hl), 0
inc hl
call DIGIT
ld d, e
call DIGIT
ld d, b
call DIGIT
ld d, c
call DIGIT
pop hl
ret
DIGIT:
ld a, d
call SUB_CHAR
ld a, d
jr SUB_CHAR2
SUB_CHAR:
rrca
rrca
rrca
rrca
SUB_CHAR2:
and 0Fh
add a, '0'
cp '9' + 1
jr c, END_CHAR
add a, 7
END_CHAR:
ld (hl), a
inc hl
ret
ENDP
pop namespace
#line 91 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
_hex__leave:
ret
_hex16:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
ld l, (ix+4)
ld h, (ix+5)
ld de, 0
call _hex
ld d, h
ld e, l
ld bc, -2
call .core.__PSTORE_STR2
ld l, (ix-2)
ld h, (ix-1)
push hl
ld hl, 4
push hl
ld hl, 7
push hl
xor a
call .core.__STRSLICE
_hex16__leave:
ex af, af'
exx
ld l, (ix-2)
ld h, (ix-1)
call .core.__MEM_FREE
ex af, af'
exx
ld sp, ix
pop ix
exx
pop hl
ex (sp), hl
exx
ret
_hex8:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
ld a, (ix+5)
ld l, a
ld h, 0
ld e, h
ld d, h
call _hex
ld d, h
ld e, l
ld bc, -2
call .core.__PSTORE_STR2
ld l, (ix-2)
ld h, (ix-1)
push hl
ld hl, 6
push hl
ld hl, 7
push hl
xor a
call .core.__STRSLICE
_hex8__leave:
ex af, af'
exx
ld l, (ix-2)
ld h, (ix-1)
call .core.__MEM_FREE
ex af, af'
exx
ld sp, ix
pop ix
exx
pop hl
ex (sp), hl
exx
ret
;; --- end of user code ---
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/mem/alloc.asm"
; vim: ts=4:et:sw=4:
; Copyleft (K) by Jose M. Rodriguez de la Rosa
; (a.k.a. Boriel)
; http://www.boriel.com
;
; This ASM library is licensed under the MIT license
; you can use it for any purpose (even for commercial
; closed source programs).
;
; Please read the MIT license on the internet
; ----- IMPLEMENTATION NOTES ------
; The heap is implemented as a linked list of free blocks.
; Each free block contains this info:
;
; +----------------+ <-- HEAP START
; | Size (2 bytes) |
; | 0 | <-- Size = 0 => DUMMY HEADER BLOCK
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| | <-- If Size > 4, then this contains (size - 4) bytes
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ |
; <Allocated> | <-- This zone is in use (Already allocated)
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Next (2 bytes) |--> NULL => END OF LIST
; | 0 = NULL |
; +----------------+
; | <free bytes...>|
; | (0 if Size = 4)|
; +----------------+
; When a block is FREED, the previous and next pointers are examined to see
; if we can defragment the heap. If the block to be freed is just next to the
; previous, or to the next (or both) they will be converted into a single
; block (so defragmented).
; MEMORY MANAGER
;
; This library must be initialized calling __MEM_INIT with
; HL = BLOCK Start & DE = Length.
; An init directive is useful for initialization routines.
; They will be added automatically if needed.
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/error.asm"
; Simple error control routines
; vim:ts=4:et:
push namespace core
ERR_NR EQU 23610 ; Error code system variable
; Error code definitions (as in ZX spectrum manual)
; Set error code with:
; ld a, ERROR_CODE
; ld (ERR_NR), a
ERROR_Ok EQU -1
ERROR_SubscriptWrong EQU 2
ERROR_OutOfMemory EQU 3
ERROR_OutOfScreen EQU 4
ERROR_NumberTooBig EQU 5
ERROR_InvalidArg EQU 9
ERROR_IntOutOfRange EQU 10
ERROR_NonsenseInBasic EQU 11
ERROR_InvalidFileName EQU 14
ERROR_InvalidColour EQU 19
ERROR_BreakIntoProgram EQU 20
ERROR_TapeLoadingErr EQU 26
; Raises error using RST #8
__ERROR:
ld (__ERROR_CODE), a
rst 8
__ERROR_CODE:
nop
ret
; Sets the error system variable, but keeps running.
; Usually this instruction if followed by the END intermediate instruction.
__STOP:
ld (ERR_NR), a
ret
pop namespace
#line 69 "/zxbasic/src/lib/arch/zx48k/runtime/mem/alloc.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/mem/heapinit.asm"
; vim: ts=4:et:sw=4:
; Copyleft (K) by Jose M. Rodriguez de la Rosa
; (a.k.a. Boriel)
; http://www.boriel.com
;
; This ASM library is licensed under the BSD license
; you can use it for any purpose (even for commercial
; closed source programs).
;
; Please read the BSD license on the internet
; ----- IMPLEMENTATION NOTES ------
; The heap is implemented as a linked list of free blocks.
; Each free block contains this info:
;
; +----------------+ <-- HEAP START
; | Size (2 bytes) |
; | 0 | <-- Size = 0 => DUMMY HEADER BLOCK
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| | <-- If Size > 4, then this contains (size - 4) bytes
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ |
; <Allocated> | <-- This zone is in use (Already allocated)
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Next (2 bytes) |--> NULL => END OF LIST
; | 0 = NULL |
; +----------------+
; | <free bytes...>|
; | (0 if Size = 4)|
; +----------------+
; When a block is FREED, the previous and next pointers are examined to see
; if we can defragment the heap. If the block to be breed is just next to the
; previous, or to the next (or both) they will be converted into a single
; block (so defragmented).
; MEMORY MANAGER
;
; This library must be initialized calling __MEM_INIT with
; HL = BLOCK Start & DE = Length.
; An init directive is useful for initialization routines.
; They will be added automatically if needed.
; ---------------------------------------------------------------------
; __MEM_INIT must be called to initalize this library with the
; standard parameters
; ---------------------------------------------------------------------
push namespace core
__MEM_INIT: ; Initializes the library using (RAMTOP) as start, and
ld hl, ZXBASIC_MEM_HEAP ; Change this with other address of heap start
ld de, ZXBASIC_HEAP_SIZE ; Change this with your size
; ---------------------------------------------------------------------
; __MEM_INIT2 initalizes this library
; Parameters:
; HL : Memory address of 1st byte of the memory heap
; DE : Length in bytes of the Memory Heap
; ---------------------------------------------------------------------
__MEM_INIT2:
; HL as TOP
PROC
dec de
dec de
dec de
dec de ; DE = length - 4; HL = start
; This is done, because we require 4 bytes for the empty dummy-header block
xor a
ld (hl), a
inc hl
ld (hl), a ; First "free" block is a header: size=0, Pointer=&(Block) + 4
inc hl
ld b, h
ld c, l
inc bc
inc bc ; BC = starts of next block
ld (hl), c
inc hl
ld (hl), b
inc hl ; Pointer to next block
ld (hl), e
inc hl
ld (hl), d
inc hl ; Block size (should be length - 4 at start); This block contains all the available memory
ld (hl), a ; NULL (0000h) ; No more blocks (a list with a single block)
inc hl
ld (hl), a
ld a, 201
ld (__MEM_INIT), a; "Pokes" with a RET so ensure this routine is not called again
ret
ENDP
pop namespace
#line 70 "/zxbasic/src/lib/arch/zx48k/runtime/mem/alloc.asm"
; ---------------------------------------------------------------------
; MEM_ALLOC
; Allocates a block of memory in the heap.
;
; Parameters
; BC = Length of requested memory block
;
; Returns:
; HL = Pointer to the allocated block in memory. Returns 0 (NULL)
; if the block could not be allocated (out of memory)
; ---------------------------------------------------------------------
push namespace core
MEM_ALLOC:
__MEM_ALLOC: ; Returns the 1st free block found of the given length (in BC)
PROC
LOCAL __MEM_LOOP
LOCAL __MEM_DONE
LOCAL __MEM_SUBTRACT
LOCAL __MEM_START
LOCAL TEMP, TEMP0
TEMP EQU TEMP0 + 1
ld hl, 0
ld (TEMP), hl
__MEM_START:
ld hl, ZXBASIC_MEM_HEAP ; This label point to the heap start
inc bc
inc bc ; BC = BC + 2 ; block size needs 2 extra bytes for hidden pointer
__MEM_LOOP: ; Loads lengh at (HL, HL+). If Lenght >= BC, jump to __MEM_DONE
ld a, h ; HL = NULL (No memory available?)
or l
#line 113 "/zxbasic/src/lib/arch/zx48k/runtime/mem/alloc.asm"
ret z ; NULL
#line 115 "/zxbasic/src/lib/arch/zx48k/runtime/mem/alloc.asm"
; HL = Pointer to Free block
ld e, (hl)
inc hl
ld d, (hl)
inc hl ; DE = Block Length
push hl ; HL = *pointer to -> next block
ex de, hl
or a ; CF = 0
sbc hl, bc ; FREE >= BC (Length) (HL = BlockLength - Length)
jp nc, __MEM_DONE
pop hl
ld (TEMP), hl
ex de, hl
ld e, (hl)
inc hl
ld d, (hl)
ex de, hl
jp __MEM_LOOP
__MEM_DONE: ; A free block has been found.
; Check if at least 4 bytes remains free (HL >= 4)
push hl
exx ; exx to preserve bc
pop hl
ld bc, 4
or a
sbc hl, bc
exx
jp nc, __MEM_SUBTRACT
; At this point...
; less than 4 bytes remains free. So we return this block entirely
; We must link the previous block with the next to this one
; (DE) => Pointer to next block
; (TEMP) => &(previous->next)
pop hl ; Discard current block pointer
push de
ex de, hl ; DE = Previous block pointer; (HL) = Next block pointer
ld a, (hl)
inc hl
ld h, (hl)
ld l, a ; HL = (HL)
ex de, hl ; HL = Previous block pointer; DE = Next block pointer
TEMP0:
ld hl, 0 ; Pre-previous block pointer
ld (hl), e
inc hl
ld (hl), d ; LINKED
pop hl ; Returning block.
ret
__MEM_SUBTRACT:
; At this point we have to store HL value (Length - BC) into (DE - 2)
ex de, hl
dec hl
ld (hl), d
dec hl
ld (hl), e ; Store new block length
add hl, de ; New length + DE => free-block start
pop de ; Remove previous HL off the stack
ld (hl), c ; Store length on its 1st word
inc hl
ld (hl), b
inc hl ; Return hl
ret
ENDP
pop namespace
#line 174 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/mem/free.asm"
; vim: ts=4:et:sw=4:
; Copyleft (K) by Jose M. Rodriguez de la Rosa
; (a.k.a. Boriel)
; http://www.boriel.com
;
; This ASM library is licensed under the BSD license
; you can use it for any purpose (even for commercial
; closed source programs).
;
; Please read the BSD license on the internet
; ----- IMPLEMENTATION NOTES ------
; The heap is implemented as a linked list of free blocks.
; Each free block contains this info:
;
; +----------------+ <-- HEAP START
; | Size (2 bytes) |
; | 0 | <-- Size = 0 => DUMMY HEADER BLOCK
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| | <-- If Size > 4, then this contains (size - 4) bytes
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ |
; <Allocated> | <-- This zone is in use (Already allocated)
; +----------------+ <-+
; | Size (2 bytes) |
; +----------------+
; | Next (2 bytes) |---+
; +----------------+ |
; | <free bytes...>| |
; | (0 if Size = 4)| |
; +----------------+ <-+
; | Next (2 bytes) |--> NULL => END OF LIST
; | 0 = NULL |
; +----------------+
; | <free bytes...>|
; | (0 if Size = 4)|
; +----------------+
; When a block is FREED, the previous and next pointers are examined to see
; if we can defragment the heap. If the block to be breed is just next to the
; previous, or to the next (or both) they will be converted into a single
; block (so defragmented).
; MEMORY MANAGER
;
; This library must be initialized calling __MEM_INIT with
; HL = BLOCK Start & DE = Length.
; An init directive is useful for initialization routines.
; They will be added automatically if needed.
; ---------------------------------------------------------------------
; MEM_FREE
; Frees a block of memory
;
; Parameters:
; HL = Pointer to the block to be freed. If HL is NULL (0) nothing
; is done
; ---------------------------------------------------------------------
push namespace core
MEM_FREE:
__MEM_FREE: ; Frees the block pointed by HL
; HL DE BC & AF modified
PROC
LOCAL __MEM_LOOP2
LOCAL __MEM_LINK_PREV
LOCAL __MEM_JOIN_TEST
LOCAL __MEM_BLOCK_JOIN
ld a, h
or l
ret z ; Return if NULL pointer
dec hl
dec hl
ld b, h
ld c, l ; BC = Block pointer
ld hl, ZXBASIC_MEM_HEAP ; This label point to the heap start
__MEM_LOOP2:
inc hl
inc hl ; Next block ptr
ld e, (hl)
inc hl
ld d, (hl) ; Block next ptr
ex de, hl ; DE = &(block->next); HL = block->next
ld a, h ; HL == NULL?
or l
jp z, __MEM_LINK_PREV; if so, link with previous
or a ; Clear carry flag
sbc hl, bc ; Carry if BC > HL => This block if before
add hl, bc ; Restores HL, preserving Carry flag
jp c, __MEM_LOOP2 ; This block is before. Keep searching PASS the block
;------ At this point current HL is PAST BC, so we must link (DE) with BC, and HL in BC->next
__MEM_LINK_PREV: ; Link (DE) with BC, and BC->next with HL
ex de, hl
push hl
dec hl
ld (hl), c
inc hl
ld (hl), b ; (DE) <- BC
ld h, b ; HL <- BC (Free block ptr)
ld l, c
inc hl ; Skip block length (2 bytes)
inc hl
ld (hl), e ; Block->next = DE
inc hl
ld (hl), d
; --- LINKED ; HL = &(BC->next) + 2
call __MEM_JOIN_TEST
pop hl
__MEM_JOIN_TEST: ; Checks for fragmented contiguous blocks and joins them
; hl = Ptr to current block + 2
ld d, (hl)
dec hl
ld e, (hl)
dec hl
ld b, (hl) ; Loads block length into BC
dec hl
ld c, (hl) ;
push hl ; Saves it for later
add hl, bc ; Adds its length. If HL == DE now, it must be joined
or a
sbc hl, de ; If Z, then HL == DE => We must join
pop hl
ret nz
__MEM_BLOCK_JOIN: ; Joins current block (pointed by HL) with next one (pointed by DE). HL->length already in BC
push hl ; Saves it for later
ex de, hl
ld e, (hl) ; DE -> block->next->length
inc hl
ld d, (hl)
inc hl
ex de, hl ; DE = &(block->next)
add hl, bc ; HL = Total Length
ld b, h
ld c, l ; BC = Total Length
ex de, hl
ld e, (hl)
inc hl
ld d, (hl) ; DE = block->next
pop hl ; Recovers Pointer to block
ld (hl), c
inc hl
ld (hl), b ; Length Saved
inc hl
ld (hl), e
inc hl
ld (hl), d ; Next saved
ret
ENDP
pop namespace
#line 175 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/pstorestr2.asm"
; vim:ts=4:et:sw=4
;
; Stores an string (pointer to the HEAP by DE) into the address pointed
; by (IX + BC). No new copy of the string is created into the HEAP, since
; it's supposed it's already created (temporary string)
;
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/storestr2.asm"
; Similar to __STORE_STR, but this one is called when
; the value of B$ if already duplicated onto the stack.
; So we needn't call STRASSING to create a duplication
; HL = address of string memory variable
; DE = address of 2n string. It just copies DE into (HL)
; freeing (HL) previously.
push namespace core
__PISTORE_STR2: ; Indirect store temporary string at (IX + BC)
push ix
pop hl
add hl, bc
__ISTORE_STR2:
ld c, (hl) ; Dereferences HL
inc hl
ld h, (hl)
ld l, c ; HL = *HL (real string variable address)
__STORE_STR2:
push hl
ld c, (hl)
inc hl
ld h, (hl)
ld l, c ; HL = *HL (real string address)
push de
call __MEM_FREE
pop de
pop hl
ld (hl), e
inc hl
ld (hl), d
dec hl ; HL points to mem address variable. This might be useful in the future.
ret
pop namespace
#line 9 "/zxbasic/src/lib/arch/zx48k/runtime/pstorestr2.asm"
push namespace core
__PSTORE_STR2:
push ix
pop hl
add hl, bc
jp __STORE_STR2
pop namespace
#line 176 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/strslice.asm"
; String slicing library
; HL = Str pointer
; DE = String start
; BC = String character end
; A register => 0 => the HL pointer wont' be freed from the HEAP
; e.g. a$(5 TO 10) => HL = a$; DE = 5; BC = 10
; This implements a$(X to Y) being X and Y first and
; last characters respectively. If X > Y, NULL is returned
; Otherwise returns a pointer to a$ FROM X to Y (starting from 0)
; if Y > len(a$), then a$ will be padded with spaces (reallocating
; it in dynamic memory if needed). Returns pointer (HL) to resulting
; string. NULL (0) if no memory for padding.
;
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/strlen.asm"
; Returns len if a string
; If a string is NULL, its len is also 0
; Result returned in HL
push namespace core
__STRLEN: ; Direct FASTCALL entry
ld a, h
or l
ret z
ld a, (hl)
inc hl
ld h, (hl) ; LEN(str) in HL
ld l, a
ret
pop namespace
#line 18 "/zxbasic/src/lib/arch/zx48k/runtime/strslice.asm"
push namespace core
__STRSLICE: ; Callee entry
pop hl ; Return ADDRESS
pop bc ; Last char pos
pop de ; 1st char pos
ex (sp), hl ; CALLEE. -> String start
__STRSLICE_FAST: ; __FASTCALL__ Entry
PROC
LOCAL __CONT
LOCAL __EMPTY
LOCAL __FREE_ON_EXIT
push hl ; Stores original HL pointer to be recovered on exit
ex af, af' ; Saves A register for later
push hl
call __STRLEN
inc bc ; Last character position + 1 (string starts from 0)
or a
sbc hl, bc ; Compares length with last char position
jr nc, __CONT ; If Carry => We must copy to end of string
add hl, bc ; Restore back original LEN(a$) in HL
ld b, h
ld c, l ; Copy to the end of str
ccf ; Clears Carry flag for next subtraction
__CONT:
ld h, b
ld l, c ; HL = Last char position to copy (1 for char 0, 2 for char 1, etc)
sbc hl, de ; HL = LEN(a$) - DE => Number of chars to copy
jr z, __EMPTY ; 0 Chars to copy => Return HL = 0 (NULL STR)
jr c, __EMPTY ; If Carry => Nothing to return (NULL STR)
ld b, h
ld c, l ; BC = Number of chars to copy
inc bc
inc bc ; +2 bytes for string length number
push bc
push de
call __MEM_ALLOC
pop de
pop bc
ld a, h
or l
jr z, __EMPTY ; Return if NULL (no memory)
dec bc
dec bc ; Number of chars to copy (Len of slice)
ld (hl), c
inc hl
ld (hl), b
inc hl ; Stores new string length
ex (sp), hl ; Pointer to A$ now in HL; Pointer to new string chars in Stack
inc hl
inc hl ; Skip string length
add hl, de ; Were to start from A$
pop de ; Start of new string chars
push de ; Stores it again
ldir ; Copies BC chars
pop de
dec de
dec de ; Points to String LEN start
ex de, hl ; Returns it in HL
jr __FREE_ON_EXIT
__EMPTY: ; Return NULL (empty) string
pop hl
ld hl, 0 ; Return NULL
__FREE_ON_EXIT:
ex af, af' ; Recover original A register
ex (sp), hl ; Original HL pointer
or a
call nz, __MEM_FREE
pop hl ; Recover result
ret
ENDP
pop namespace
#line 177 "/zxbasic/src/lib/arch/zx48k/stdlib/hex.bas"
END