Question

Write a mips program that defines two integer array that are pre-sorted and the same size...

Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6, 7, 9, 11, 14, 15, 18, 19, 21]. Have your 'main' function (not the merge function) print out the final array using a comma as a delimiter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#Data declartion . data arr1: .word 3 7 9 11 15 21 #Test array arr2: .word 1 4 6 14 18 19 #Test arraya arr3: .word 0 0 0 0 0

Program

#Data declartion
.data
    arr1: .word 3 7 9 11 15 21            #Test array1
    arr2: .word 1 4 6 14 18 19            #Test array2
    arr3: .word 0 0 0 0 0 0 0 0 0 0 0 0   #Result array
#Main
.globl main
.text
main:
    #Get address of arrays and size
    la $a0,arr1
    la $a1,arr2
    li $a2,6
    la $a3,arr3
    #CAll merge function
    jal merge
    #Set size of new array
    mul $a2,$a2,2
    #Call print function to display array
    jal print
    #End of the program
    li $v0,10
    syscall
#Merge functionimplementation
merge:
   #Set passed arguments as temporary
    move $t0,$a0
    move $t1,$a1
    move $t2,$a2
    move $t3,$a3
#Loop to combine 2 arrays into new array
mergeLoop:
    beqz $t2,sort
    #First array read and write into new array
    lw $t4,($t0)
    sw $t4,($t3)
    addi $t3,$t3,4
    #Second array read and write into new array
    lw $t4,($t1)
    sw $t4,($t3)
    #Increment array addresses
    addi $t3,$t3,4
    addi $t0,$t0,4
    addi $t1,$t1,4
    #Decrement loop index
    addi $t2,$t2,-1
    j mergeLoop
#Sort the merged array into ascending order
sort:
   move $t3,$a2
   mul $t3,$t3,2
   move $t1,$t3
sortArray:
li $t0,0                                  #Set counter for array bound                         
loop1:                                    #for (i = 0; i < n-1; i++)
addi $t0,$t0,1                            #Increment t0 for j value                
bgt $t0,$t3,end                           #check array bound reach          
add $t1,$t3,$zero                        #If not set t1=a1=array size             
loop2:                                  #for (j = 0; j < n-i-1; j++)
bge $t0,$t1,loop1                       #Check each round completed         
subi $t1,$t1,1                          #Subtract t1 to reduce count to reach size of the array 0             
sll $t4, $t1, 2                        #Shift left to 2              
subi $t6, $t4, 4                       #subtact t4 with 4 and store into t3                                    
add $t4,$t4,$a3                       #Get address of the array                
add $t6,$t6,$a3                       #Get address of the array               
lw $t5,0($t4)                         #Load values into t5
lw $t7,0($t6)                          #Load values into t6
#It's loop to swap values small into forward and large backward
swap:
bgt $t5,$t7,loop2                 
sw $t5,0($t6)                     
sw $t7,0($t4)
j loop2
#return from the sort to main
end:
jr $ra

#Function to print the array elements as coma separated
print:
    #Check array reach n-1 of size
    beq $a2,1,ret
   #Get and display list data
   lw $a0,0($a3)                     
   li $v0,1
   syscall
   #Display coma
   li $a0,44
   li $v0,11
   syscall
   #Get next index
   addi $a3,$a3,4                    
   addi $a2,$a2,-1
   j print                                       
ret:
#Display last element
   lw $a0,0($a3)                     
   li $v0,1
   syscall
   #Return to main
   jr $ra

Output

1,3,4,6,7,9,11,14,15,18,19,21

Add a comment
Know the answer?
Add Answer to:
Write a mips program that defines two integer array that are pre-sorted and the same size...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT