rss
twitter
facebook

Home

Ordenar

.data
.align 2
lista: .space 40 #int lista[10]
prompt: .asciiz " Insira um inteiro " #char prompt[18]

.text
.globl main

#le a lista dos numeros a ordenar

main: la $a3,lista #a3=lista
li $t0,0 #i=0
readlp: la $a0,prompt
li $v0,4
syscall #puts prompt
li $v0,5
syscall #geti v0
sw $v0,0($a3)
add $a3,$a3,4
add $t0,$t0,1 #i=i+1
slt $t1,$t0,10 #if(t0<10) t1=1
bnez $t1,readlp
# ordenaçao da lista
li $t0,0 # i=0
for1: bge $t0,9,end_for1 # while (i<9)
la $s0,lista
sll $s1,$t0,2 #s1=i*4
add $s0,$s0,$s1 #s0=&(lista[i])
addi $t1,$t0,1 # j=i+1

for2: bge $t1,10,end_for2 # while (j<10)
la $s2,lista
sll $s1,$t1,2 #s1=j*4
add $s2,$s2,$s1 #s0=&(lista[j])
lw $t2,0($s0) # aux=lista[i]
lw $t3,0($s2) # aux1=lista[j]
if: ble $t2,$t3,end_if # if (aux>aux1)
sw $t3,0($s0) # lista[i]=aux1
sw $t2,0($s2) # lista[j]=aux

end_if: addi $t1,$t1,1 # j++
b for2
end_for2: addi $t0,$t0,1 # i++
b for1
end_for1: li $v0,10 # exit
syscall

0 comentários:

 
Powered by Blogger