mips 指令對指定的陣列進行排序
阿新 • • 發佈:2019-02-12
.globl main
.data
Array:.word 2,4,6,1
.text
main:
addi $t5,$zero,4
addi $t4,$zero,3#i($t4)=3
loop1:
la $t1,Array#將陣列地址存到$t1中
addi $t6,$zero,0# j($t6)=0
loop2:
lw $t0,0($t1)#temp($t0) = array[j]
lw $t7,4($t1)# t7=arry[j+1]
blt $t0,$t7,skip#當array[j]<array[j+1],跳過
sw $t7,0($t1)# arry[j]=arry[j+1]
sw $t0,4($t1)# arry[j+1]=temp
skip:
addi $t6,$t6,1#j=j+1
addi $t1,$t1,4
blt $t6,$t4,loop2#當j<i,返回迴圈
subi $t4,$t4,1 #i=i-1
bne $t4,$zero,loop1#如果i>0,重新迴圈
la $t1,Array#out put segment
addi $t8,$zero,0
loop3:
lw $a0,0($t1)
li $v0,1
syscall
addi $t1,$t1,4
addi $t8,$t8,1
blt $t8,$t5,loop3
.data
Array:.word 2,4,6,1
.text
main:
addi $t5,$zero,4
addi $t4,$zero,3#i($t4)=3
loop1:
la $t1,Array#將陣列地址存到$t1中
addi $t6,$zero,0# j($t6)=0
loop2:
lw $t0,0($t1)#temp($t0) = array[j]
lw $t7,4($t1)# t7=arry[j+1]
blt $t0,$t7,skip#當array[j]<array[j+1],跳過
sw $t7,0($t1)# arry[j]=arry[j+1]
sw $t0,4($t1)# arry[j+1]=temp
skip:
addi $t6,$t6,1#j=j+1
addi $t1,$t1,4
blt $t6,$t4,loop2#當j<i,返回迴圈
subi $t4,$t4,1 #i=i-1
bne $t4,$zero,loop1#如果i>0,重新迴圈
la $t1,Array#out put segment
addi $t8,$zero,0
loop3:
lw $a0,0($t1)
li $v0,1
syscall
addi $t1,$t1,4
addi $t8,$t8,1
blt $t8,$t5,loop3