在MIPS排序阵列(组装) [英] sorting array in mips (assembly)
问题描述
即时通讯在使用MIPS类学习汇编。我的工作排序数字数组,我认为,我有办法正常工作,但只是有点麻烦。我不知道如何检查时,即时通讯完全排序。即时通讯使用pretty基本的方法排序,但仅此而已,我们迄今的经验教训。另外,我不知道如何输出的数字来检查,看它是否是排序。 IM用于Java和这样使组件还挺扔我的自旋。这里是我的code迄今:
im in a class learning assembly using mips. I am working on sorting an array of numbers and i think that I have the method working correctly, but just a bit of trouble. I do not know how to check when im sorted fully. Im using a pretty rudimentary method for sorting, but that is all that we have learned thus far. Also, i do not know how to output the numbers to check to see if it is sorted. Im used to Java and such so assembly is kinda throwing me for a spin. Here is my code thus far:
.text
.globl main
main: la $a0, Array # sets the base address of the array to $a0
loop: lw $t0, 0($a0) # sets $t0 to the current element in array
lw $t1, 4($a0) # sets $t1 to the next element in array
blt $t1, $t0, swap # if the following value is greater, swap them
addi $a0, $a0, 4 # advance the array to start at the next location from last time
j loop # jump back to loop so we can compare next two elements
swap: sw $t0, 4($a0) # store the greater numbers contents in the higher position in array (swap)
sw $t1, 0($a0) # store the lesser numbers contents in the lower position in array (swap)
li $a0, 0 # resets the value of $a0 back to zero so we can start from beginning of array
j loop # jump back to the loop so we can go through and find next swap
.data
Array: .word 14, 12, 13, 5, 9, 11, 3, 6, 7, 10, 2, 4, 8, 1
任何帮助的感谢你们!
thanks for any help guys!
推荐答案
此链接说明如何打印到屏幕的MIPS模拟器像<一个href=\"https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC0QFjAA&url=http://spimsimulator.sourceforge.net/&ei=im9XUsnXE5Lb4APHwIHwCg&usg=AFQjCNGN7AI4es3oD5n4Q8pF2rB2CQ-E_Q&sig2=_pvtl8_O4UgbJQNApuuEDg&bvm=bv.53899372,d.dmg\"相对=nofollow> QTSPIM 或<一个href=\"https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCsQFjAA&url=http://courses.missouristate.edu/kenvollmar/mars/&ei=pm9XUu7eEffj4APIhIHoBg&usg=AFQjCNGNWrmeHgY8UzCq6kwpee2tdKLpqw&sig2=iAWY5ctd96PWvs_1-cwMpA&bvm=bv.53899372,d.dmg\"相对=nofollow> MARS 。
This link explains how to print to the screen in a MIPS simulator like QTSPIM or MARS.
对于code,有一些错误。行李$ A0,0
是覆盖由最初的拉$ A0,阵列
指令完成的工作,因为里
代替阵列的基地址设置为0,你应该移动拉
指令进入死循环,使 $ A0
正确重置为阵列
迭代整个数组结束后,并删除<$ C的基址$ C>里指令。您还需要在当你的程序已经完成的排序为条件加入。我建议如下修改(使用SPIM测试):
As for the code, there were a few bugs. The line li $a0, 0
is overwriting the work done by the initial la $a0, Array
instruction because the li
is setting the base address of your array to 0. Instead, you should move the la
instruction into the loop so that $a0
is properly reset to the base address of Array
after iterating over the entire array, and remove the li
instruction. You also will need to add in a condition for when your program has completed the sort. I would suggest the following revisions (tested with SPIM):
main:
la $t0, Array # Copy the base address of your array into $t1
add $t0, $t0, 40 # 4 bytes per int * 10 ints = 40 bytes
outterLoop: # Used to determine when we are done iterating over the Array
add $t1, $0, $0 # $t1 holds a flag to determine when the list is sorted
la $a0, Array # Set $a0 to the base address of the Array
innerLoop: # The inner loop will iterate over the Array checking if a swap is needed
lw $t2, 0($a0) # sets $t0 to the current element in array
lw $t3, 4($a0) # sets $t1 to the next element in array
slt $t5, $t2, $t3 # $t5 = 1 if $t0 < $t1
beq $t5, $0, continue # if $t5 = 1, then swap them
add $t1, $0, 1 # if we need to swap, we need to check the list again
sw $t2, 4($a0) # store the greater numbers contents in the higher position in array (swap)
sw $t3, 0($a0) # store the lesser numbers contents in the lower position in array (swap)
continue:
addi $a0, $a0, 4 # advance the array to start at the next location from last time
bne $a0, $t0, innerLoop # If $a0 != the end of Array, jump back to innerLoop
bne $t1, $0, outterLoop # $t1 = 1, another pass is needed, jump back to outterLoop
请务必检查出此链接为每个MIPS指令做什么额外的例子和说明。
Be sure to check out this link for additional examples and explanations on what each MIPS instruction does.
这篇关于在MIPS排序阵列(组装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!