Merge Sort MIPS Assembly help with stack pointer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Merge Sort MIPS Assembly help with stack pointer

I am in some need of some desperate help at the moment. I have a merge sort program running in MIPS assembly and I am almost complete. I have a few issues with my pointer, but I cannot seem to find them. If anyone would help that will be much appreciative.
Feel free to ask me if uncertain what is going on in a certain place.
Thanks in advance!
[code]
main:
li $s0, 0
addi $s1, $s1, 19 # $s1=hi
addi $s0, $s0, 0 # $s0=lo

la $a0, list
addi $s2, $s2, 0

addi $sp, $sp, -20
sw $ra, 16($sp)
sw $a0, 12($sp)
sw $s2, 8($sp) # m
sw $s1, 4($sp) # hi
sw $s0, 0($sp) # lo
jal merge_sort

merge_sort:
add $t0, $s0, $s1 # t0 = lo+hi
div $s2, $t0, 2 # m=lo+hi/2

slt $t0, $s0, $s1 # $t0=1 if lohi next loop

la $a0, list # array list into $a0
move $t5, $t2 # save iterator
sll $t5, $t5, 2 # shift by 4
add $t6, $t5, $a0 # $t6=a[i]
lw $t1, 0($t6)

la $a0, b_array
move $t7, $t2
sll $t7, $t7, 2 # shift 4
add $t7, $t7, $a0
sw $t1, 0($t7) # $b[i]=a[i]

addi $t2, $t2, 1

j for


###START OF WHILE LOOP 1####

next:
move $t2, $s0 # i=lo
addi $t3, $s2, 1 # j=m+1 (Hopefully)
move $t4, $s0 # k=lo

while1:
slt $t1, $s2, $t2 #$t1=1;mhi branch to while2

##### getting b[i] & storing into $s3 #######

move $t5, $t2 #preserve i into $t5

la $a0, b_array
sll $t5, $t5, 2
add $t6, $t5, $a0 #load b[i] into $t6
lw $s3, 0($t6) #load content of b[i] into $s3

###Getting element b[j] & storing into $s4########

la $a0, b_array
move $t7, $t3 #preserve j
sll $t7, $t7, 2 #j*4
add $t6 $t7, $a0 #b[j]=$t6
lw $s4, 0($t6) #content of b[j] into $s4

####Compare value of $s3 & $s4#######

if: slt $t1, $s4, $s3 #$t1=1 if b[j]b[j] branch to else

####copy b[i++] into a[k++]

##b[i++]##
la $a0, b_array
move $t5, $t2 # preserve i
sll $t5, $t5, 2 # shift address *4
add $t6, $t5, $a0
lw $t0, 0($t6) # load element into $t0

##a[k++]##
la $a0, list
move $t5, $t4 #preserve k
sll $t5, $t5, 2 #shift by 4
add $t6, $t5, $a0
sw $t0, 0($t6) # a[k++]=b[i++]

addi $t2, $t2, 1 #i++
addi $t4, $t4, 1 #k++
j while1

###else b[j++]--->a[k++]###
else:

##b[j++]##

la $a0, b_array
move $t5, $t3 # preserve j
sll $t5, $t5, 2 # shift 4
add $t6, $t5, $a0 # load into $t6
lw $t0, 0($t6) # $t6-->$t0

##a[k++]

la $a0, list
move $t5, $t4 #preserve k
sll $t5, $t5, 2 #shift 4
add $t6, $t5, $a0 #load into $t6
sw $t0, 0($t6) # $t0-->0($t6)

addi $t3, $t3, 1 #j++
addi $t4, $t4, 1 #k++
j while1

while2:
slt $t1, $s2, $t2 #$t1=1;m<i
bne $t1, $zero, end_merge #ifm<i branch to end_me$

###a[k++]=b[i++]###

la $a0, b_array
move $t5, $t2 # preserve i
sll $t5, $t5, 2 # shift address *4
add $t6, $t5, $a0
lw $t0, 0($t6)

la $a0, list
move $t5, $t4
sll $t5, $t5, 2
add $t6, $t5, $a0
sw $t0, 0($t6) # a[k++]

addi $t2, $t2, 1 #i++
addi $t4, $t4, 1
j while2

end_merge:


j $ra #jump to previous location


[/code]
Sign In or Register to comment.