forked from hperaza/ZSM4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.mac
1468 lines (1371 loc) · 31 KB
/
macros.mac
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;**********************************************************************;
; ;
; This file is part of ZSM4, a Z80/Z180/Z280 relocatable macro- ;
; assembler written in Z80 assembly. ;
; Copyright (C) 2017-2020, Hector Peraza. ;
; ;
; This program is free software; you can redistribute it and/or ;
; modify it under the terms of the GNU General Public License as ;
; published by the Free Software Foundation; either version 2 of ;
; the License, or (at your option) any later version. ;
; ;
; This program is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with this program; if not, write to the Free Software ;
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;
; ;
;**********************************************************************;
TITLE Z80/Z180/Z280 Macro-Assembler
SUBTTL 'MACRO support'
.Z80
include ZSM.INC
; Macro types
.MACRO equ 1
.REPT equ 2
.IRP equ 3
.IRPC equ 4
public DEFMAC,ENDMAC,MSTORE,MDFLVL,MACLVL,DFREPT
public DFIRP,DFIRPC,GNRMAC,FNDMAC,EXPMAC,GETSTR
public MACPTR,CVTNUM,LCLNUM
extrn ID,REC,SYMPTR,NEWSYM,DSPTR,IDADR,IDBUF,IDLEN
extrn GNC,ERRFLG,PTR1,FNDOPC,EVALNEW,RELERR,OERROR
extrn VALID,BACKUP,EVMODE,WERROR,FNDREC,FNDNXT,MFREE
extrn CNV2HX,MCHECK,MACCHK,ERRQ,GNR,RADIX,SYMLUK
extrn SYMTBL,GBCOL,CNDSTK,CONDSP,CLEVEL
cseg
SUBTTL 'MACRO pseudo-operator'
;
; Macro structure as saved in high memory:
;
; DSPTR:
; db STMDEF ; type = MACRO def
; dw len ; length of following block:
; db len,'name' ; macro name
; db nlocal ; number of local variables
; db 'body line 1',0 ; macro body, dummy params replaced by
; db 'body line 2',0 ; 80h + param number, local vars by
; db 'body line 3',0 ; 0C0h + var number
; db ... ;
; db EOF ; end of macro marker
;
; Macros are built first on low memory (following symbol table)
; and moved to upper memory when the final size is known.
;
; SYMPTR:
; db len,'name' ; macro name (old var name)
; db len,'arg1' ; dummy param list, needed only for scanning
; db len,'arg2' ; of macro definition body and will not be
; db len,'arg3' ; copied to high mem, len has the hi-bit
; db len,'arg4' ; set if it corresponds to a local var
; db ... ;
; db 0FFh ; end of args
; db nlocal ; number of local variables
; db 'body line 1',0 ; params and local variables replaced by
; db 'body line 2',0 ; single-byte code as explained above
; db 'body line 3',0 ;
; db ... ;
; db EOF ; end of macro marker
;
; Nested macros are allowed. However, nesting happens at execution time and
; not at definition time (i.e. an inner macro becomes defined only when the
; outer macro is executed). The macro generation code has to track the
; nesting level nevertheless to prevent exiting prematurely the macro
; definition mode when the ENDM of an inner macro is encountered.
;
DEFMAC: ld hl,(IDADR) ; get pointer to identifier (MACRO name)
ld a,h
or l
jp z,OERROR ; ensure is present
ld a,(NEWSYM)
or a
jr nz,NEWMAC ; branch if new symbol
; M80 allows macros to have the same name as existing symbols,
; or even existing macros. Here we do the same.
; Macros have priority over instruction mnemonics.
ld a,(hl) ; get identity char
and 0Fh
jp z,OERROR ; error if name is invalid
ld c,a
ld b,0
inc bc
ld de,(SYMPTR) ; for code @NEWMAC
ld (IDADR),de
ldir ; copy length + name
NEWMAC: ld hl,(IDADR)
ld (SYMPTR),hl ; remove new symbol from symbol table
ld a,(hl) ; get length+flags
and 0Fh ; remove flags, leave length
ld (hl),a
push hl
ld (IDLEN),a
inc hl
ld de,IDBUF
ld c,a
ld b,0
ldir
call FNDMAC ; is this a macro re-definition?
jr c,NM1 ; branch if not
ld a,STDEL
ld (de),a ; else delete old macro
ld a,(MACLVL)
or a
call z,GBCOL ; if at safe level, call garbage collector
NM1: ld a,(IDLEN)
pop hl
ld c,a
ld b,0
inc bc
inc bc
call MCHECK ; make sure we have enough memory
dec bc
jp c,WERROR
add hl,bc
ld (MACPTR),hl ; set macro build pointer
ld (PARAMS),hl ; set pointer to parameters
xor a
ld (LCLFLG),a ; clear LOCAL flag for GETPRM
call GNC ; get next character
or a ; end of line?
call nz,GETPRM ; get parameters if not
xor a
ld (LCOUNT),a ; reset LOCAL variable count
ld hl,(MACPTR)
ld (hl),0FFh ; set end of args marker
inc hl
ld (MACPTR),hl
ld a,.MACRO
ld (MACTYP),a ; remember type is MACRO
INCDFL: ld hl,MDFLVL
inc (hl) ; increase macro def level
ret nz
dec (hl)
VERROR: ld a,'V' ; on overflow, set error flag
ld (ERRFLG),a
ret
;
; Get MACRO dummy parameters or LOCAL variables
;
GETPRM: ld bc,RECMAX ; ensure enough available memory
call MACCHK ; (RECMAX is a bit more than enough here,
jp c,WERROR ; but that will save time and some bytes)
call BACKUP
GP0: call ID ; get identifier
ld a,(IDLEN)
or a ; valid identifier?
jr nz,GP1 ; yes -> branch
call OERROR ; no -> argument error
jr GP2
GP1: ld c,a
ld b,0
ld hl,IDBUF
ld de,(MACPTR)
ld a,(LCLFLG)
or c ; set MSB of length if LOCAL variable name
ld (de),a ; store length
inc de
ldir
ld (MACPTR),de
ld hl,LCOUNT
inc (hl) ; increase LOCAL variable count
GP2: call GNC
cp ',' ; more params?
jr z,GP0 ; loop if yes
or a ; end of line?
ret z ; return Z if yes
jp BACKUP ; else backup to scanned char and return
;
; Store macro, called from the main thread while in macro def mode.
;
MSTORE: ld hl,(PTR1)
push hl ; save current PTR1
call ID ; get first identifier
cp ':'
jr z,MST1 ; skip if label
cp '&' ; test for possible param substitution
jr nz,MST0
ld hl,(PTR1)
MSKIP: ld a,(hl) ; if yes, ignore everything
or a ; up to the next delimiter
jr z,MSKP1
cp ' '
jr z,MSKP1
cp TAB
jr z,MSKP1
inc hl
cp ':'
jr z,MSKP1
jr MSKIP
MSKP1: ld (PTR1),hl
jr MST2
MST0: call FNDOPC ; lookup opcode
jr nc,MST3 ; branch if found
jr MST2
MST1: call GNC ; skip over delimiter
MST2: call ID ; try second identifier
call FNDOPC ; lookup opcode
jp c,MST8 ; branch if not found
MST3: ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld a,(hl)
cp 27 ; class 27? (pseudo-ops)
jp nz,MST8 ; branch if not
ld a,e
cp 24 ; MACRO/REPT/IRP/IRPC?
jp z,MST7 ; branch if yes - nested macro definition
cp 25 ; ENDM?
jp z,MST4 ; branch if yes
cp 27 ; LOCAL?
jp nz,MST8 ; branch if not
; LOCAL
ld a,(MACTYP)
cp .MACRO
jp nz,MST8 ; only MACROs can have LOCALs
ld a,(MDFLVL)
dec a
jp nz,MST8 ; don't process it if inside a nested macro
pop hl ; drop saved PTR1
ld hl,(MACPTR)
dec hl
ld a,(hl)
inc a ; test for 0FFh
ret nz ; too late, LOCAL not allowed here
ld a,80h
ld (LCLFLG),a ; set LOCAL flag for GETPRM
ld (MACPTR),hl
call GNC ; get next character
or a ; end of line?
jp z,OERROR ; error if yes
call GETPRM ; else get parameter list
ld hl,(MACPTR)
ld (hl),0FFh ; set new end of args
inc hl
ld (MACPTR),hl
ret
MST4: ld hl,MDFLVL
dec (hl) ; decrement macro level
jp nz,MST8
; ENDM
ld a,(MACLVL)
or a
call z,GBCOL ; if at safe level, call garbage collector
pop hl ; drop saved PTR1
ld hl,(MACPTR)
ld (hl),EOF ; set end of macro marker
inc hl
ld (MACPTR),hl
ld a,(MACTYP) ; check macro type
cp .MACRO
jr nz,MST6 ; branch if REPT/IRP/IRPC - execute immediately
; end MACRO definition
ld de,(SYMPTR) ; begin of macro build area
MST5: ld a,(de)
inc de
inc a ; 0FFh marks end of param list
jr nz,MST5 ; find begin of macro body (use CPIR?)
ld hl,(MACPTR)
push hl
or a
sbc hl,de ; obtain length
ld c,l
ld b,h
ld de,(DSPTR)
pop hl
push bc
dec hl
dec de
lddr ; copy to high memory
ld a,(LCOUNT)
ld (de),a ; save number of LOCAL variables
dec de
ld hl,(SYMPTR)
ld a,(hl) ; get length
ld c,a
ld b,0
inc bc
push bc
add hl,bc
dec hl
lddr ; copy name + len
pop hl
pop bc
add hl,bc
inc hl ; account for LOCAL variable count field
ex de,hl
ld (hl),d
dec hl
ld (hl),e ; set length
dec hl
ld (hl),STMDEF ; struct type = macro def
ld (DSPTR),hl
ld hl,(SYMPTR)
ld (hl),0 ; restore end of symbol table marker
ret
MST6: ; REPT/IRP/IRPC execution
ld de,(MACBGN)
ld hl,(MACPTR)
push hl
or a
sbc hl,de ; obtain length
ld c,l
ld b,h
ld de,(DSPTR)
pop hl
push bc
dec hl
dec de
lddr ; copy to high memory
pop bc
ex de,hl
xor a
ld (hl),a ; clear text index
dec hl
ld (hl),a
dec hl
ld a,(CLEVEL)
ld (hl),a ; store current conditional level
dec hl
ld a,(MACTYP)
ld (hl),a ; set macro type
dec hl
inc bc
inc bc
inc bc
inc bc
ld (hl),b
dec hl
ld (hl),c ; set length
dec hl
ld (hl),STMEXP ; struct type = macro expansion
ld (DSPTR),hl
ld hl,(SYMPTR)
ld (hl),0 ; restore end of symbol table marker
ld hl,MACLVL
inc (hl) ; start macro execution
ret nz
dec (hl)
jp VERROR
MST7: call INCDFL
MST8: ld a,' '
ld (ERRFLG),a
pop hl
ld (PTR1),hl ; restore old PTR1
ld bc,0
MST9: ld a,(hl)
inc hl
inc bc ; compute length, including trailing null
or a
jr nz,MST9
call MACCHK ; check for enough space
jp c,WERROR ; error if out of memory
ld hl,(PTR1)
ld de,(MACPTR)
push de
ldir ; copy line to macro body
ld (MACPTR),de
dec hl ; point back to trailing null
ex (sp),hl
ld (PTR1),hl
ld a,(ERRFLG) ; save ERRFLG, as MSCAN may generate spurious
push af ; errors when testing for valid identifiers
call MSCAN ; scan line and encode params
pop af
ld (ERRFLG),a ; restore original ERRFLG
xor a
ld (de),a ; end line with a null
inc de
ld (MACPTR),de
pop hl
ld (PTR1),hl ; restore old record pointer
ret
;
; MSCAN - Scan line for dummy parameters and local variables,
; and replace them with one-byte codes.
;
; Code for dummy parameter = 80h + param ordinal
; Code for local variable = C0h + label ordinal
;
MSCAN: ld e,l ; get dest address in DE
ld d,h ; (will overwrite src contents)
S0: ld a,(hl) ; get char from line
or a
ret z ; if end of line, return
cp ';'
jr z,S14 ; branch if start of comment
cp "'"
jr z,S9 ; branch if start of quoted string
cp '"' ; (inside a string any parameters
jr z,S9 ; must be preceded by an ampersand)
cp ' '
jr z,S1 ; store blanks directly
cp TAB
jr z,S1
S1A: call SUBST ; test for parameter and substitute if found
jr S0 ; loop
S1: ld (de),a ; store char
inc hl ; advance pointers
inc de
jr S0 ; loop
S9: ; quoted string
ld (de),a ; store quote char
inc de
ld c,a
S10: inc hl
S11: ld a,(hl) ; get next char
or a
ret z ; if end of line, return
cp c ; quote?
jr z,S1 ; exit this loop if yes
cp '&' ; ampersand?
jr nz,S12 ; branch if not
push bc
call SUBST ; else match parameter
pop bc
jr S11 ; loop
S12: ld (de),a ; store char
inc de ; advance pointer
jr S10 ; and loop
S14: ; comment
inc hl
cp (hl) ; do not store comments that begin with
ret z ; two semicolons
dec hl
S15: ld (de),a ; store comment, but do not perform any
or a ; parameter substitutions
ret z
inc de
inc hl
ld a,(hl)
jr S15
;
; SUBST - Test for dummy parameter or local variable name
; and substitute with the corresponding value if found.
;
SUBST: push af
cp '&' ; ampersand?
jr nz,S5A
inc hl ; skip it if yes (but remember it)
S5A: push hl
ld (PTR1),hl ; remember start of possible identifier
call ID
ld a,(IDLEN)
or a ; valid identifier?
jr z,S2 ; branch if not
push de
ld c,a ; C = identifier length
ld b,80h ; B = parameter code
ld hl,(PARAMS) ; HL = dummy parameter list
S5: ld a,(hl) ; get dummy parameter length
inc a ; test for 0FFh
jr z,S3 ; parameter not found, exit loop
dec a
bit 7,a ; hi-bit set?
jr z,S8 ; branch if not
bit 6,b ; first time?
jr nz,S8 ; branch if not
ld b,0C0h ; else make B = LOCAL label code
S8: and 7Fh ; kill hi-bit
cp c
jr nz,S4 ; not same length
push hl
push bc
inc hl
ld b,a
ld de,IDBUF
S6: ld a,(de)
cp (hl) ; compare strings (both already in uppercase)
jr nz,S7 ; not same
inc hl
inc de
djnz S6
pop bc
pop hl ; matches
pop de
ld a,b
ld (de),a ; store parameter code
inc de
pop hl
pop af
ld hl,(PTR1) ; point past identifier
ret
S7: pop bc
pop hl
ld a,(hl)
and 7Fh ; get length
S4: ld e,a
ld d,0
add hl,de
inc hl ; point to next parameter
inc b
jr S5 ; loop to match next
S3: pop de ; parameter not found, restore registers
pop hl
pop af
cp '&' ; first char was ampersand?
jr nz,S3A ; branch if not
ld (de),a ; else restore it
inc de
S3A: ld b,0
ldir ; copy identifier name
S3B: ld a,(hl)
call VALID ; and any other following VALID chars
ret c
ld (de),a
inc hl
inc de
jr S3B
S2: pop hl ; not an identifier, restore registers
pop af
cp '&' ; first char was ampersand?
jr nz,S2A ; branch if not
ld (de),a ; else restore it
inc de
S2A: ld a,(hl) ; copy char
ld (de),a
inc hl
inc de
ret
SUBTTL 'MACRO processing'
;
; EXPMAC - Expand MACRO.
; Called with HL pointing to macro body in dynamic area
; (e.g. as returned by FNDMAC).
;
; Macro expansion structure as saved in high memory:
;
; db STMEXP ; type = MACRO expansion
; dw len ; length of the following block:
; db .MACRO ; macro type
; db clevel ; saved conditional level
; dw index ; text index
; dw local ; first/base number for LOCAL variables
; dw macptr ; pointer to macro def
; db len,'arg1' ; arg 1
; db len,'arg2' ; arg 2
; db ... ;
; db 0FFh ; end of args
;
EXPMAC: ld bc,6
call MCHECK
jp c,WERROR
ld a,(hl) ; get number of LOCAL variables
inc hl ; point to start of macro text
push hl
ld bc,(LCLNUM) ; get LOCAL base number into BC
ld l,a
ld h,0
add hl,bc
ld (LCLNUM),hl ; save number of next LOCAL variable
pop de
ld hl,(SYMPTR)
ld (MACBGN),hl
ld (hl),c ; store base number of LOCAL labels
inc hl
ld (hl),b
inc hl
ld (hl),e ; store pointer to macro def
inc hl
ld (hl),d
inc hl
ld (MACPTR),hl
call GNC
or a
call nz,GETARG ; get arguments
ld (hl),0FFh ; end of args
inc hl
ld (MACPTR),hl
ld a,.MACRO
ld (MACTYP),a
jp MST6 ; exit via common code
;
; Get comma-separated list of arguments
;
GETARG: ld bc,RECMAX ; ensure enough available memory
call MACCHK ; (RECMAX is a bit more than enough here,
jp c,WERROR ; but will save some bytes and time)
call BACKUP
GT0: ld de,(MACPTR)
push de
inc de ; placeholder for length
call GETSTR ; get next argument
ex de,hl
ld (MACPTR),hl
pop de
push af
or a
sbc hl,de
dec hl
ld a,l
ld (de),a ; store length
pop af
jr c,GT1
call GNC
or a
jr z,GT1
cp ','
jr z,GT0
GT1: ld hl,(MACPTR)
ret
;
; FNDMAC - Find MACRO
;
FNDMAC: ld c,STMDEF ; record type = macro def
call FNDREC
FNDM1: ret c ; not found
push hl
inc hl ; skip type
inc hl ; and record length
inc hl
ld a,(IDLEN)
cp (hl) ; compare name length
jr nz,FNDM3 ; branch if not same
inc hl
ld b,a
ld de,IDBUF
FNDM2: ld a,(de)
cp (hl)
jr nz,FNDM3
inc hl
inc de
djnz FNDM2
pop de ; found, HL now points past macro name
ret ; return with CY clear
FNDM3: pop hl
ld c,STMDEF
call FNDNXT
jr FNDM1
;
; Reading from MACRO/REPT/IRP/IRPC
;
GNRMAC: ld c,STMEXP
call FNDREC ; find top-level executing macro
jr nc,GNR1 ; branch if found
xor a ; if not found (should not happen!)
ld (MACLVL),a ; reset the macro execution level
jp GNR ; and switch back to reading from file
GNR1: inc hl ; skip struct type
inc hl ; and length fields
inc hl
ld a,(hl) ; get macro type
inc hl
inc hl ; skip over saved conditional level
cp .MACRO
jp z,XMACRO ; execute MACRO
cp .REPT
jp z,XREPT ; execute REPT
cp .IRP
jp z,XIRP ; execute IRP
cp .IRPC
jp z,XIRPC ; execute IRPC
jp OERROR ; should never happen
;
; Read from MACRO and expand arguments
;
XMACRO: push hl ; save pointer
ld c,(hl) ; get char index
inc hl
ld b,(hl)
inc hl
ld e,(hl) ; get LOCAL base number
inc hl
ld d,(hl)
ld (LBASE),de
inc hl
ld e,(hl) ; get pointer to macro definition
inc hl
ld d,(hl)
inc hl
ld (ARGS),hl ; save pointer to arguments
ex de,hl
add hl,bc ; index into macro text
ld a,(hl) ; get char
cp EOF
jp z,XEND ; end of text = end of macro
ld de,REC
ld (PTR1),de
ld a,RECMAX-1
ld (RCOUNT),a
XM1: ld a,(hl) ; get char from macro text
inc hl
inc bc ; advance index
or a ; test for coded dummy parameter
jp p,XM3 ; branch if not parameter
push hl ; else save text pointer
push bc
ld hl,(ARGS) ; get start address of argument list
sub 80h-1 ; get param number
cp 40h+1
jr nc,XM8 ; branch if local variable
ld b,a ; B = param number
XM6: ld a,(hl) ; get length
inc a ; 0FFh marks end of arguments
jr z,XM4 ; branch if not found
dec a
inc hl ; point to string
djnz XM5 ; count until found
or a
jr z,XM7 ; if null argument
ld b,a
XM2: ld a,(hl)
call SAVREC ; expand argument
inc hl
djnz XM2
XM7: pop bc
pop hl ; restore text pointers
jr XM1 ; continue with text
XM5: ld e,a ; get length into DE
ld d,0
add hl,de ; point to next argument
jr XM6 ; loop
XM8: sub 40h+1 ; get label ordinal
ld e,a
ld d,0
ld hl,(LBASE)
add hl,de
ex de,hl
push de
ld a,'?'
call SAVREC ; create label of type '??nnnn'
call SAVREC
pop de
ld hl,(PTR1)
ld c,d
call XM9
ld c,e
call XM9
ld (PTR1),hl
pop bc
pop hl
jr XM1
XM9: ld a,(RCOUNT)
sub 2
ret c
ld (RCOUNT),a
ld a,c
jp CNV2HX
XM4: pop bc
pop hl ; not found, restore text pointers
jr XM1 ; and loop (will expand as null string)
XM3: call SAVREC ; save char in REC buffer
or a ; end of line?
jr nz,XM1 ; loop if not
ld hl,(PTR1)
ld (hl),a ; append null in case line was truncated
ld hl,REC
ld (PTR1),hl
pop hl
ld (hl),c ; update text index
inc hl
ld (hl),b
ret
SAVREC: push hl
ld e,a
ld hl,RCOUNT
ld a,(hl)
or a ; limit reached?
jr z,ST1 ; return if yes
dec a
ld (hl),a
ld hl,(PTR1)
ld (hl),e ; store char in buffer
inc hl
ld (PTR1),hl
ST1: ld a,e
pop hl
ret
;
; Read from REPT, no arguments to expand
;
XREPT: push hl
ld c,(hl) ; get char index
inc hl
ld b,(hl)
inc hl
ld e,(hl) ; get count
inc hl
ld d,(hl)
ld a,d
or e ; counter reached zero?
jp z,XEND ; if yes, end execution
inc hl
add hl,bc ; index into macro text
ld a,(hl)
cp EOF ; end of text?
jr z,XR3 ; branch if yes, start next iteration
ld de,REC
XR1: ld a,(hl) ; no arg substitution for REPT
inc hl
inc bc
ld (de),a ; and so no need to check for REC buf overflow
or a
jr z,XR2
inc de
jr XR1
XR2: pop hl
ld (hl),c ; update text index
inc hl
ld (hl),b
ret
XR3: pop hl ; next iteration
push hl
xor a
ld (hl),a ; reset text index
inc hl
ld (hl),a
inc hl
ld c,(hl)
inc hl
ld b,(hl)
dec bc ; decrement iteration counter
ld (hl),b
dec hl
ld (hl),c
pop hl
jr XREPT ; restart execution
;
; Read from IRP, expand single argument
;
XIRP: push hl
ld c,(hl) ; get text index
inc hl
ld b,(hl)
inc hl
ld a,(hl) ; get argument index
inc hl ; point to argument list
inc a
XP1: ld e,(hl) ; get arg length
inc e
jp z,XEND ; arg list exhausted
dec a
jr z,XP2
ld d,0
add hl,de ; point to next argument
jr XP1
XP2: ld (ARGS),hl
XP3: ld a,(hl)
inc hl
inc a
jr nz,XP3 ; find start of body text
add hl,bc ; index into macro text
ld a,(hl) ; get char
cp EOF ; end of text?
jp z,XP7 ; branch if yes, start next iteration
ld de,REC
ld (PTR1),de
ld a,RECMAX-1
ld (RCOUNT),a
XP4: ld a,(hl) ; get char from macro text
inc hl
inc bc ; advance index
or a ; test for coded dummy parameter
jp p,XP5 ; branch if not parameter
push hl
push bc
ld hl,(ARGS) ; else expand parameter
ld a,(hl)
inc a
jr z,XP6 ; if no arguments
dec a
jr z,XP6 ; if null argument
ld b,a
inc hl
XP9: ld a,(hl)
call SAVREC ; expand argument
inc hl
djnz XP9
XP6: pop bc
pop hl
jr XP4
XP5: call SAVREC ; save char in REC buffer
or a ; end of line?
jr nz,XP4 ; loop if not
ld hl,(PTR1)
ld (hl),a ; append null in case line was truncated
ld hl,REC
ld (PTR1),hl
pop hl
ld (hl),c ; update text index
inc hl
ld (hl),b
ret
XP7: pop hl ; next iteration
push hl
xor a
ld (hl),a ; reset text index
inc hl
ld (hl),a
inc hl
inc (hl) ; advance arg index
pop hl
jr XIRP ; restart execution
;
; Read from IRPC, expand single argument
;
XIRPC: push hl ; save pointer
ld c,(hl) ; get text index
inc hl
ld b,(hl)
inc hl
ld a,(hl) ; get string index
inc hl
cp (hl) ; compare with string length
jp nc,XEND ; end if string exhausted
push hl
inc hl
ld e,a
ld d,0
add hl,de ; index into string