sorting array in mips (assembly) -
im in class learning assembly using mips. working on sorting array of numbers , think have method working correctly, bit of trouble. not know how check when im sorted fully. im using pretty rudimentary method sorting, have learned far. also, not know how output numbers check see if sorted. im used java , such assembly kinda throwing me spin. here code far:
.text .globl main main: la $a0, array # sets base address of array $a0 loop: lw $t0, 0($a0) # sets $t0 current element in array lw $t1, 4($a0) # sets $t1 next element in array blt $t1, $t0, swap # if following value greater, swap them addi $a0, $a0, 4 # advance array start @ next location last time j loop # jump loop can compare next 2 elements swap: sw $t0, 4($a0) # store greater numbers contents in higher position in array (swap) sw $t1, 0($a0) # store lesser numbers contents in lower position in array (swap) li $a0, 0 # resets value of $a0 0 can start beginning of array j loop # jump loop can go through , find next swap .data array: .word 14, 12, 13, 5, 9, 11, 3, 6, 7, 10, 2, 4, 8, 1
thanks guys!
this link explains how print screen in mips simulator qtspim or mars.
as code, there few bugs. line li $a0, 0
overwriting work done initial la $a0, array
instruction because li
setting base address of array 0. instead, should move la
instruction loop $a0
reset base address of array
after iterating on entire array, , remove li
instruction. need add in condition when program has completed sort. suggest following revisions (tested spim):
main: la $t0, array # copy base address of array $t1 add $t0, $t0, 40 # 4 bytes per int * 10 ints = 40 bytes outterloop: # used determine when done iterating on array add $t1, $0, $0 # $t1 holds flag determine when list sorted la $a0, array # set $a0 base address of array innerloop: # inner loop iterate on array checking if swap needed lw $t2, 0($a0) # sets $t0 current element in array lw $t3, 4($a0) # sets $t1 next element in array slt $t5, $t2, $t3 # $t5 = 1 if $t0 < $t1 beq $t5, $0, continue # if $t5 = 1, swap them add $t1, $0, 1 # if need swap, need check list again sw $t2, 4($a0) # store greater numbers contents in higher position in array (swap) sw $t3, 0($a0) # store lesser numbers contents in lower position in array (swap) continue: addi $a0, $a0, 4 # advance array start @ next location last time bne $a0, $t0, innerloop # if $a0 != end of array, jump innerloop bne $t1, $0, outterloop # $t1 = 1, pass needed, jump outterloop
be sure check out this link additional examples , explanations on each mips instruction does.
Comments
Post a Comment