-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
360 lines (259 loc) · 6.97 KB
/
main.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
353
354
355
356
357
358
359
360
# Trabalho de Organizacao de Computadores (DCC070 - 2023.1)
# Jonatas Dias Machado Costa
# Maria Luisa Riolino Guimaraes
# Nicolas Soares Martins
.data
vet: .word 0:19
.text
.eqv SIZE 20
.eqv nline 10
.main:
#endereco de memoria do vetor
la $a0, vet
#tamanho do vetor
li $a1, SIZE
#ultimo valor
li $a2, 71
jal inicializaVetor
#copia valores de retorno
move $t0, $v0
move $t1, $v1
#soma retornos
add $t3, $t1, $t0
add $s6, $zero, $t3
#li $v0, 1
#add $a0, $t3, $zero
#syscall
#imprime o vetor
la $a0, vet
li $a1, SIZE
jal imprimeVetor
la $a0, vet
li $a1, SIZE
jal ordenaVetor
la $a0, vet
li $a1, SIZE
jal imprimeVetor
#carrega endereco da primeira posicao do vetor
la $t0, vet
li $t3, SIZE
sll $t3, $t3, 2
#carrega ultima posicao do vetor
add $t1, $t0, $t3
move $a0, $t0
move $a1, $t1
jal zeraVetor
la $a0, vet
li $a1, SIZE
jal imprimeVetor
#imprime Soma:
li $v0, 11
#S
li $a0, 83
syscall
#o
li $a0, 111
syscall
#m
li $a0, 109
syscall
#a
li $a0, 97
syscall
#:
li $a0, 58
syscall
#espaco em branco
li $a0, 32
syscall
#imprime a soma em si
li $v0, 1
add $a0, $s6, $zero
syscall
#imprime \n
li $v0, 11
li $a1, nline
li $v0, 10
syscall
inicializaVetor:
#prepara stack para 5 valores
addi $sp, $sp -24
sw $ra, 0($sp)
#carrega valores em registradores s
add $s0, $zero, $a0 #vetor
add $s1 ,$zero, $a1 #tamanho
add $s2, $zero, $a2 #ultimo valor
#salva valores na stack
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
#caso trivial, tam =0
li $t3, 0
ble $s1, $t3, end
#carrega argumentos de valorAleatorio
move $a0, $s2
li $a1, 47
li $a2, 97
li $a3, 337
li $s4, 3
#carrega quinto argumento
sw $s4, 16($sp)
jal valorAleatorio
move $t2, $v1
#subtrai tam-1
addi $s1, $s1, -1
#carrega bytes do vetor
add $t0, $zero, $s1
sll $t0, $t0, 2
#vai para o indice tam-1
add $s0, $s0, $t0
#salva valor aleatorio
sw $t2, 0($s0)
#volta o indice
sub $s0, $s0, $t0
#carrega argumentos novamente
add $a0, $zero, $s0
add $a1, $zero, $s1
add $a2, $zero, $t2
#salva novoValor
add $s5, $s5, $t2
sw $s5, 20($sp)
jal inicializaVetor
end:
#carrega caso trivial
li $v0, 0
#recupera endereco de retorno
lw $ra, 0($sp)
#recupera somatorio
add $v1, $zero, $s5
#desaloca stack
addi $sp, $sp, 24
jr $ra
valorAleatorio:
mult $a0, $a1 # a * b
mflo $t0
add $t1, $t0, $a2 # a * b + c
div $t1, $a3 # (a * b + c) % d
mfhi $t2
sub $v1, $t2, $s4 # (a * b + c) % d - e
jr $ra # retorna
imprimeVetor:
# contador do for
li $t0, 0
#$t1 mantem o indice do vetor
add $t1, $zero,$a0
#tamanho do vetor em $s1
add $t3, $zero,$a1
# multiplica por 4 para a quantidade de bytes
sll $t3,$t3, 2
for:
#incrementa o indice
addi $t0, $t0, 4
#checa condicao de parada
bgt $t0, $t3, fim_for
#carrega valor localizado no vetor
lw $t2, 0($t1)
#prepara o print
li $v0,1
#carrega valor no registrador $a0
move $a0, $t2
#printa
syscall
#printa espaco em branco
li $a0, 32
li $v0, 11
syscall
#incrementa posicao do vetor
addi $t1,$t1, 4
j for
fim_for:
li $v0, 11
li $a0, nline
syscall
jr $ra
ordenaVetor:
addi $sp, $sp, -20 # ajusta a pilha
sw $ra, 16($sp) # salva $ra
sw $s3, 12($sp) # salva $s3
sw $s2, 8($sp) # salva $s2
sw $s1, 4($sp) # salva $s1
sw $s0, 0($sp) # salva $s0
move $s2, $a0 # salva vetor em $s2
move $s3, $a1 # salva tamanho do vetor em $s3
move $s0, $zero # i = 0
for_i:
subi $t0, $s3, 1 # $t0 = n-1
bge $s0, $t0, fim_for_i # testa condicao de parada (se i >= n-1, vai para fim_for_i)
move $t1, $s0 # $t1 = min_idx = i
addi $s1, $s0, 1 # j = i + 1
for_j:
bge $s1, $s3, fim_for_j # testa condicao de parada (j < n)
sll $t2, $s1, 2 # $t2 = j * 4
add $t3, $s2, $t2 # $t3 = &vet[j]
lw $t4, 0($t3) # $t4 = vet[j]
sll $t5, $t1, 2 # $t5 = min_idx * 4
add $t6, $s2, $t5 # $t6 = &vet[min_idx]
lw $t7, 0($t6) # $t7 = vet[min_idx]
move $t8, $s1 # $t8 = j
addi $s1, $s1, 1 # j++
bge $t4, $t7, for_j # se (vet[j] >= vet[min_idx]), volta pro inicio do loop
move $t1, $t8 # min_idx = j
j for_j # vai pro inicio do for_j
fim_for_j:
move $t8, $s0 # $t8 = i
addi $s0, $s0, 1 # i++
beq $t1, $t8, for_i # se (min_idx == i), vai pro inicio do for_i
# passa &vet[i] e &vet[min_idx] como argumentos para troca
sll $t2, $t8, 2 # $t2 = i * 4
add $t3, $s2, $t2 # $t3 = &vet[i]
sll $t5, $t1, 2 # $t5 = min_idx * 4
add $t6, $s2, $t5 # $t6 = &vet[min_idx]
move $a1, $t6 # $a1 = &vet[min_idx]
move $a2, $t3 # $a2 = &vet[i]
jal troca # troca(&vet[min_idx], &vet[i])
j for_i # vai para for_i (loop)
fim_for_i:
lw $s0, 0($sp) # restaura $s0
lw $s1, 4($sp) # restaura $s1
lw $s2, 8($sp) # restaura $s2
lw $s3, 12($sp) # restaura $s3
lw $ra, 16($sp) # restaura $ra
addi $sp, $sp, 20 # ajusta a pilha
jr $ra # retorna
troca:
# salva o endereco de retorno
addi $sp, $sp, -4
sw $ra, 0($sp)
add $t0, $zero, $a1 # t0 tem o indice da primeira posicao
lw $t1, ($t0) # t1 tem o valor da primeira posicao
add $t0, $zero, $a2 # t0 tem o indice da segunda posicao
lw $t2, ($t0) # t2 tem o valor da segunda posicao
beq $t1, $t2, end_troca # se os valores forem iguais, vai pro fim
sw $t1, ($t0) # salva o valor da primeira posicao na segunda
add $t0, $zero, $a1 # t0 tem o indice da primeira posicao
sw $t2, ($t0) # salva o valor da segunda posicao na primeira
j end_troca
end_troca:
# recupera o endereco de retorno
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
zeraVetor:
# salva o endereco de retorno
addi $sp, $sp, -4
sw $ra, 0($sp)
for_loop:
# compara o contador com o tamanho do vetor
bge $a0, $a1, exit_loop
# zera o valor da posicao atual do vetor
sw $zero, 0($a0)
# vai pra pr?xima posicao do vetor
addi $a0, $a0, 4
# volta pro inicio do loop
j for_loop
exit_loop:
# restaura o endereco de retorno em $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# sai da funcao
jr $ra