【发布时间】: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