-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.asm
228 lines (158 loc) · 2.97 KB
/
array.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
.data
dim: .word 0
address: .word 0
.text
.globl allocaArray
.globl carica
.globl stampaArray
.globl ordinaCrescente
.globl ordinaDecrescente
allocaArray: # a0 = dim
#salvo in stack
addi $sp,$sp,-4
sw $ra,0($sp)
la $t0,dim
sw $a0,0($t0)
mul $a0,$a0,4 #byte da allocare
li $v0,9
syscall
la $t1,address
sw $v0,0($t1)
#ripristino reg
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
# fine alloca
carica:
#salvo in stack
addi $sp,$sp,-4
sw $ra,0($sp)
la $t1,dim
lw $t0,0($t1) #t0 = dim
la $t1,address #indirizzo vettore
move $t2,$zero #i
move $t3,$zero #indirizzo elemento
loopCarica:
beq $t2,$t0 fineloopCarica
mul $t4,$t2,4 #offset
add $t3,$t4,$t1 #indirizzo
li $v0,5
syscall
sw $v0,0($t3)
addi $t2,$t2,1
j loopCarica
fineloopCarica:
#ripristino reg
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
#fine carica
stampaArray:
#salvo in stack
addi $sp,$sp,-4
sw $ra,0($sp)
la $t1,dim
lw $t0,0($t1) #t0 = dim
la $t1,address #indirizzo vettore
move $t2,$zero #i
move $t3,$zero #indirizzo elemento
loopStampa:
beq $t2,$t0 fineloopStampa
mul $t4,$t2,4 #offset
add $t3,$t4,$t1 #indirizzo
lw $a0,0($t3)
li $v0,1
syscall
li $a0,' '
li $v0,11
syscall
addi $t2,$t2,1
j loopStampa
fineloopStampa:
#ripristino reg
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
#fine stampaArray
ordinaCrescente:
#salvo in stack
addi $sp,$sp,-4
sw $ra,0($sp)
la $t0,dim
lw $t0,0($t0) # dim
addi $t1,$t0,-1 #dim-1
la $t2,address
move $t3,$zero #i
loopCi:
beq $t1,$t3 fineloopCi #dim -1 = i?
addi $t4,$t3,1 # j = i+1
loopCj:
beq $t0,$t4 fineloopCj #dim = j?
addi $sp,$sp,-8
sw $t3,0($sp) #sevono t3 e t4 per a[i] e a[j]
sw $t4,4($sp)
mul $t5,$t3,4
add $t5,$t5,$t2 # indirizzo a[i]
lw $t3,0($t5) #a[i]
mul $t6,$t4,4
add $t6,$t6,$t2 # indirizzo a[j]
lw $t4,0($t6) #a[j]
blt $t3,$t4 oltreC #t3 < #t4
sw $t3,0($t6)
sw $t4,0($t5)
oltreC:
lw $t4,4($sp)
lw $t3,0($sp)
addi $sp,$sp,8
addi $t4,$t4,1
j loopCj
fineloopCj:
addi $t3,$t3,1
j loopCi
fineloopCi:
#ripristino reg
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
#fine OrdinaCrescente
ordinaDecrescente:
#salvo in stack
addi $sp,$sp,-4
sw $ra,0($sp)
la $t0,dim
lw $t0,0($t0) # dim
addi $t1,$t0,-1 #dim-1
la $t2,address
move $t3,$zero #i
loopDi:
beq $t1,$t3 fineloopDi #dim -1 = i?
addi $t4,$t3,1 # j = i+1
loopDj:
beq $t0,$t4 fineloopDj #dim = j?
addi $sp,$sp,-8
sw $t3,0($sp) #sevono t3 e t4 per a[i] e a[j]
sw $t4,4($sp)
mul $t5,$t3,4
add $t5,$t5,$t2 # indirizzo a[i]
lw $t3,0($t5) #a[i]
mul $t6,$t4,4
add $t6,$t6,$t2 # indirizzo a[j]
lw $t4,0($t6) #a[j]
bgt $t3,$t4 oltreD #t3 > #t4
sw $t3,0($t6)
sw $t4,0($t5)
oltreD:
lw $t4,4($sp)
lw $t3,0($sp)
addi $sp,$sp,8
addi $t4,$t4,1
j loopDj
fineloopDj:
addi $t3,$t3,1
j loopDi
fineloopDi:
#ripristino reg
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
#fine OrdinaDecrescente