-
Notifications
You must be signed in to change notification settings - Fork 0
/
LISTBUBBLESORT
208 lines (151 loc) · 3.07 KB
/
LISTBUBBLESORT
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
.eqv char $s0
.eqv index_lb $t0
.eqv digit $s1
.eqv num $s2
.eqv buffer $s3
.eqv multiplicador $t1
.eqv index_sw $t2
.eqv index_wr $t3
.eqv endereco $t8
.eqv lenght 400
.eqv quociente $t6
.eqv resto $t7
.eqv char2 $s4
.eqv aux $s5
.eqv flag $t4
.data
lista_numerica: .space 1024
.align 2
fout: .asciiz "lista.txt"
fin: .asciiz "lista2.txt"
.text
li index_sw, 0
li index_lb, 0
li index_wr, 0
open_file:
li $v0, 13
la $a0, fout
li $a1, 0
li $a2, 0
syscall
move buffer, $v0
read_file:
li $v0, 14
move $a0, buffer
la $a1, lista_numerica
li $a2, 1024
syscall
move $s3, $v0
initiate:
li num, 0
li multiplicador, 1
loop:
lb char, lista_numerica(index_lb)
add index_lb, index_lb, 1
beq char, 0, store
beq char, ',', store
beq char, '-', sinal
sub digit, char, 0x30 #char2num
mul num, num, 10
add num, num, digit
j loop
sinal:
li multiplicador, -1
j loop
store:
mul num, num, multiplicador
sw num, lista_numerica(index_sw)
add index_sw, index_sw, 4
beq char, 0, initsort
j initiate
initsort:
li index_lb, 0
li index_sw, 0
li flag, 0 #se flag =0, sair
loopBubbleSort:
lw char, lista_numerica(index_lb)
add index_lb, index_lb, 4
lw char2, lista_numerica(index_lb)
bgt char, char2, sort
bgt index_lb, 401, verifyflag
addi index_sw, index_sw, 4
j loopBubbleSort
sort:
li flag, 1
move aux, char
move char, char2
sw char, lista_numerica(index_sw)
addi index_sw, index_sw, 4
move char2, aux
sw char2, lista_numerica(index_sw)
j loopBubbleSort
verifyflag:
beq flag, 0, converter_para_string
beq flag, 1, initsort
.data
file_name: .asciiz "list1.txt"
.align 2
lista_char: .space 1024
buffw: .space 1024
.text
converter_para_string:
#CONVERTER PARA STRING
li index_lb, 0 #indexLista
li index_sw, 0 #indexNum
restart:
li flag, 0
li index_wr, 0 #indexBuffer
teste_sinal:
bge index_sw, lenght, escrita
lw num, lista_numerica(index_sw)
add index_sw, index_sw, 4
bge num, $zero, converte
li flag, 1
mul num, num, -1
converte:
move quociente, num
proximo:
li $t9, 10
div quociente, $t9 #div por 10
mfhi resto
mflo quociente
add char, resto, 0x30
sb char, buffw(index_wr)
add index_wr, index_wr, 1
bnez quociente, proximo
beqz flag, ordena_digitos #se positivo
li $t9, '-' #senao
sb $t9, buffw(index_wr)
add index_wr, index_wr, 1
ordena_digitos:
beqz index_wr, sai
add index_wr, index_wr, -1
lb char, buffw(index_wr)
sb char, lista_char(index_lb)
add index_lb, index_lb, 1
j ordena_digitos
sai:
bge index_sw, lenght, escrita
li $t9, ','
sb $t9, lista_char(index_lb)
add index_lb, index_lb, 1
j restart
escrita:
li $v0, 13
la $a0, file_name
li $a1, 1
li $a2, 0
syscall
read:
move $s7, $v0
li $v0, 15
move $a0, $s7
la $a1, lista_char
li $a2, 1023
syscall
close:
li $v0, 16
move $a0, $s7
exit:
li $v0, 10
syscall