forked from hperaza/ZSM4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.mac
332 lines (319 loc) · 7.54 KB
/
symbols.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
;**********************************************************************;
; ;
; 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 Symbol table routines
.Z80
include ZSM.INC
public ID,ADDSYM,SYMLUK,FNDOPC,VALID,VALID1,WERROR
extrn GNC,PTR1,UCASE,IDBUF,IDLEN,MCHECK,ERRFLG
extrn NEWSYM,SYMTBL,SYMADR,IDADR,VAL,CMNPTR
extrn SYMPTR,SYMMOD,OPCODES,OPLEN,EVFLGS
;
; ID - Collect ID and place in IDBUF
; Returns delimiter character in A, symbol length in IDLEN
;
ID: push hl
push de
push bc ; save regs
ld de,IDBUF
ld b,IDMAX
call GNC ; skip blanks, get char, convert to uppercase
call VALID1
jr c,LERROR ; error if not a valid starting char
ld hl,(PTR1)
jr ID3
;
ID2: ld a,(hl)
call UCASE ; convert to uppercase
call VALID
jr c,ID4
inc hl
ID3: ld (de),a
inc de
djnz ID2
ID7: ld a,(hl)
call UCASE
call VALID
jr c,ID4
inc hl
jr ID7
;
ID4: ld (PTR1),hl
ID6: cp ';'
jr nz,ID9
ID8: xor a
ID9: ld c,a
ld a,IDMAX
sub b
ld (IDLEN),a ; store symbol length
ld a,c
pop bc
pop de
pop hl
ret
;
LERROR: ld a,'L'
ld (ERRFLG),a
jr ID8
;
VALID: call ISDIG
ret nc
VALID1: call ISLETR
ret nc
cp '_'
ret z
cp '$'
ret z
cp '?'
ret z
cp '.'
ret z
cp '@'
ret z
scf
ret
;
ISDIG: cp '0'
ret c
cp '9'+1
ccf
ret
;
ISLETR: cp 'A'
ret c
cp 'Z'+1
ccf
ret
;
; ADDSYM - Add symbol to symbol table, if not already there
;
; Upon entry, IDBUF contains the symbol name and IDLEN its length.
;
; On return, HL and SYMADR point to value low byte in new entry for
; possible further update, and IDADR is set to the address of the
; length+flags field.
;
; Z=1 if symbol already there, Z=0 otherwise.
;
; Initial flags field is cleared.
; Initial value is set to (VAL) and mode to (SYMMOD).
;
ADDSYM: xor a
ld (NEWSYM),a
ld de,(SYMTBL) ; no conflict with reg names
ld c,5
call SYMLUK ; lookup symbol
push hl ; save table ptr
ld (SYMADR),hl ; save address for EQU
ld (IDADR),de
pop hl ; restore symbol table ptr
ret nc ; return if already there, else continue below
;
; SYMENT - Enter a symbol into the symbol table
; Upon entry IDBUF is assumed to contain the name of the symbol
; to enter, name length in IDLEN.
;
; Entry format: flags/length (each 4 bits)
; n a m e ... (up to 16 bytes)
; value (2 bytes)
; address mode (1 byte)
; pointer to COMMON def (2 bytes)
;
; On return HL points to value low byte in new entry for possible
; further update (used by EQU pseudo operator)
;
SYMENT: ld bc,IDMAX+6+1 ; max entry length + end marker
call MCHECK ; check for available memory
jr nc,SYMEN1 ; jump if enough
WERROR: ld a,'W'
ld (ERRFLG),a
ret
;
SYMEN1: ld a,(IDLEN)
or a
jr nz,SYMEN2
ld a,'L'
ld (ERRFLG),a
scf
ret ; else label error
;
SYMEN2: ld hl,(SYMPTR)
ld (IDADR),hl ; HL = dest ptr
ld c,a
ld b,0
ld (hl),a ; set up length, clear flags
inc hl
ex de,hl ; DE = dst
ld hl,IDBUF ; HL = src
ldir ; copy name
ld hl,(VAL)
ex de,hl ; switch registers
push hl
ld (hl),e ; set value
inc hl
ld (hl),d
inc hl
ld a,(SYMMOD)
ld (hl),a ; set mode
inc hl
ld de,(CMNPTR)
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (SYMPTR),hl
ld (hl),0 ; set up new pointer and new end marker
pop hl
ld (SYMADR),hl ; save address for EQU
or 0FFh ; set Z=0
ld (NEWSYM),a
ret
;
; SYMLUK - Look up symbols in table
;
; On entry, DE points to table to search and C contains data field
; length. Symbol name in IDBUF and length in IDLEN.
;
; On return, CY set means symbol not found.
; Else DE points to start of table entry and HL to value low byte
; in table entry.
;
SYMLUK: ld a,(IDLEN)
ld b,a
inc c ; include flags+length field
SYML1: ld a,(de)
or a
scf
ret z ; return if end of table
and 0Fh ; get length of symbol
cp b
jr nz,SYML4
push de
push bc
inc de ; skip length
ld hl,IDBUF
SYML2: ld a,(de)
cp (hl)
jr nz,SYML3
inc hl
inc de
djnz SYML2
SYML3: pop bc
pop de
jr z,SYML5 ; branch if match
ld a,b
SYML4: add a,c ; get full entry length
ld l,a
ld h,0
add hl,de
ex de,hl
jr SYML1 ; advance pointer and continue to loop
;
SYML5: ld a,(de)
ld b,a
and 0F0h
ld (EVFLGS),a ; save for main processor
ld a,b
and 0Fh
ld l,a
ld h,0
add hl,de
inc hl
xor a
ret ; point to value low and exit
;
; Search opcode table using a binary search
;
FNDOPC: ld a,(IDLEN)
sub 2
ret c
cp 8+1-2
ccf
ret c
ld c,a
ld b,0
ld hl,OPLEN
add hl,bc
ex de,hl
ld hl,OPCODES
add hl,bc
add hl,bc
add a,4+2
ld c,a ; save item size
ld a,(hl)
inc hl
ld h,(hl)
ld l,a ; get table base address into HL
ld a,(de)
ld b,a ; get number of table items into B
ld d,c ; get item size into D
; binary search
ld e,0FFh ; mark M <> old M
ld c,0 ; lower bound = 0
BNEXT: ld a,b
add a,c
rra ; middle point = (upper+lower)/2
cp e ; same as last time?
scf
ret z ; return error - not found
ld e,a ; new middle value
push hl ; table base addr
push de
push bc
ld b,d ; get item size into B and C
ld c,b ; for loop counting below
ld d,0 ; DE = middle point
BADD: add hl,de ; index into table (HL += DE times B)
djnz BADD ; count and loop
push hl
inc hl ; address of byte to compare
ld de,IDBUF
ld a,c
sub 4
ld b,a ; B = string length
BCMP: ld a,(de)
cp (hl)
inc de
inc hl
jr nz,DIFF ; branch if no match
djnz BCMP
pop hl ; match found
pop bc
pop de
pop bc
ld a,e ; return index in A
ex de,hl
jp SYML5
DIFF: pop hl
pop bc ; restore bounds
pop de ; restore item size and middle point
pop hl ; restore table base address
jr c,LOWER ; IDBUF is lower
ld c,e ; lower = middle
jr BNEXT ; IDBUF is higher
LOWER: ld b,e ; upper = middle
jr BNEXT ; IDBUF is lower
end