开发者

Merge sort with arrays (in MIPS assembler)

The purpose of the program is to take an even number of user inputed integers and sort them (using merge sort). It prints the sorted list to the screen. The program first takes the set size, then asks for the integers. It splits them into two even separate sets, and from here sorts them in ascending order. When both sets are sorted, the program will take merge both sets together in a final sorted set. The final set is then printed to the screen. Output I'm getting is just a list of 0's. Not sure where my error is, could use some fresh eyes. Thanks.

    .data
msg1:
    .asciiz "Please Enter the the total even number of integers in the list: "
    .align 2
msg2:
    .asciiz "\nPlease Enter an integer: "
    .align 2
msg3:
    .asciiz "\nSorted list of integers is: "
    .align 2
coma:
    .asciiz ", "
    .align 2
set1:
    .space 69
    .align 2
set2:
    .space 69
    .align 2
set3:
    .space 138
    .align 2

    .text
    .globl main
main:
#Get input for size of sortedSet, divide it in half to get size of two
#final sorted sets to merge and store them into arguements
    li $v0, 4
    la $a0, msg1                #prints message1 to screen
    syscall
    li $v0, 5
    syscall                 #inputed value is number of elements
    move $s3, $v0
    beqz $s3, main              #check if inputed value is greater than zero
    li $t2, 2
    div $s3, $t2                #divides size of final array by 2
    mflo $s4                #stores quotient as size of smaller arrays

Enter:
#Get input of each integer in the list, after each entered integer itterator is increased by 1.
#input will be saved into set1,when size is equal set1size input will be saved into set2.
    beq $t4, $s4, Enter2            #branch to set2 when set1 is full
    li $v0, 4
    la $a0, msg2
    syscall                 #print message2 to screen, taking input for set1
    li $v0, 5
    syscall                 #takes inputed user value
    move $a1, $v0               
    la $a0, set1
    move $t2, $t4
    sll $t2, $t2, 2             #times itterator by 4
    add $t1, $t2, $a0
    sw $a1, ($t1)               #stores user value as current element of set1
    addi $t4, $t4, 1            #$t4 is itterator for entering loop. Increase by 1 to move to next space in set1
    j Enter
Enter2:
    beq $t5, $s4, step          #branch to step after both sets are full
    li $v0, 4
    la $a0, msg2
    syscall                 #print message2 to screen, taking input for set2
    li $v0, 5
    syscall                 #takes inputed user value
    move $a1, $v0
    la $a0, set2
    move $t2, $t5
    sll $t2, $t2, 2             #times itterator by 4
    add $t1, $t2, $a0
    sw $a1, ($t1)               #stores user value as current element of set2
    addi $t5, $t5, 1            #$t5 is itterator for second entering loop. Increase by 1 to move to next spot in set2
    j Enter2

step:
#resets itterators for safety/to use again
    li $t4, 0
    li $t5, 0
sort:
#sort each individual list to create two sorted list in ascending order for the final
# merge-sort with two lists.
#first set
    beq $t4, $s4, next          #branch to next (second set) when itterator is equal to size of set1
    la $a0, set1                
    move $t2, $t4               #move current spot into $t2 to preserve itterator
    sll $t2, $t2, 2             # times by 4
    add $t1, $t2, $a0           #get address of first element
    lw $s1, ($t1)               #load first element into $s1
    move $t2, $t4               
    addi $t2, $t2, 1            #add 1 to itterator to get next element in set
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s2, ($t1)               #load next element in set to $s2
    bge $s1, $s2, switch            #if set1[$s1] >= set1[$s2] goto label switch
    addi $t4, $t4, 1            #add 1 to itterator
    j sort
switch:
    la $a0, set1                #switch set1[$s1] and set1[$s1+1]
    move $t2, $t4
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s2, ($t1)               #store $s2 into the $s1 spot
    move $t2, $t4               
    addi $t2, $t2, 1                
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s1, ($t1)               #store $s1 into the $s2 spot
    addi $t4, $t4, 1            #add 1 to itterator
    j sort

next:
    beq $t5, $s4, merge         #branch to merge when itterator equals set2 size
    la $a0, set2                #load address of set2
    move $t2, $t5               #move itterator to $t2 to preserve it
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s1, ($t1)               #load element into $s1
    move $t2, $t5
    addi $t2, $t2, 1            #add 1 to itterator for next element
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s2, ($t1)               #load next element into $s2
    bge $s1, $s2, switch2    开发者_JAVA百科       #if set2[$t5] >= set2[$t5+1] goto label switch2
    addi $t5, $t5, 1            #add 1 to itterator
    j next
switch2:
    la $a0, set2                #switch set2[$s2] and set2[$s2+1]
    move $t2, $t5               
    addi $t2, $t2, 1        
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s1, ($t1)               #store $s1 into $s2 spot
    move $t2, $t5
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s2, ($t1)               #store $s2 into $s1 spot
    addi $t5, $t5, 1            #add 1 to itterator
    j next

merge:
#resets itterators for safety/to use again
    li $t4, 0               # = set1 itterator
    li $t5, 0               # = set2 itterator
    li $t7, 0               # = set3 itterator
while:
#while($s1 <= $s4 || $s2 <= $s4) do{
# if($s1 == $s4){ set3[$s3] = set2[$s2]; addi $s2, 1; addi $s3, 1;
#   j while}
# if($s2 == $s4){ set3[$s3] = set1[$s1]; addi $s1, 1; addi $s3, 1;
#   j while}
# if(set1[$s1] > set2[$s2]){ set3[$s3] = set1[$s1]; addi $s1, 1;}
# else{ set3[$s3] = set2[$s2]; addi $s2, 1;}
# addi $s3, 1;
#} return 
# break

    beq $t4, $s4, print         #1st part of while conditional, branch to print if $t4 = $s4
here:
    beq $t4, $s4, if1           #1st if statement, branch to if1 loop if $t4 = $s4
    beq $t5, $s4, if2           #2nd if statement, branch to if2 loop if %t5 = $s4

    la $a0, set1                #3rd if statement
    move $t2, $t4
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s1, ($t1)               #load first element from set1 into $s1
    la $a0, set2
    move $t2, $t5
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s2, ($t1)               #load first element from set2 into $s2
    slt $t6, $s2, $s1           #returns 1 if $s1 > $s2
    bnez $t6, if3               #if $t6 =/= 0, branch to if3 loop
    beqz $t6, else3             #if $t6 = 0, branch to else3 loop

if1:
#branch for $s1 == $s4
# set3[$t7] = set2[$t4];
    la $a0, set2                #load set2 address
    move $t2, $t4
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s1, ($t1)               #load element at $t4 in set2 to $s1
    la $a0, set3                #load set3
    move $t2, $t7
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s1, ($t1)               #store $s1 into $t7 position in set3
    addi $t4, $t4, 1            #add 1 to $t4 itterator
    addi $t7, $t7, 1            #add 1 to $t7 itterator
    j while


if2:
#branch for $s2 == $s4
# set3[$t7] = set1[$t4];
    la $a0, set1                #load set1 address
    move $t2, $t4               #load itterator into $t2
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s1, ($t1)               #load element in position $t4 into $s1
    la $a0, set3                #load set3 address
    move $t2, $t7
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s1, ($t1)               #store $s1 into position $t7 in set3
    addi $t4, $t4, 1            #move to next position in set1
    addi $t7, $t7, 1            #move to next position in set3
    j while

if3:
#branch for set1[$t4] > set2[$s2]
# set3[$t7] = set1[$t4];
    la $a0, set1                #load set1 address
    move $t2, $t4               #move set1 itterator
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s1, ($t1)               #load element at $t4 of set1 into $s1
    la $a0, set3                #load set3 address
    move $t2, $t7               #load set3 itterator
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s1, ($t1)               #store $s1 into set3 at position $t7
    addi $t4, $t4, 1            #move to next position in set1
    addi $t7, $t7, 1            #move to next position in set3
    j while

else3:
#branch for set1[$s1] < set2[$t5]
# set3[$t7] = set2[$t5];
    la $a0, set2                #load set2 address
    move $t2, $t5               #move set2 itterator to $t2
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    lw $s2, ($t1)               #load element in position $t5 of set2 into $s2
    la $a0, set3                #load set3 address
    move $t2, $t7               #move set3 itterator
    sll $t2, $t2, 2
    add $t1, $t2, $a0
    sw $s2, ($t1)               #store element $s2 into position $t7 in set3
    addi $t5, $t5, 1            #move to next position in set2
    addi $t7, $t7, 1            #move to next position in set3
    j while

print:
#itterate through sortedSet and print it to console then exit
    blt $t5, $s4, here          #go to label here if 2nd part of while conditional is not met
    li $v0, 4   
    la $a0, msg3                #load msg3
    syscall                 #print to screen
    la $t3, set3                #load addresss of set3
    move $t2, $s3               #move set3 size into $t2
    li $t7, 0               #load 0 into set3 itterator
print2:
    sll $t7, $t7, 2
    add $t1, $t7, $t3
    li $v0, 1
    lw $a0, ($t3)               #load element at position $t2 of set3 into $a0
    syscall                 #print to screen
    li $v0, 4
    la $a0, coma                #load address of coma
    syscall                 #print screen
    addi $t7, $t7, 1            #move to next element in set3
    addi $t2, $t2, -1           #subtract 1 from set3 size
    bnez $t2, print2            #if set3 size =/= 0, branch to print2

    li $v0, 10
    syscall                 #exit program


Hey I just had the same problem with my code but I figured it out, mines had to do with me loading the array spacing address before I print. I see you do load the set3 again but maybe the problem is with your actual storing the values in register or loading the correct register

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜