-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymtab.s
executable file
·380 lines (339 loc) · 7.05 KB
/
symtab.s
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
|Fields of the entry in the offent
| are offset and changeval
size_offent = 4
MaxNumberOfSymbols = 750
MaxOffsetEntries = 1000
MaxStringTableSize = 10000
PostFixBufferSize = 300
SymTabStart = EndOfCode
SymTabEnd = SymTabStart + (MaxNumberOfSymbols * size_syment)
OffsetTableStart = SymTabEnd
OffsetTableEnd = OffsetTableStart + (MaxOffsetEntries * size_offent)
StringTableStart = OffsetTableEnd
StringTableEnd = StringTableStart + MaxStringTableSize
PostFixBufferStart = StringTableEnd
PostFixBufferEnd = PostFixBufferStart + PostFixBufferSize
OutputStart = PostFixBufferEnd
OutputEnd = 62535
PresentFileNameOffset:
.word 0
OutputFileNameOffset:
.word 0
ListFileNameOffset:
.word 0
EmptyOffent:
.word OffsetTableStart
LastFilledSymbol:
.word SymTabStart
StringSpace:
.word StringTableStart
|This procedure finds the symbol in the symbol table. The symbol is
|assumed to be in the present input word
|The offset of the start of the entry found is in di and si
FindSymbol:
mov si,#SymTabStart
FindSym1:
mov bp, si
mov di,#InputWord
lodsw
or ax,ax
jz DSymbolNotFound
mov si,ax
lodsb
movb cl,al
xorb ch,ch
rep
cmpsb
jnz NotThisDSymbol
cmpb [di],#0
jnz NotThisDSymbol
mov si,bp
mov di,si
clc
ret
RecordXref:
push ax
mov ax,si
call WriteListWord
mov ax,PresentFileNameOffset
call WriteListWord
mov ax,InputLineNumber
call WriteListWord
pop ax
ret
NotThisDSymbol:
mov si,bp
add si,#size_syment
jmps FindSym1
DSymbolNotFound:
stc
ret
|Add a symbol into the symbol table. The identifier to be added will
|be in the InputWord. When it returns, di and si (like in the previous
|function) points to the attributes.
AddSymbol:
mov di,LastFilledSymbol
cmp di,#SymTabEnd
jnz NoSymTabOverflow
mov bx,#SymTabOverflowMessage
call Panic
NoSymTabOverflow:
mov bp,di
mov ax,StringSpace
mov idname[di], ax
mov di,StringSpace
mov dx,di
inc di
mov si,#InputWord
mov cx,#MaxIdentifierSize - 2
MoreCharsInIdentifier:
lodsb
stosb
orb al,al
jz EndOfInputWord
jcxz ReportLargeIdentifier
loop MoreCharsInIdentifier
ReportLargeIdentifier:
mov bx,#LargeIdentMessage
call PanicRecover
EndOfInputWord:
call CheckStringTableOverflow
mov StringSpace,di
neg cx
add cx,#MaxIdentifierSize - 2
mov di,dx
movb [di],cl
mov di,bp
mov ax,PresentFileNameOffset
mov DefFileNameOffset[di],ax
mov ax,InputLineNumber
mov DefLineNumber[di],ax
mov size_syment[di],#0
mov LastFilledSymbol,di
add LastFilledSymbol,#size_syment
mov si,di
ret
|Find a Fake Symbol, if not present,add it
|
|ax has the value of the fake symbol
|
FindFakeSymbol:
mov di,#InputWord
push ax
movb al,#'_'
stosb
pop ax
push ax
call SprintRegister
xorb al,al
stosb
call FindSymbol
pop ax
jnc FindFakeEnd
push ax
call AddSymbol
mov Attributes[di],#IsFake + Calculated + IsDefined + IsEquate
pop ax
mov Value[di],ax
FindFakeEnd:
ret
|WriteListFile, writes the list file out the format of the list file is
|
|1.Symbol table start :word as in the assembler
|2.Symbol table end :word points to one byte past the true end.
|3.Symbol table :2 - 1 bytes of data, raw symbol table
|4.String table start :word as in the assembler
|5.String table end :word points to one past the end.
|6.String table :5 - 4 bytes of data, raw string table
WriteListFile:
mov ax,#0 |Terminate the xref list in the list file
call WriteListWord
mov ax,#SymTabStart
call WriteListWord
mov ax,LastFilledSymbol
call WriteListWord
mov cx,LastFilledSymbol
mov dx,#SymTabStart
sub cx,dx
call WriteList
mov ax,#StringTableStart
call WriteListWord
mov ax,StringSpace
call WriteListWord
mov cx,StringSpace
mov dx,#StringTableStart
sub cx,dx
call WriteList
ret
ListError:
mov bx,#OutputFileMessage
call Panic
DummyListWord:
.word 0
WriteListWord:
mov DummyListWord,ax
mov dx,#DummyListWord
mov cx,#2
WriteList:
movb ah,#64
mov bx,ListFileHandle
int #DosInterrupt
jc ListError
ret
|Initialise the symbol table
InitSymbolTable:
mov SymTabStart,#0
ret
SprintRegister:
push cx
movb ch,#4
SprintRegisterMore:
rol ax
rol ax
rol ax
rol ax
call SprintHexDigit
decb ch
jnz SprintRegisterMore
pop cx
ret
SprintHexDigit:
push ax
push dx
push bx
andb al,#15
mov bx,#HexDigitTable
xlat
stosb
pop bx
pop dx
pop ax
ret
FindValue:
mov bx,ax
test Attributes[bx],#Calculated
jz FoundNoValue
mov ax,Value[bx]
clc
ret
FoundNoValue:
stc
ret
NoteErrorOnDef:
mov ax,DefLineNumber[si]
mov InputLineNumber,ax
mov ax,DefFileNameOffset[si]
mov PresentFileNameOffset,ax
ret
UndefinedError:
mov bx,[si]
inc bx
push bx
mov ax,Value[si]
call NoteErrorOnDef
call PutErrorAndPosition
pop bx
call DisplayOtherMessage
call DisplayMessage
.asciz ":"
mov bx,#SymbolNotDefinedMessage
call DisplayOtherMessage
call PutCarriageReturn
or Attributes[si],#IsDefined + Calculated + NeverDefined
jmps FixNextSymbol
fixation:
.byte 0
FixUnknowns:
mov si,#SymTabStart
sub si,#size_syment
movb fixation,#0
FixNextSymbol:
add si,#size_syment
cmp [si],#0
jz EndedFix
test Attributes[si],#IsDefined
jz UndefinedError
test Attributes[si],#Calculated
jnz FixNextSymbol
test Attributes[si],#IsFake
jnz JustAnotherSymbol
push si
mov si,Value[si]
call EvaluateExpression
pop si
jnc Evaluated
jmps FixNextSymbol
JustAnotherSymbol:
mov bx,Value[si]
test Attributes[bx],#Calculated
jz FixNextSymbol
mov ax,Value[bx]
Evaluated:
movb fixation,#1
or Attributes[si],#Calculated
mov Value[si],ax
jmps FixNextSymbol
EndedFix:
cmpb fixation,#1
jnz DoneFixes
jmp FixUnknowns
DoneFixes:
ret
AddOffEnt:
push di
mov di,EmptyOffent
stosw
mov ax,LocationCounter
add ax,bx
stosw
mov EmptyOffent,di
pop di
ret
JumpOutOfRange:
mov bx,#JumpErrorMessage
call PanicRecover
PatchCode:
mov si,#OffsetTableStart
NextOffent:
cmp si,EmptyOffent
jz EndOfOffs
lodsw
mov bx,ax
lodsw
add ax,#OutputStart
mov di,ax
mov ax,[di]
testb al,#2
jnz RelativePatch
testb al,#1
mov ax,Value[bx]
jz bytemove
mov [di],ax
jmps NextOffent
EndOfOffs:
ret
RelativePatch:
testb al,#1
jz bytepatch
mov ax,di
add ax,#2
sub ax,#OutputStart
sub ax,Value[bx]
neg ax
mov [di],ax
jmps NextOffent
bytepatch:
mov ax,di
inc ax
sub ax,#OutputStart
sub ax,Value[bx]
neg ax
rolb al
rorb al
adcb ah,#0
jnz JumpOutOfRange
movb [di],al
jmps NextOffent
bytemove:
movb [di],al
jmps NextOffent