-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasm.s
executable file
·274 lines (251 loc) · 7.2 KB
/
asm.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
PathSize = 64
BufferSize = 20
WordSize = 32
MaxIdentifierSize = 30
.include dos.i
.include symtab.i
Reg8 = 1
Reg16 = 2
SegReg = 3
Immediate = 4
AddMode = 5
Disp = 6
|AddDisp = 7 |Isn't ever used!
CR = 13
LF = 10
call GetFileName |Get the name of the file to be
|assembled into PresentFilename
call InitSymbolTable
call AssembleOneFile
jmp ExitToDos |Get back to DOS
CloseInputFile:
movb ah,#CloseFileFunction |Close the input file
mov bx,InputFileHandle
int #DosInterrupt
ret
CloseOutputFiles:
movb ah,#CloseFileFunction
mov bx,OutputFileHandle
int #DosInterrupt
movb ah,#CloseFileFunction
mov bx,ListFileHandle
int #DosInterrupt
ret
GetFileName:
mov si,#CommandLineStart |Get the start of the command line
lodsb |first byte is length of command line
orb al,al |If there is no command line then
jnz SkipCommandLineSpaces |show the
ReportNoName:
mov bx,#HelpMessage |
call Panic |help screen to the nosey user.
SkipCommandLineSpaces: |
lodsb |Get more chars from the command line
cmpb al,#' ' |Skip spaces at start of name
jz SkipCommandLineSpaces |Any number of them
cmpb al,#' ' |Also skip tabs
jz SkipCommandLineSpaces |
cmpb al,#CR
jz ReportNoName
mov di,StringSpace |Copy the name into the StringSpace
mov PresentFileNameOffset,di |
stosb |done by string insts.
mov bx,si |To display of the filename.
dec bx |Actually si is pointing at one
|past the start of the filename.
xorb cl,cl |cl is whether the filename
|given had an extension or not.
|If there was no extension then we
|have to put a .s after it.
| We assume that there is
|enough space to do such a thing
|in the command line.
FindCommandLineCR: |white spaces after the name of the
lodsb |should be removed
stosb |Keep Storing the name
cmpb al,#' ' |it means that that was the end of
jz FoundFilenameEnd |the filename
cmpb al,#' ' |This is true for tabs too
jz FoundFilenameEnd| |If a '.' is found, it means that
cmpb al,#'.' |an extension need not be added to
jnz NotTheDotInFilename |to the filename
incb cl |record the fact in cl that a dot
|has been seen in the input name
NotTheDotInFilename:
cmpb al,#'/' |reset the dot if a path separator
jnz NotSlashinFilename |is found
xorb cl,cl
NotSlashinFilename:
cmpb al,#'\'
jnz NotBackSlashInFilename
xorb cl,cl
NotBackSlashInFilename: |We have to check whether we
cmpb al,#CR |crash into the tail of the command
jnz FindCommandLineCR |line. Check for carriage return
FoundFilenameEnd: |Here we are at the end of the name
dec di |Adjust, 'cos even the carriage retn
orb cl,cl |or white space is in. If there was
jnz NoNeedToPutExtension |an extension don't put another
mov ax,#'s'!'.' |store an extension after the name
stosw |'.s'
NoNeedToPutExtension: |Terminate the name with a zero
movb al,#0 |character, C and DOS style
stosb |store the zero
call CheckStringTableOverflow
mov StringSpace,di
ret |return to main routine.
.include symtab.s
AssembleOneFile:
call OpenInputFile
jc InputFileOpenError
call ConstructOutputFileNames
mov dx,OutputFileNameOffset
call OpenOutputFile
mov OutputFileHandle,ax
mov dx,ListFileNameOffset
call OpenOutputFile
mov ListFileHandle,ax
call Assemble
call FixUnknowns
call PatchCode
call WriteOutputFile
call CloseInputFile
call WriteListFile
call CloseOutputFiles
ret
InputFileOpenError:
mov bx,#NoInputFileMessage |There was an error - so quit.
call Panic
OpenInputFile:
mov ax,#OpenFileFunction!ReadOnly
mov dx,PresentFileNameOffset
int #DosInterrupt
jc CouldNotOpenFile
mov InputFileHandle,ax |Preserving Carry dangerous stuff
mov InputLineNumber,#1
mov ax,#0
mov InputBufferReadPtr,ax |Both Should be the same
mov InputBufferEndPtr,ax
CouldNotOpenFile:
ret
ConstructOutputFileNames:
mov di,PresentFileNameOffset
movb al,#0
mov cx,#PathSize
repnz
scasb
dec di
std
mov cx,#PathSize
movb al,#'.'
repnz
scasb
cld
inc di
mov si,PresentFileNameOffset
mov cx,di
sub cx,si
mov dx,cx |save offset for list file opening
mov di,StringSpace
mov OutputFileNameOffset,di
rep
movsb
mov ax,#'c'!'.'
stosw
mov ax,#'m'!'o'
stosw
xorb al,al
stosb
mov si,PresentFileNameOffset
mov cx,dx
mov ListFileNameOffset,di
rep
movsb
mov ax,#'l'!'.'
stosw
mov ax,#'t'!'s'
stosw
xorb al,al
stosb
call CheckStringTableOverflow
mov StringSpace,di
ret
OpenOutputFile:
movb ah,#60
mov cx,#0
int #DosInterrupt
jnc OutputFileWasOpened
mov bx,#OutputFileMessage
call Panic
OutputFileWasOpened:
ret
RemoveOutputFile:
movb ah,#DeleteFileFunction
mov dx,OutputFileNameOffset
int #DosInterrupt
ret
|Assemble
|The main procedure for processing a file
|On input, the variable,
| InputFileHandle - is a valid handle of an opened file.
|
|
|
Assemble:
mov SavedStackPointer,sp
AssembleLabelTail:
xor ax,ax
mov bp,1
call GetEndOfLine1
jc AssembleEnd
Assemble1:
call GetWord
jc AssembleEnd
movb al,InputWord
cmpb al,#'|'
jnz NotAComment
call GetEndOfLine
jc AssembleEnd
jmps Assemble1
NotAComment:
mov si,#Directives
call MatchKeyword
jc NotDirective
call [si]
call GetEndOfLine
jc AssembleEnd
jmps Assemble1
NotDirective:
call GetInstruction
jc EquateOrLabel
call GetEndOfLine
jc AssembleEnd
jmps Assemble1
EquateOrLabel:
call DefineNewSymbol
or bx,#IsEquate
jz AssembleLabelTail
call GetEndOfLine
jc AssembleEnd
jmps Assemble1
AssembleEnd:
ret
NotImplemented:
ret
CheckStringTableOverflow:
cmp di,#StringTableEnd
jc NoStringOverflow
mov bx,#StrTblOverflowMessage
call Panic
NoStringOverflow:
ret
.include input.s
.include symbols.s
.include message.s
.include display.s
.include support.s
.include equ.s
.include expr.s
.include direct.s
.include output.s
EndOfCode: