MIPS气泡排序未排序

时间:2019-03-16 06:05:17

标签: mips bubble-sort

im试图编写一个程序以使用冒泡排序对数组进行排序,但是我的输出始终与输入数字时的顺序相同,它没有对任何内容进行排序。任何关于我错的帮助将不胜感激。它在运行时不会给出任何错误,但也不会进行任何排序,因此我有点像是在砖墙上弄清楚了这一点

  

.data

prompt:     .asciiz "how many numbers will you enter?: "
intPrompt:  .asciiz "please enter a number: "
comma:      .asciiz   ", "
crlf:       .asciiz "\n" 
array:      .word 60
     .text
    .globl main
main:
li $t7,0        #counter for adding numbers to array
la $t0,array        # t0 = address of array
    la $a0,prompt       # load address of prompt into a0
li $v0,4            # load call code number to display a           string into v0
syscall             # system call to print the prompt string
li $v0,5            # load call code number to read an integer -> v0
syscall             # system call to read the integer and store in v0

#saving integer
move $t1,$v0        # move integer from v0 -> t1 to keep as count for loop
while:  
la $a0, intPrompt        #prompt for integer
li $v0,4                 #a0 = address of string
syscall                  #v0 = 4, indicates display a String

li $v0,5                 # enter input -> v0
syscall                  # 5 is sys call for read int

sw $v0, ($t0)

add $t0,$t0,4            # move pointer ahead to next array element
add $t7,$t7,1            # increment counter


blt $t7,$t1,while        # branch to while if counter < size of array

#sort
li $t0,0
la $t0,array
li $t2,0        #initialize i with 0
li $t3,4        #initialize j with 4
add $t4,$t1,-1      #end index for outer loop
mul $t4,$t4,4       #end index for outer loop
mul $t5,$t1,4       #end index for inner loop

OUTER:
ble $t2,$t4,EOUTER
INNER:
ble $t3,$t5,EINNER
add $t6,$t3,-4
lw $s1,0($t0)
lw $s2,4($t0)
bgt$s1,$s2,NOSWAP
sw $s1,0($t0)
sw $s2,4($t0)
NOSWAP:
add $t3,$t3,4
j INNER
EINNER:
add $t2,$t2,4
li $t3,4
j OUTER
EOUTER:


#print array
li $t0, 0           # initilize array index value back to 0
    li $t7, 0           # initial size counter back to zero
    la $t0, array       # load address of array back into $t0

startPrint:

    lw $t2,($t0)        # load word a[i] into temp (t2)
    add $t4, $t4, $t2
    move $a0, $t2       # move a[i] to a0 for display
    li $v0,1            # display a[i]
    syscall

    la $a0,comma        # Display ", "
    li $v0,4            # a0 = address of message
    syscall             # v0 = 4 which indicates display a      string

    add $t0,$t0,4       # move pointer ahead to next array element 
    add $t7, $t7, 1     #increment counter

    blt $t7,$t1,startPrint     # branch to startPrint if counter < size of array

    la $a0,crlf         # Display "cr/lf"
    li $v0,4            # a0 = address of message
    syscall             # v0 = 4 which indicates display a string
    li $v0,10           # End Of Program
    syscall 

0 个答案:

没有答案