使用MIPS排序程序

时间:2014-09-25 01:56:53

标签: sorting mips

当我在MIPS中编写排序程序时,我遇到了一些问题。

这个程序的功能是这样的:从键盘输入10个整数并打印出来,然后对它们进行排序并再次打印排序的数组。

但输出与我的预期不同,有些数字没有排序,有人会帮我吗?

.data
str1: .asciiz "Please input integer numbers, maximum 10: \n"
str2: .asciiz "The 10 integer numbers you input are: \n"
str3: .asciiz "The numbers you entered are sorted as: \n"
array: .space 40
space: .asciiz " "

.text
.globl main

main:
la  $t6, array          #load the address of array into $t6
move    $t7, $t6
addi    $t7, $t7, 40            #point $t7 to the end of the array
jal readin

la  $t6, array
la  $a0, str2
li  $v0, 4
syscall
jal print1              #print out the array before sort

la  $t0, array          #put the address of array into $t0
add $t0, $t0, 40            #put $t0 to the end of the array
move    $t1, $zero          #set $t1 as counter of the outerloop
li  $t2, 10             #set $t2 as number of the outerloop
la  $t3, array          #put the address to $t3
jal innerloop

la  $a0, str3
li  $v0, 4
syscall

la  $t3, array
#move   $t7, $t3
#add    $t7, $t7, 40
jal print2

li  $v0, 10
syscall

innerloop:                  
lb  $t4, 0($t3)
lb  $t5, 4($t3)
bgt $t4, $t5, swap
addi    $t3, $t3, 4         
blt $t3, $t0, innerloop     #if $t3 < $t0, keep in the innerloop to check two                           #neighbor numbers
jal outerloop           #jump to outerloop if one round is finished

swap:
sb  $t4, 4($t3)
sb  $t5, 0($t3)
addi    $t3, $t3, 4
#sll    $t3, $t3, 2
blt $t3, $t0, innerloop     #
jr  $ra

outerloop:
la  $t3, array
addi    $t1, $t1, 1         #add the outerloop counter $t1 by one, 
addi    $t0, $t0, -1
blt $t1, $t2, innerloop     #if $t1 < $t2, keep searching 
jr  $ra

readin:
la  $a0, str1           #print str1
li  $v0, 4
syscall

li  $v0, 5              #read in the number
syscall

sb  $v0, ($t6)          #store the number in the array
add $t6, $t6, 4
blt $t6, $t7, readin
jr  $ra

print1: 
lb  $a0, ($t6)
li  $v0, 1
syscall

li  $a0, 32
li  $v0, 11             # syscall number for printing character
syscall

add $t6, $t6, 4
blt $t6, $t7, print1
jr  $ra

print2: 
lb  $a0, ($t3)
li  $v0, 1
syscall

li  $a0, 32
li  $v0, 11             # syscall number for printing character
syscall

add $t3, $t3, 4
blt $t3, $t0, print2
jr  $ra

SPIM的结果是这样的:

Please input integer numbers, maximum 10: 
44
Please input integer numbers, maximum 10: 
11
Please input integer numbers, maximum 10: 
33
Please input integer numbers, maximum 10: 
22
Please input integer numbers, maximum 10: 
66
Please input integer numbers, maximum 10: 
55
Please input integer numbers, maximum 10: 
99
Please input integer numbers, maximum 10: 
77
Please input integer numbers, maximum 10: 
88
Please input integer numbers, maximum 10: 
24
The 10 integer numbers you input are: 
44 11 33 22 66 55 99 77 88 24 The numbers you entered are sorted as: 
11 33 22 44 55 66 77 88 24 32   

有人请向我解释为什么最后有一个32以及为什么它们没有正确分类?

0 个答案:

没有答案