【问题标题】:Bubble sort using MIPS by user input通过用户输入使用 MIPS 进行冒泡排序
【发布时间】:2019-10-16 18:54:59
【问题描述】:

我对 MIPS 很陌生。 好的,所以我的整个程序是,关于用户输入 10 个整数,然后我们将其存储在一个数组中,然后我们计算最小值和最大值,然后打印出未排序的数组。然后我们开始我现在正在苦苦挣扎的冒泡排序算法,我不会发布我的整个程序,因为它很多,我只会发布我的冒泡排序算法。

这是我对算法的思考过程,所以首先我初始化我的计数器和数组的大小。 然后我开始我的外部循环,如果 i == size 则分支到 endouter, 然后是内部循环,如果 j == size 则分支到 endinner, 然后获取我比较的两个索引, 然后比较,如果我的第一个索引大于第二个索引,则交换。由于某种原因,我遇到了无限循环的异常,有人帮忙吗?

#BubbleSort
    li $t2, 0               #outer counter = 0 i
    li $t1, 9               #size of array 0-9
    li $t8, 1               #inner counter j


outer:  beq $t2,$t1, endouter       #branch to endouter if i < 0

inner:  beq $t8,$t1, endinner   #branch to endinner if j < size
    li $t9, 1
    sub $t9,$t8, $t9        #j - 1
    lw $t4,array($t9)       #load (j-1) into temp
    lw $t5,array($t8)       #load j into temp

    blt $t4,$t5, noswap     #if (j-1) < j branch to no swap
    lw $t4,array($t8)       #swithc (j-1) with j
    lw $t5,array($t9)       #swithc (j-1) with j


 noswap:    add $t8,$t8, 1          #increment j
    j inner

 endinner:
    add $t2,$t2,1
    li $t8, 1
    j outer
 endouter:

【问题讨论】:

  • 无休止的执行很可能是因为您的代码的其他一些部分被您忽略了。使用 SPIM 或 MARS 等模拟器中的调试功能找出问题所在。

标签: mips


【解决方案1】:

试试这个:

li $t0, 0               #outer counter = 0 i
li $t1, 40 #9               #size of array 10 * 4 bytes
li $t8, 40             #inner counter j


outer:  blt $t0,$zero, endouter       #branch to endouter if i < 0

inner_j:
subiu $t8,$t8,4
li $t0,0
beq $t8,$t0,endouter   #branch to endouter if j == i
inner_i:


        lw $t4,array($t0)       #load i into temp
        lw $t5,array($t8)       #load j-1 into temp

        blt $t4,$t5, noswap     #if (j-1) < j branch to no swap

    sw $t4,array($t8)       # swap 
    sw $t5,array($t0)       # swap
    addiu $t0,$t0,4
    beq $t0,$t8,inner_j
        j inner_i
 noswap:    
 addiu $t0,$t0, 4          #increment j
 beq $t0,$t8,inner_j
 j inner_i

 endouter:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2016-02-09
    • 1970-01-01
    • 2014-03-14
    相关资源
    最近更新 更多