forked from qrush/unix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbas0.s
347 lines (327 loc) · 3.54 KB
/
bas0.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
/ bas0 -- basic
.globl main
.globl sin, cos, log, exp, atan, pow
.globl atoi, atof, ftoa, ftoo
.globl rand, srand
one = 40200
main:
setd
sys time
mov r1,r0
mov r0,randx
jsr pc,srand
sys intr; intrup
mov sp,gsp
clr seeka
mov $'a,r1
1:
movb r1,tmpf+8
sys stat; tmpf; line
bes 1f
inc r1
cmp r1,$'z
blos 1b
br 2f
1:
sys creat; tmpf; 14
bes 2f
mov r0,tfo
sys open; tmpf; 0
bec 1f
2:
mov $3f,r0
jsr pc,print
sys exit
3:
<Tmp file?\n\0>; .even
1:
mov r0,tfi
jsr pc,isymtab
cmp (sp),$2
blt loop
mov 4(sp),0f
sys open; 0:..; 0
bes 1f
mov r0,fi
br loop
1:
mov $1f,r0
jsr pc,print
br loop
1:
<Cannot open file\n\0>; .even
intrup:
mov $'\n,r0
jsr r5,putc
jsr r5,error
<ready\n\0>; .even
loop:
mov gsp,sp
clr lineno
jsr pc,rdline
mov $line,r3
1:
movb (r3),r0
jsr pc,digit
br 1f
jsr r5,atoi; nextc
cmp r0,$' /
bne 1f
mov $lintab,r3
mov r1,r0
bgt 2f
jsr pc,serror
2:
cmp r0,(r3)
beq 2f
tst (r3)
beq 2f
add $6,r3
br 2b
2:
cmp r3,$elintab-12.
blo 2f
jsr r5,error
<too many lines\n\0>; .even
2:
mov r0,(r3)+
mov seeka,(r3)+
mov tfo,r0
sys seek; seeka:..; 0
mov $line,r0
jsr pc,size
inc r0
add r0,seeka
mov r0,0f
mov tfo,r0
sys write; line; 0:..
br loop
1:
mov $line,r3
jsr pc,singstat
br loop
nextc:
movb (r3)+,r0
rts r5
size:
clr -(sp)
1:
inc (sp)
cmpb (r0)+,$'\n
bne 1b
mov (sp)+,r0
rts pc
rdline:
mov $line,0f
1:
mov fi,r0
sys read; 0:..; 1
bes 2f
tst r0
beq 2f
cmp 0b,$line+99.
bhis 2f / bad check, but a check
movb *0b,r0
inc 0b
cmp r0,$'\n
bne 1b
clrb *0b
rts pc
2:
mov fi,r0
beq 1f
sys close
clr fi
br 1b
1:
jmp _done
error:
tst fi
beq 1f
sys close
clr fi
1:
tst lineno
beq 1f
jsr pc,nextlin
br 1f
mov $line,r0
jsr pc,print
1:
mov r5,r0
jsr pc,print
jmp loop
serror:
dec r3
tst fi
beq 1f
sys close
clr fi
1:
mov $line,r1
1:
cmp r1,r3
bne 2f
mov $'_,r0
jsr r5,putc
mov $10,r0
jsr r5,putc
2:
movb (r1),r0
jsr r5,putc
cmpb (r1)+,$'\n
bne 1b
jmp loop
print:
mov r0,0f
jsr pc,size
mov r0,0f+2
mov $1,r0
sys write; 0:..; ..
rts pc
digit:
cmp r0,$'0
blo 1f
cmp r0,$'9
bhi 1f
add $2,(sp)
1:
rts pc
alpha:
cmp r0,$'a
blo 1f
cmp r0,$'z
bhi 1f
add $2,(sp)
1:
rts pc
name:
mov $nameb,r1
clr (r1)
clr 2(r1)
1:
cmp r1,$nameb+4
bhis 2f
movb r0,(r1)+
2:
movb (r3)+,r0
jsr pc,alpha
br 2f
br 1b
2:
jsr pc,digit
br 2f
br 1b
2:
mov $resnam,r1
1:
cmp nameb,(r1)
bne 2f
cmp nameb+2,2(r1)
bne 2f
sub $resnam,r1
asr r1
add $2,(sp)
rts pc
2:
add $4,r1
cmp r1,$eresnam
blo 1b
mov $symtab,r1
1:
tst (r1)
beq 1f
cmp nameb,(r1)
bne 2f
cmp nameb+2,2(r1)
bne 2f
rts pc
2:
add $14.,r1
br 1b
1:
cmp r1,$esymtab-28.
blo 1f
jsr r5,error
<out of symbol space\n\0>; .even
1:
mov nameb,(r1)
mov nameb+2,2(r1)
clr 4(r1)
clr 14.(r1)
rts pc
skip:
cmp r0,$' /
bne 1f
movb (r3)+,r0
br skip
1:
rts pc
putc:
tstb drflg
beq 1f
jsr pc,drput
rts r5
1:
mov r0,ch
mov $1,r0
sys write; ch; 1
rts r5
nextlin:
clr -(sp)
mov $lintab,r1
1:
tst (r1)
beq 1f
cmp lineno,(r1)
bhi 2f
mov (sp),r0
beq 3f
cmp (r0),(r1)
blos 2f
3:
mov r1,(sp)
2:
add $6,r1
br 1b
1:
mov (sp)+,r1
beq 1f
mov (r1)+,lineno
mov (r1)+,0f
mov tfi,r0
sys seek; 0:..; 0
mov tfi,r0
sys read; line; 100.
add $2,(sp)
1:
rts pc
getloc:
mov $lintab,r1
1:
tst (r1)
beq 1f
cmp r0,(r1)
beq 2f
add $6,r1
br 1b
1:
jsr r5,error
<label not found\n\0>; .even
2:
rts pc
isymtab:
mov $symtab,r0
mov $symtnam,r1
clrf fr0
movf $one,fr1
1:
mov (r1)+,(r0)+
mov (r1)+,(r0)+
mov $1,(r0)+
subf r1,r0
movf r0,(r0)+
cmp r1,$esymtnam
blo 1b
clr (r0)+
rts pc