-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.s
326 lines (292 loc) · 4.69 KB
/
runtime.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
# ============================================================
# ================== TIGER RUNTIME LIBRARY ===================
# ============================================================
# int *initArray(int size, int init)
# {int i;
# int *a = (int *)malloc(size*sizeof(int));
# for(i=0;i<size;i++) a[i]=init;
# return a;
# }
.text
initArray:
sll $a0,$a0,2
li $v0,9
syscall
move $a2,$v0
b Lrunt2
Lrunt1:
sw $a1,($a2)
sub $a0,$a0,4
add $a2,$a2,4
Lrunt2:
bgtz $a0, Lrunt1
j $ra
#
# int *allocRecord(int size)
# {int i;
# int *p, *a;
# p = a = (int *)malloc(size);
# for(i=0;i<size;i+=sizeof(int)) *p++ = 0;
# return a;
# }
allocRecord:
li $v0,9
syscall
move $a2,$v0
b Lrunt4
Lrunt3:
sw $0,($a2)
sub $a0,$a0,4
add $a2,$a2,4
Lrunt4:
bgtz $a0, Lrunt3
j $ra
# struct string {int length; unsigned char chars[1];};
#
# int stringEqual(struct string *s, struct string *t)
# {int i;
# if (s==t) return 1;
# if (s->length!=t->length) return 0;
# for(i=0;i<s->length;i++) if (s->chars[i]!=t->chars[i]) return 0;
# return 1;
# }
stringEqual:
beq $a0,$a1,Lrunt10
lw $a2,($a0)
lw $a3,($a1)
addiu $a0,$a0,4
addiu $a1,$a1,4
beq $a2,$a3,Lrunt11
Lrunt13:
li $v0,0
j $ra
Lrunt12:
lbu $t0,($a0)
lbu $t1,($a1)
bne $t0,$t1,Lrunt13
addiu $a0,$a0,1
addiu $a1,$a1,1
addiu $a2,$a2,-1
Lrunt11:
bgez $a2,Lrunt12
Lrunt10:
li $v0,1
j $ra
#
# void print(struct string *s)
# {int i; unsigned char *p=s->chars;
# for(i=0;i<s->length;i++,p++) putchar(*p);
# }
print:
lw $a1,0($a0)
add $a0,$a0,4
add $a2,$a0,$a1
lb $a3,($a2)
sb $0,($a2)
li $v0,4
syscall
sb $a3,($a2)
j $ra
# void flush()
# {
# fflush(stdout);
# }
flush:
j $ra
# int main()
# {int i;
# for(i=0;i<256;i++)
# {consts[i].length=1;
# consts[i].chars[0]=i;
# }
# return t_main(0 /* static link */);
# }
.data
Runtconsts:
.space 2048
Runtempty: .word 0
.text
main:
li $a0,0
la $a1,Runtconsts
li $a2,1
Lrunt20:
sw $a2,($a1)
sb $a0,4($a1)
addiu $a1,$a1,8
slt $a3,$a0,256
bnez $a3,Lrunt20
li $a0,0
j t_main
# int ord(struct string *s)
# {
# if (s->length==0) return -1;
# else return s->chars[0];
# }
ord:
lw $a1,($a0)
li $v0,-1
beqz $a1,Lrunt5
lbu $v0,4($a0)
Lrunt5:
j $ra
# struct string empty={0,""};
# struct string *chr(int i)
# {
# if (i<0 || i>=256)
# {printf("chr(%d) out of range\n",i); exit(1);}
# return consts+i;
# }
.data
Lrunt30: .asciiz "character out of range\n"
.text
chr:
andi $a1,$a0,255
bnez $a1,Lrunt31
sll $a0,$a0,3
la $v0,Runtconsts($a0)
j $ra
Lrunt31:
li $v0,4
la $a0,Lrunt30
syscall
li $v0,10
syscall
# int size(struct string *s)
# {
# return s->length;
# }
size:
lw $v0,($a0)
j $ra
# struct string *substring(struct string *s, int first, int n)
# {
# if (first<0 || first+n>s->length)
# {printf("substring([%d],%d,%d) out of range\n",s->length,first,n);
# exit(1);}
# if (n==1) return consts+s->chars[first];
# {struct string *t = (struct string *)malloc(sizeof(int)+n);
# int i;
# t->length=n;
# for(i=0;i<n;i++) t->chars[i]=s->chars[first+i];
# return t;
# }
# }
#
.data
Lrunt40: .asciiz "substring out of bounds\n"
substring:
lw $t1,($a0)
bltz $a1,Lrunt41
add $t2,$a1,$a2
sgt $t3,$t2,$t1
bnez $t3,Lrunt41
add $t1,$a0,$a1
bne $a2,1,Lrunt42
lbu $a0,($t1)
b chr
Lrunt42:
bnez $a2,Lrunt43
la $v0,Lruntempty
j $ra
Lrunt43:
addi $a0,$a2,4
li $v0,9
syscall
move $t2,$v0
Lrunt44:
lbu $t3,($t1)
sb $t3,($t2)
addiu $t1,1
addiu $t2,1
addiu $a2,-1
bgtz $a2,Lrunt44
j $ra
Lrunt41:
li $v0,4
la $a0,Lrunt40
syscall
li $v0,10
syscall
# struct string *concat(struct string *a, struct string *b)
# {
# if (a->length==0) return b;
# else if (b->length==0) return a;
# else {int i, n=a->length+b->length;
# struct string *t = (struct string *)malloc(sizeof(int)+n);
# t->length=n;
# for (i=0;i<a->length;i++)
# t->chars[i]=a->chars[i];
# for(i=0;i<b->length;i++)
# t->chars[i+a->length]=b->chars[i];
# return t;
# }
# }
concat:
lw $t0,($a0)
lw $t1,($a1)
beqz $t0,Lrunt50
beqz $t1,Lrunt51
addiu $t2,$a0,4
addiu $t3,$a1,4
add $t4,$t0,$t1
addiu $a0,$t4,4
li $v0,9
syscall
addiu $t5,$v0,4
sw $t4,($v0)
Lrunt52:
lbu $a0,($t2)
sb $a0,($t5)
addiu $t2,1
addiu $t5,1
addiu $t0,-1
bgtz $t0,Lrunt52
Lrunt53:
lbu $a0,($t4)
sb $a0,($t5)
addiu $t4,1
addiu $t5,1
addiu $t2,-1
bgtz $t2,Lrunt52
j $ra
Lrunt50:
move $v0,$a1
j $ra
Lrunt51:
move $v0,$a0
j $ra
# int not(int i)
# { return !i;
# }
#
_not:
seq $v0,$a0,0
j $ra
# #undef getchar
#
# struct string *getchar()
# {int i=getc(stdin);
# if (i==EOF) return ∅
# else return consts+i;
# }
.data
getchbuf: .space 200
getchptr: .word getchbuf
.text
getchar:
lw $a0,getchptr
lbu $v0,($a0)
add $a0,$a0,1
bnez $v0,Lrunt6
li $v0,8
la $a0,getchbuf
li $a1,200
syscall
la $a0,getchbuf
lbu $v0,($a0)
bnez $v0,Lrunt6
li $v0,-1
Lrunt6:
sw $a0,getchptr
j $ra