-
Notifications
You must be signed in to change notification settings - Fork 13
/
bai8.asm
352 lines (340 loc) · 6.76 KB
/
bai8.asm
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
.data
start: .asciiz "Nhap chuoi ky tu : "
hex: .byte '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
d1: .space 4
d2: .space 4
d3: .space 4
array: .space 32
string: .space 5000
enter: .asciiz "\n"
error_length: .asciiz "Do dai chuoi khong hop le! Nhap lai.\n"
m: .asciiz " Disk 1 Disk 2 Disk 3\n"
m2: .asciiz "---------------- ---------------- ----------------\n"
m3: .asciiz "| "
m4: .asciiz " | "
m5: .asciiz "[[ "
m6: .asciiz "]] "
comma: .asciiz ","
ms: .asciiz "Try again?"
.text
la $s1, d1
la $s2, d2
la $s3, d3
la $a2, array # dia chi mang chua parity
input: li $v0, 4 # nhap ten (chuoi)
la $a0, start
syscall
li $v0, 8
la $a0, string
li $a1, 1000
syscall
move $s0, $a0 # s0 chua dia chi xau moi nhap
li $v0, 4
la $a0, m
syscall
li $v0, 4
la $a0, m2
syscall
#-----------------------kiem tra do dai co chia het cho 8 khong--------------------------
length: addi $t3, $zero, 0 # t3 = length
addi $t0, $zero, 0 # t0 = index
check_char: add $t1, $s0, $t0 # t1 = address of string[i]
lb $t2, 0($t1) # t2 = string[i]
nop
beq $t2, 10, test_length # t2 = '\n' ket thuc xau
nop
addi $t3, $t3, 1 # length++
addi $t0, $t0, 1 # index++
j check_char
nop
test_length: move $t5, $t3
and $t1, $t3, 0x0000000f # xoa het cac byte cua $t3 ve 0, chi giu lai byte cuoi
bne $t1, 0, test1 # byte cuoi bang 0 hoac 8 thi so chia het cho 8
j split1
test1: beq $t1, 8, split1
j error1
error1: li $v0, 4
la $a0, error_length
syscall
j input
#-------------------------------ket thuc kiem tra do dai----------------------------------
#------------------------lay parity----------------------------
HEX: li $t4, 7
loopH: blt $t4, $0, endloopH
sll $s6, $t4, 2 # s6 = t4*4
srlv $a0, $t8, $s6 # a0 = t8>>s6
andi $a0, $a0, 0x0000000f # a0 = a0 & 0000 0000 0000 0000 0000 0000 0000 1111 => lay byte cuoi cung cua a0
la $t7, hex
add $t7, $t7, $a0
bgt $t4, 1, nextc
lb $a0, 0($t7) # print hex[a0]
li $v0, 11
syscall
# lb $t6, 0($t7)
# beq $t6, 48, in
nextc: addi $t4,$t4,-1
j loopH
# in: bgt $t4, 1, loopH
# move $a0, $t6
# li $v0, 11
# syscall
endloopH: jr $ra
#----------------------------------------------------------
#------------------------------mo phong RAID 5-------------------------------
#-----------------------xet 6 khoi dau----------------------
#----------------lan 1: luu vao 2 khoi 1,2; xor vao 3-------
split1: addi $t0, $zero, 0 # so byte duoc in ra (4 byte)
addi $t9, $zero, 0
addi $t8, $zero, 0
la $s1, d1
la $s2, d2
la $a2, array
print11:li $v0, 4
la $a0, m3
syscall
b11: lb $t1, ($s0)
addi $t3, $t3, -1
sb $t1, ($s1)
b21: add $s5, $s0, 4
lb $t2, ($s5) # t2 chua dia chi tung byte cua dick 2
addi $t3, $t3, -1
sb $t2, ($s2)
b31: xor $a3, $t1, $t2
sw $a3, ($a2)
addi $a2, $a2, 4
addi $t0, $t0, 1
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s2, $s2, 1
bgt $t0, 3, reset
j b11
reset: la $s1, d1
la $s2, d2
print12:lb $a0, ($s1)
li $v0, 11
syscall
addi $t9, $t9, 1
addi $s1, $s1, 1
bgt $t9, 3, next11
j print12
next11: li $v0, 4
la $a0, m4
syscall
li $v0, 4
la $a0, m3
syscall
print13:lb $a0, ($s2)
li $v0, 11
syscall
addi $t8, $t8, 1
addi $s2, $s2, 1
bgt $t8, 3, next12
j print13
next12: li $v0, 4
la $a0, m4
syscall
li $v0, 4
la $a0, m5
syscall
la $a2, array
addi $t9, $zero, 0
print14:lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, comma
syscall
addi $t9, $t9, 1
addi $a2, $a2, 4
bgt $t9, 2, end1 # in ra 3 parity dau co dau ",", parity cuoi cung k co
j print14
end1: lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, m6
syscall
li $v0, 4
la $a0, enter
syscall
beq $t3, 0, exit1
#-----------------------------------------------------------
split2: la $a2, array
la $s1, d1
la $s3, d3
addi $s0, $s0, 4
addi $t0, $zero, 0
print21:li $v0, 4
la $a0, m3
syscall
b12: lb $t1, ($s0)
addi $t3, $t3, -1
sb $t1, ($s1)
b32: add $s5, $s0, 4
lb $t2, ($s5)
addi $t3, $t3, -1
sb $t2, ($s3)
b22: xor $a3, $t1, $t2
sw $a3, ($a2)
addi $a2, $a2, 4
addi $t0, $t0, 1
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s3, $s3, 1
bgt $t0, 3, reset2
j b12
reset2: la $s1, d1
la $s3, d3
addi $t9, $zero, 0
print22:lb $a0, ($s1)
li $v0, 11
syscall
addi $t9, $t9, 1
addi $s1, $s1, 1
bgt $t9, 3, next21
j print22
next21: li $v0, 4
la $a0, m4
syscall
la $a2, array
addi $t9, $zero, 0
li $v0, 4
la $a0, m5
syscall
print23:lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, comma
syscall
addi $t9, $t9, 1
addi $a2, $a2, 4
bgt $t9, 2, next22
j print23
next22: lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, m6
syscall
li $v0, 4
la $a0, m3
syscall
addi $t8, $zero, 0
print24:lb $a0, ($s3)
li $v0, 11
syscall
addi $t8, $t8, 1
addi $s3, $s3, 1
bgt $t8, 3, end2
j print24
end2: li $v0, 4
la $a0, m4
syscall
li $v0, 4
la $a0, enter
syscall
beq $t3, 0, exit1
#------------------------------------------------------------
split3: la $a2, array
la $s2, d2
la $s3, d3
addi $s0, $s0, 4
addi $t0, $zero, 0
print31:li $v0, 4
la $a0, m5
syscall
b23: lb $t1, ($s0)
addi $t3, $t3, -1
sb $t1, ($s1)
b33: add $s5, $s0, 4
lb $t2, ($s5)
addi $t3, $t3, -1
sb $t2, ($s3)
b13: xor $a3, $t1, $t2
sw $a3, ($a2)
addi $a2, $a2, 4
addi $t0, $t0, 1
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s3, $s3, 1
bgt $t0, 3, reset3
j b23
reset3: la $s2, d2
la $s3, d3
la $a2, array
addi $t9, $zero, 0
print32:lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, comma
syscall
addi $t9, $t9, 1
addi $a2, $a2, 4
bgt $t9, 2, next31
j print32
next31: lb $t8, ($a2)
jal HEX
li $v0, 4
la $a0, m6
syscall
li $v0, 4
la $a0, m3
syscall
addi $t9, $zero, 0
print33:lb $a0, ($s2)
li $v0, 11
syscall
addi $t9, $t9, 1
addi $s2, $s2, 1
bgt $t9, 3, next32
j print33
next32: addi $t9, $zero, 0
addi $t8, $zero, 0
li $v0, 4
la $a0, m4
syscall
li $v0, 4
la $a0, m3
syscall
print34:lb $a0, ($s3)
li $v0, 11
syscall
addi $t8, $t8, 1
addi $s3, $s3, 1
bgt $t8, 3, end3
j print34
end3: li $v0, 4
la $a0, m4
syscall
li $v0, 4
la $a0, enter
syscall
beq $t3, 0, exit1
#--------------------------------end 6 khoi dau---------------------------------------
# chuyen sang 6 khoi tiep theo
nextloop: addi $s0, $s0, 4
j split1
exit1: li $v0, 4
la $a0, m2
syscall
j ask
#-------------------------------ket thuc mo phong RAID 5-----------------------------------
#-------------------------------------try again----------------------------------------
ask: li $v0, 50
la $a0, ms
syscall
beq $a0, 0, clear
nop
j exit
nop
# clear: dua string ve trang thai ban dau de thuc hien lai qua trinh
clear: la $s0, string
add $s3, $s0, $t5 # s3: dia chi byte cuoi cung duoc su dung trong string
li $t1, 0
goAgain: sb $t1, ($s0) # set byte o dia chi s0 thanh 0
nop
addi $s0, $s0, 1
bge $s0, $s3, input
nop
j goAgain
nop
#-----------------------------------end try again----------------------------------------
exit: li $v0, 10
syscall