forked from hperaza/ZSM4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing.mac
956 lines (897 loc) · 21 KB
/
listing.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
;**********************************************************************;
; ;
; This file is part of ZSM4, a Z80/Z180/Z280 relocatable macro- ;
; assembler written in Z80 assembly. ;
; Copyright (C) 2017-2020, Hector Peraza. ;
; ;
; This work is derived from Z80ASM, originally written by Michael ;
; G. Lehman (1977) and with modifications by Ray Halls (1992) and ;
; Neil Harrison (1983). ;
; ;
; 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 Listing generation routines
.Z80
include ZSM.INC
public LSTINI,LSTOUT,LSTB,LSTW,CNV2HX,PLINE,PMSG
public SYMBOL,MACROS,MAXLNE
extrn GETPPC,MACCHK,FNDREC,FNDNXT,WERROR,WLINE,WNB
extrn HDRBUF,HOFEND,HOFPG,HOFMSG,ERRFLG,ERRCNT,MACFLG
extrn INCLVL,LEN,LENDS,LSTOPT,MACPTR,CONDSP,VAL,EQUFLG
extrn LBLFLG,DSFLAG,DBWFLG,CLINE,PC,EVMODE,CURSEG,CURLNE
extrn LFLAG,UFLAG,SYMPTR,SYMTBL,TITLEB,SBTTLB,IFLIST
extrn NOLIST
CSEG
;
; Clear line header (error, address and object code fields)
;
LSTINI: ld hl,HDRBUF
ld e,l
ld d,h
inc de
ld bc,HDRSZ-1
ld (hl),' '
ldir
xor a
ld (de),a
ld (LSTCNT),a ; clear character count
call GETPPC ; get effective PC
ld a,(LEN)
ld e,a
ld d,0
add hl,de
ld (PCLINE),hl
ret
;
; Prepare line for listing
;
LSTPRE: ld a,(ERRFLG)
cp ' ' ; test for error
jr z,S403
ld hl,(ERRCNT)
inc hl ; incr count
ld (ERRCNT),hl
S403: ld hl,HDRBUF+HDRSZ-3
ld a,(INCLVL) ; test for INCLUDE file
or a
jr z,S406
ld (hl),'C' ; 'C' indicates this line is from an INCLUDE file
S406: ld a,(MACFLG)
inc hl
ld (hl),a ; '+' indicates this line is part of a MACRO expansion
ld de,HDRBUF
ld a,(ERRFLG)
or a
res 7,a
ld (de),a ; store error code
jp m,S407 ; skip conditional test if forced error output
ld hl,(CONDSP)
ld a,(hl) ; else get conditional state
rra ; into carry, set = true
ret nc ; return if not true, no code generated
S407: ex de,hl ; pointer to HDRBUF now in HL
inc hl
inc hl
ld a,(EQUFLG)
or a
ld de,(VAL)
jr nz,S405 ; branch if to use VAL (EQU, ORG, END)
ld a,(LEN)
ld c,a
ld a,(DSFLAG)
or c
ld c,a
ld a,(LBLFLG)
or c
ret z ; do not output address if no code generated
xor a
ld de,(PCLINE) ; get effective PC value for this line
S405: push af ; remember EQUFLG value
ld a,d
call CNV2HX ; store address
ld a,e
call CNV2HX
call SEGCHR
ld (hl),c ; add segment type char
inc hl
inc hl
pop af ; pop EQUFLG value
jr nz,S410 ; jump if EQU
ld a,(DSFLAG)
or a
jr nz,S411 ; branch if to use LENDS
ret
S410: ld (hl),'='
ret
S411: ld (hl),'('
inc hl
inc hl
ld de,(LENDS)
ld a,d
call CNV2HX ; output DEFS/DS size
ld a,e
call CNV2HX
inc hl
ld (hl),')'
ret
;
; Process the listing output if required
;
LSTOUT: ld a,(UFLAG) ; undefined symbol
or a
jr z,S401B ; skip if not
ld a,'U' ; set up undefined error
ld (ERRFLG),a
xor a
ld (UFLAG),a
S401B: ld a,(ERRFLG) ; have we an error on this line?
cp ' '
jp nz,S402 ; always print errors
;
; LFLAG: Copy of list option from command line
;
ld a,(LFLAG) ; do we need to print this line?
cp 'Z'
ret z ; don't print if user asked us not to
;
; LISTFL: True if we want a listing
;
ld a,(LSTOPT) ; has the listing been turned off?
bit LISTFL,a ; Z = yes
ret z ; skip if listing turned off
;
; NOLIST: True to avoid listing TITLE, FORM, PAGE, EJECT
;
ld a,(NOLIST) ; is this a TITLE, FORM,
or a ; PAGE or EJECT pseudo-op?
ret nz ; we don't print those
;
ld a,(MACFLG) ; get MACRO state
cp ' ' ; ' ' = false
jr z,S401D ; skip if false
;
; MACRFL: False to suppress listing of MACRO expansions
;
ld a,(LSTOPT)
bit MACRFL,a ; bit set = print MACRO expansions
ret z ; bit clear = don't
;
; XMACFL: True to list only MACRO lines that generate object code
;
bit XMACFL,a ; bit set = print only lines that generate code
jr z,S401D ; bit clear = print all lines
;
ld a,(LEN) ; list line if object code is generated
ld c,a
ld a,(DSFLAG)
or c ; also if it contains DEFS statements
ld c,a
ld a,(LBLFLG) ; or if a label is defined on that line
or c
ret z
;
S401D: ld hl,(CONDSP) ; get conditional state
ld a,(hl) ; get state
rra ; into carry, set = true
jr nc,S401C ; skip if not true
;
; IFLIST: True to suppress listing on IF, ELSE, ENDIF
; when "LIST NOCOND" is current.
;
ld a,(IFLIST) ; IF, ELSE, ENDIF pseudo-op?
or a ; NZ => IF, ELSE or ENDIF
jr z,S402 ; don't print these if "LIST NOCOND"
;
; OK, the conditional state is false, do we want to list
; the false instructions?
; CONDFL: True if we want to list false conditionals
;
S401C: ld a,(LSTOPT) ; bit set = print conds
bit CONDFL,a ; bit clear = don't print conds
ret z ; don't print if 'LIST NOCOND'
; also means IF, ELSE, ENDIF too
ld c,a
ld a,(MACFLG)
cp ' '
jr z,S402
bit XMACFL,c ; don't print if 'LIST XMACROS' either,
ret nz ; as no code was generated (this cancels the
; effect of the LBLFLG test above)
;
; Print the line
;
S402: call LSTPRE ; prepare the line
call PLINE ; write and page
IF LNGERR
ld hl,ERRFLG ; any errors?
ld a,' '
cp (hl) ; space means no
ret z ; so nothing else to print
ld (hl),a ; don't report the same error twice
;
; Print extended error message
;
ld hl,ERRH1 ; move some stars to
ld de,HDRBUF ; the print buffer
ld bc,ERRH1L ; to make errors
ldir ; conspicuous
push de ; save buffer pointer
ld a,(ERRFLG) ; get error character
sub 'A' ; remove ASCII offset
cp 'Z'-'A'+1 ; in range?
jr c,S410B ; skip if so
ld a,'X'-'A' ; else generate an error!
S410B: add a,a ; each entry is 2 bytes
ld e,a ; put index into DE
ld d,0
ld hl,ERRTAB ; start of error table
add hl,de ; point to msg address
ld e,(hl) ; get address of error msg
inc hl
ld d,(hl)
pop hl ; get REC ptr back
ERRMLP: ld a,(de) ; get a char from the msg
or a
jr z,ERRMDN
ld (hl),a ; store in print buffer
inc hl
inc de
jp ERRMLP
;
ERRMDN: ex de,hl ; DE points to output buffer
ld hl,ERRH2 ; some more stars
ld bc,ERRH2L
ldir
call PLINE ; output error message
ELSE
ld a,' '
ld (ERRFLG),a ; don't report the same error twice
ENDIF
ret
;
; Get ASCII character representing mode or segment type
;
SEGCHR: ld a,(EQUFLG)
or a
ld a,(EVMODE)
jr nz,SEGCH
ld a,(CURSEG)
SEGCH: and NOT GBLSYM ; strip PUBLIC bit
cp 40h
ld c,"'"
ret z
cp 80h
ld c,'"'
ret z
cp 0C0h
ld c,'!'
ret z
and EXTSYM
ld c,'#'
ret nz
ld c,' '
ret
;
; Output byte value to listing, A = value
;
LSTB: ld e,a
ld a,(LSTCNT)
ld c,a
add a,3
cp HDRSZ-12+1 ; value fits in field?
jr c,LB1 ; branch if yes
push de ; else overflow to next line
call LSTOUT ; print the line
call LSTINI ; prepare for next
pop de
ld a,3
ld c,0
LB1: ld (LSTCNT),a
ld b,0
ld hl,HDRBUF+8
add hl,bc
ld a,e
jp CNV2HX ; convert and store byte value
;
; Output word value to listing, HL = value, C = mode
;
LSTW: ld a,(LSTCNT)
ld b,a
add a,6
cp HDRSZ-12+1 ; word fits in field?
jr c,LW1 ; branch if yes
push hl ; else overflow to next line
push bc
call LSTOUT ; print the line
call LSTINI ; prepare for next
pop bc
pop hl
ld a,6
ld b,0
LW1: ld (LSTCNT),a
ld a,c
ld c,b
ld b,0
ex de,hl
ld hl,HDRBUF+8
add hl,bc
ld c,a
ld a,d
call CNV2HX ; convert and store word value
ld a,e
call CNV2HX
ld a,c
call SEGCH
ld (hl),c ; store mode indicator
ret
;
; PLINE routine - Write and Page
;
PLINE: ld hl,MAXLNE
ld a,(CURLNE)
cp (hl) ; page full?
jr c,PLINE1 ; branch if not
;
; Headers are required if we generate a listing
; to any destination. Suppressed if 'Z' option.
;
xor a
ld (CURLNE),a
ld a,(LFLAG) ; get list flag
cp 'Z' ; Z = no print
jr z,PLINE1 ; skip heading
ld a,(ERRFLG)
push af
ld a,' '
ld (ERRFLG),a
ld hl,HOFPG+3 ; increment page number
call INCNUM
ld hl,HOFMSG ; print it
call PLINE2 ; (don't inc line no)
ld hl,TITLEB ; print title
ld a,(hl)
or a
call nz,PLINE2
ld hl,SBTTLB ; print subtitle
ld a,(hl)
or a
call nz,PLINE2
ld hl,HOFEND
call PLINE2 ; newline
pop af
ld (ERRFLG),a
PLINE1: ld hl,HDRBUF ; point to buffer
PLINE2: ld a,(CURLNE)
inc a ; increase page line counter
ld (CURLNE),a
ld a,(LFLAG) ; get list flag
cp 'Z' ; Z = no print, errors to console
jp z,PLINE3 ; skip print, but send errors
cp 'X' ; list to console only
jp z,CLINE
cp 'Y' ; errors to printer?
jr nz,PLINE3
push af ; save list flag
push hl ; save ptr line buffer
call CLINE ; listing to console
pop hl ; recover line buff ptr
pop af ; get list flag
ld a,(ERRFLG) ; fetch error flag
cp ' ' ; blank?
jp nz,WLINE ; errors to printer
ret ; else done
;
PLINE3: ld a,(ERRFLG) ; fetch error flag
cp ' ' ; blank?
push hl ; save ptr line buffer
call nz,CLINE ; listing to console
pop hl ; recover line buff ptr
ld a,(LFLAG) ; get list flag
cp 'P' ; listing to printer?
jp z,WLINE ; listing to printer
cp 'Z' ; listing supressed?
ret z ; done if yes
; else listing to disk .PRN file
push hl ; save line buffer ptr
PRNOPT: ld a,(hl) ; fetch char from line buffer
or a ; is char terminator?
jr z,PRCRLF ; exit loop if yes
call WNB ; write char to print buffer
inc hl ; incr line ptr
jp PRNOPT ; loop, more on line
PRCRLF: ld a,CR ; end with a newline
call WNB
ld a,LF
call WNB
pop hl ; recover buffer ptr
ret ; done
;
; CNV2HX - Convert contents of A reg to hex characters
; and place in buffer pointed to by HL
;
CNV2HX: push af
rrca
rrca
rrca
rrca
call CNV2H
pop af
CNV2H: and 0Fh
add a,90h
daa
adc a,40h
daa
ld (hl),a
inc hl
ret
;
; INCNUM - Increment ASCII number
;
INCNUM: ld a,(hl)
cp ' '
jr nz,INCNU1
ld a,'0'
INCNU1: inc a
ld (hl),a
cp '9'+1
ret nz
ld (hl),'0'
dec hl
jr INCNUM
SUBTTL 'Macro Table sort and list'
;
; Sort macro table and list it. The routine is called only
; if at least one macro has been defined.
;
; Build macro pointer table, a list of addresses
; pointing to the start of each macro definition entry
;
MACROS: ld hl,HDRMAC
ld bc,HMSIZE
call PMSG ; output header
ld hl,(SYMPTR) ; get address of next free memory space
inc hl ; point to byte after
ld (SYMREF),hl ; start of macro ref table
ld (MACPTR),hl ; reuse MACPTR as ref table pointer
ld c,STMDEF
call FNDREC
ld bc,0 ; BC = symbol counter
LMAC1: inc bc
push bc
push hl
inc hl
inc hl
inc hl
ex de,hl
ld bc,2
call MACCHK ; check memory
jp c,LMAC5 ; jp if overflow
ld hl,(MACPTR) ; pointer into reference table
ld (hl),e ; store macro start address
inc hl ; bump pointer
ld (hl),d ; high byte of address
inc hl ; bump pointer
ld (MACPTR),hl ; and save for later
pop hl
ld c,STMDEF
call FNDNXT
pop bc
jr nc,LMAC1
LMAC2: push bc
ld a,(LSTOPT)
bit SORTFL,a ; do we want sorted symbol table?
call nz,SORT ; sort pointers if yes
pop bc
ld hl,(SYMREF) ; start of macro table pointers
ld (MACPTR),hl ; save pointer
LMAC3: ld a,' '
ld hl,HDRBUF
ld de,HDRBUF+1
ld (hl),a
push bc
ld bc,HDRSZ+RECMAX-1
ldir
pop bc
xor a ; set up symbols-per-line counter
ld (SYMCNT),a
LMAC4: ld hl,(MACPTR) ; get macro pointer back
ld e,(hl) ; get pointer
inc hl
ld d,(hl) ; hi byte
inc hl
ld (MACPTR),hl ; save reference pointer
push de ; save macro pointer
ld hl,HDRBUF ; point to output buffer
ld a,(SYMCNT) ; which symbol?
ld e,a ; multiply by 26 (IDMAX+11)
add a,a ; *2
ld d,a
add a,e ; *3
add a,a ; *6
add a,a ; *12
add a,a ; *24
add a,d ; *26
ld e,a
ld d,0
add hl,de ; index into line
ex de,hl ; put output record pointer in DE
pop hl ; get macro pointer back
ld a,b
or c ; test for end of table
jr z,MACXIT
dec bc
push bc
ld c,(hl) ; length in BC
ld b,0
inc hl ; point to start of macro name
ldir ; copy name
ld a,(SYMCNT)
inc a
ld (SYMCNT),a
cp 3 ; 4 for wide listing
pop bc
jp c,LMAC4
push bc
ex de,hl
ld (hl),0 ; terminate the line
call PLINE
pop bc
jp LMAC3
LMAC5: pop hl
pop bc
jp WERROR
MACXIT: ex de,hl ; HL = buffer pointer
ld (hl),0 ; terminate the line
call PLINE ; print it
ld hl,HDRBUF
ld (hl),0
jp PLINE ; print an extra blank line and return
HDRMAC: db 'Macros:',0
HMSIZE equ $-HDRMAC
PMSG: ld de,HDRBUF
ldir
jp PLINE
SUBTTL 'Symbol Table sort and list'
;
; Sort symbol table and list it
;
; Build symbol pointer table, a list of addresses
; pointing to the start of each symbol table entry
;
SYMBOL: ld hl,HDRSYM
ld bc,HSSIZE
call PMSG ; output header
ld hl,(SYMPTR) ; get address of next free memory space
inc hl ; point to byte after
ld (SYMREF),hl ; start of symbol ref table
ld (MACPTR),hl ; ref table pointer
ld de,(SYMTBL) ; start of symbols
ld bc,0 ; BC = symbol counter
SYMB5: ld a,(de) ; get length of this symbol
or a
jr z,SYMB6 ; finish if done
inc bc
push bc
ld bc,2
call MACCHK ; check memory
pop bc
jp c,WERROR ; jp if overflow
ld hl,(MACPTR) ; pointer into reference table
ld (hl),e ; store symbol start address
inc hl ; bump pointer
ld (hl),d ; high byte of address
inc hl ; bump pointer
ld (MACPTR),hl ; and save for later
ld a,(de) ; get length of this symbol
and 0Fh ; in bottom 4 bits
add a,6 ; 6 for overhead
ld l,a ; add to start address
ld h,0 ; to find start of next symbol
add hl,de ; HL = start of next symbol
ex de,hl ; DE = start of next symbol
jr SYMB5 ; round again
SYMB6: push bc
ld a,(LSTOPT)
bit SORTFL,a ; do we want sorted symbol table?
call nz,SORT ; sort pointers if yes
pop bc
ld hl,(SYMREF) ; start of symbol table pointers
ld (MACPTR),hl ; save pointer
SYMB65: ld a,' '
ld hl,HDRBUF
ld de,HDRBUF+1
ld (hl),a
push bc
ld bc,HDRSZ+RECMAX-1
ldir
pop bc
xor a ; set up symbols-per-line counter
ld (SYMCNT),a
SYMB70: ld hl,(MACPTR) ; get symbol pointer back
ld e,(hl) ; get pointer
inc hl
ld d,(hl) ; hi byte
inc hl
ld (MACPTR),hl ; save reference pointer
push de ; save symbol pointer
ld hl,HDRBUF ; point to output buffer
ld a,(SYMCNT) ; which symbol?
ld e,a ; multiply by 26 (IDMAX+11)
add a,a ; *2
ld d,a
add a,e ; *3
add a,a ; *6
add a,a ; *12
add a,a ; *24
add a,d ; *26
ld e,a
ld d,0
add hl,de ; index into line
ex de,hl ; put output record pointer in DE
pop hl ; get symbol pointer back
ld a,b
or c ; test for end of table
jr z,SYMXIT
dec bc
push bc
IF 0
push de ; save record pointer
ld a,(hl) ; get length
and 0Fh
ld c,a ; length in BC
ld b,0
inc hl ; point to start of symbol
ldir ; copy name
ld c,(hl) ; BC = value
inc hl
ld b,(hl)
inc hl
ld a,(hl) ; A = type
pop hl ; get record pointer
ld de,17 ; move over to value (IDMAX+2)
add hl,de
ld (hl),'='
inc hl
ELSE
ld a,(hl) ; get length
and 0Fh
ld c,a ; length in BC
ld b,0
inc hl ; point to start of symbol
ldir ; copy name
ld c,(hl) ; BC = value
inc hl
ld b,(hl)
inc hl
ld l,(hl) ; A = type
ex de,hl
inc hl
sub 17
SYMB73: ld (hl),'.'
inc hl
inc a
jr nz,SYMB73
ld a,e
ENDIF
inc hl
ld e,a ; save type
and EXTSYM ; External?
jr z,SYMB71 ; branch if not
ld bc,0 ; else display value as 0000
SYMB71: ld a,b
call CNV2HX
ld a,c
call CNV2HX
ld a,e ; get type
and 0C0h+EXTSYM ; mask segment type and External bits
call SEGCH
ld (hl),c
inc hl
ld a,e ; get type
and GBLSYM ; Public?
jr z,SYMB72
ld (hl),'P'
inc hl
SYMB72: ld a,(SYMCNT)
inc a
ld (SYMCNT),a
cp 3 ; 4 for wide listing
pop bc
jp c,SYMB70
push bc
ld (hl),0 ; terminate the line
call PLINE
pop bc
jp SYMB65
SYMXIT: ex de,hl ; HL = buffer pointer
ld (hl),0 ; terminate the line
jp PLINE ; print it and return
HDRSYM: db 'Symbols:',0
HSSIZE equ $-HDRSYM
;
; Sort the pointers using a simple bubble sort comparing the
; symbols.
;
SORT: ld a,b
or c
ret z ; no symbols to sort, return
SORT1: dec bc
ld a,b
or c
ret z ; only one symbol, return
push bc
inc bc
xor a ; cheap zero
ld (SWPFLG),a ; initialise the swap flag
ld hl,(SYMREF) ; start of symbol table pointers
SORT2: ld e,(hl) ; get first pointer
inc hl
ld d,(hl) ; hi byte
inc hl
push hl ; save pointer
ld a,(hl) ; get 2nd pointer
inc hl
ld h,(hl) ; hi byte
ld l,a ; HL = 2nd pointer
dec bc
ld a,b
or c
jp z,SORT7 ; end of this pass
push bc
ld a,(de) ; get length of first symbol
and 0Fh ; 4 bits only
ld b,a ; save in B
ld a,(hl) ; get length of 2nd symbol
and 0Fh ; 4 bits only
ld c,a ; save in C for later
push hl ; save all pointers
push de ; and lengths
push bc
cp b ; compare to length of first
jr nc,SORT3 ; skip if 1st shorter
ld b,a ; 2nd is shorter
SORT3: inc de ; point into
inc hl ; actual label
ld a,(de) ; get char from 1st
cp (hl) ; compare to 2nd
jr c,SORT5 ; 2nd > 1st so no swap
jr nz,SORT4 ; 1st > 2nd so swap immediate
djnz SORT3 ; dec character count and loop for next char
;
; By now the symbols are the same (as far as we have checked.)
; Now swap pointers only if 2nd is longer than 1st.
;
pop bc ; get lengths back
ld a,c ; get 2nd length
cp b ; is 1st longer?
jr nc,SORT6 ; don't swap if not
dec sp ; clean up stack
dec sp ; to just drop through
;
; 1st > 2nd, swap pointers
;
SORT4: pop bc ; get counts back & ignore
pop de ; pop first pointer into BE
pop bc ; pop second pointer into BC
pop hl
ex (sp),hl ; get reference pointer
dec hl ; drop back to first pointer
dec hl
ld (hl),c ; and store 2nd pointer there
inc hl
ld (hl),b
inc hl
ld (hl),e ; store first pointer
inc hl
ld (hl),d
dec hl ; restore ref pointer
ld a,0FFh ; set the swap flag
ld (SWPFLG),a ; to say we have swapped
pop bc
jp SORT2 ; and round again
;
; 2nd > 1st, no swap required
;
SORT5: pop bc ; get counts back
SORT6: pop de ; pop first pointer
pop hl ; pop second pointer
pop bc ; get symbol counter
pop hl ; get ref pointer
jp SORT2 ; and round again
;
SORT7: pop hl
pop bc
ld a,(SWPFLG)
or a
jp nz,SORT1 ; round again
ret ; sort complete, return
IF LNGERR
SUBTTL 'Error Messages'
;
ERRH1: db '***** '
ERRH1L equ $-ERRH1
ERRH2: db ' *****',0
ERRH2L equ $-ERRH2
;
; Error message storage
;
; Index table
;
ERRTAB: dw ERRMA
dw ERRMB
dw ERRMC
dw ERRMD
dw ERRME
dw ERRMF
dw ERRMG
dw ERRMH
dw ERRMI
dw ERRMJ
dw ERRMK
dw ERRML
dw ERRMM
dw ERRMN
dw ERRMO
dw ERRMP
dw ERRMQ
dw ERRMR
dw ERRMS
dw ERRMT
dw ERRMU
dw ERRMV
dw ERRMW
dw ERRMX
dw ERRMY
dw ERRMZ
;
; Actual error messages
;
ERRMA: db 'Too many IF statements',0
ERRMB: db 'ENDIF without matching IF statement',0
ERRMC: db 'ELSE without matching IF statement',0
ERRMD: db 'Relative jump range error',0
ERRME: db 'Expression error',0
ERRMF: db 0
ERRMG: db 0
ERRMH: db 0
ERRMI: db 0
ERRMJ: db 0
ERRMK: db 0
ERRML: db 'Invalid identifier',0
ERRMM: db 'Multiple definition',0
ERRMN: db 'Illegal opcode',0
ERRMO: db 'Syntax error',0
ERRMP: db 'Phase error',0
ERRMQ: db 'Missing closing quote',0
ERRMR: db 'Relocation error',0
ERRMS: db 0
ERRMT: db 'Missing ENDM or ENDIF',0
ERRMU: db 'Undefined symbol',0
ERRMV: db 'Value error',0
ERRMW: db 'Symbol table overflow',0
ERRMX: db 'Unknown error',0
ERRMY: db 0
ERRMZ: db 'Divide by Zero',0
ENDIF
DSEG
MAXLNE: ds 1 ; lines per page
LSTCNT: ds 1 ; object code field character count
PCLINE: ds 2 ; PC for current listing line
SYMREF: ds 2 ; symbol ref address
SWPFLG: ds 1 ; flag to indicate a swap in the sort
SYMCNT: ds 1 ; symbols-per-line counter
END