Question
I have the following MIPS code that allows user input for 2 arrays and sorts them, but need help merging the two arrays together and
I have the following MIPS code that allows user input for 2 arrays and sorts them, but need help merging the two arrays together and sorting them as one big array!!! Please help!!!!!
.data
array1: .space 40
array2: .space 40
array3: .space 40
msg1: .asciiz "Please enter size of the 1st array: "
msg2: .asciiz "Please enter the elements of the 1st array: "
msg3: .asciiz "First sorted list: "
msg4: .asciiz "Please enter the size of the 2nd array: "
msg5: .asciiz "Please enter the elements of the 2nd array: "
msg6: .asciiz "Second sorted list: "
endl: .asciiz " "
spacebar: .asciiz " "
.text
addi $t9, $zero, 2
pt1: # Initial output for the first array
la $t0, array1 # $t0 holds the array input for the first array
la $a0, msg1 # Prompts the user to enter the size of the first array to be created
li $v0, 4
syscall
li $v0, 5 # Saves user input for size of array
syscall
move $t1, $v0 # Size of array1 is saved under $t1
beqz $t1, exit1 # If the array size is zero, the program exits
la $a0, msg2 # outputs message 2, asking for first array elements
li $v0, 4
syscall
move $t2, $t1 # $t1, $t2, and $t3 will all hold the value for the size of the first array
move $t3, $t1
j loop # Jumps to the loop for the first array to be further developed
pt2: # Initial output for the second array
la $t0, array2 # t0 holds the array input for the second array
la $a0, msg4 # Prompts user to enter size of the second array to be created
li $v0, 4
syscall
li $v0, 5 # Saves user input for size of the array
syscall
move $t1, $v0 # The size of the second array is saved into $t1
beqz $t1, exit2 # If the size of the second array is set to 0, program jumps to the exit
la $a0, msg5 # Prompts user to enter desired elements for the second array
li $v0, 4
syscall
move $t2, $t1 # $t1, $t2, and $t3 will all hold the value for the size of second array
move $t3, $t1
j loop5 # Jumps to the loop for the second array to be further developed, skips "loop"
loop: # Loop for first array to be further developed
li $v0, 5
syscall
sw $v0, ($t0) # The array is saved to $v0
add $t0, $t0, 4 # Adds the next input element of the array
add $t1, $t1, -1 # count = count - 1
bnez $t1, loop # Keeps allowing user input until size of the array is reached, then stops looping
move $t1, $t2 # Sets $t1 back to orignal size of the array
la $t0, array1 # makes a copy of the first array into $t0
la $t4, array1 # makes a second copy of the first array into $t4
move $t6, $t4 # $t6 is initiated with the first element of the array
la $a0, endl # Displays an empty line of space to the user
li $v0, 4
syscall
j sort # Jumps to sort loop to skip loop5
loop5: # Loop for second array to be further developed
li $v0, 5
syscall
sw $v0, ($t0) # The array is saved to $v0
add $t0, $t0, 4 # Adds the next input element of the array
add $t1, $t1, -1 # count = count - 1
bnez $t1, loop5 # Keeps allowing user input until size of the array is reached, then stops looping
move $t1, $t2 # Sets $t1 back to orignal size of the array
la $t0, array2 # Makes a copy of the second array into $t0
la $t4, array2 # makes a second copy of the first array into $t4
move $t6, $t4 # $t6 is initiated with the first element of the array
la $a0, endl # Displays an empty line of space to the user
li $v0, 4
syscall
sort:
add $t2, $t2, -1 # loops until every element in the array is accounted for
beqz $t2, loop1 # When the array is completely sorted in descending order, jumps to the loop to reverse it
addi $t0, $t0, 4 # Set to go through each element within the array
lw $s1, ($t0) # Each element is loaded into $s1
lw $s0, ($t6) # The first element of the array is stored into $s0
blt $s1, $s0, sort # Compares $t6 to each other element in the array
move $t6, $t0 # If the other element is larger than $t6, $t6 is stored with the greater value
bnez $t2, sort # Continues to re-iterate the loop until every element is tested and the array is sorted
loop1:
add $t1, $t1, -1 # Keeps track of the number of loops being made
move $t2, $t1 # Copies value of $t1 into $t2
beqz $t2, done # When the max number of loops are made, jumps to the "done" function to finish up
lw $t7, ($t4) # Loads the descending order sorted array into $t7
lw $t8, ($t6) # Loads the largest element in the array into $t8
sw $t7, ($t6) # Moves the largest element in the array to be the first element in the array
sw $t8, ($t4)
add $t4, $t4, 4 # Moves on to the next element in the array
move $t0, $t4 # Stores next read element into $t0
move $t6, $t4 # The last element viewed is now set as the maximum element in the array
bnez $t2, sort # The loop re-iterates until every element in the array has been accounted for
done:
beq $t9, 1, done2
la $a0, msg3 # Prompts the user that the first array is sorted and is about to be displayed
li $v0, 4
syscall
move $t7, $t4 # Last index of the sorted array is copied into $t7
beqz $t3, exit1
done2:
bne $t9, 1, print
la $a0, msg6 # Prompts the user that the second array is sorted and is about to be displayed
li $v0, 4
syscall
move $t7, $t4 # Last index of the sorted array is copied into $t7
beqz $t3, exit1
j print
print: # Function to print the desired array in reverse order, since it is currently in descending order
lw $a0, ($t4) # The last index of the array is loaded into $a0
add $t4, $t4, -4 # Moves to read the previous element in the array
li $v0, 1 # Displays each element of the array in reverse order
syscall
la $a0, spacebar # Displays a space to the user between each array element
li $v0, 4
syscall
add $t3, $t3, -1 # Count set to monitor the numebr of elements being printed
bnez $t3, print
la $a0, endl # Displays an empty line of space to the user
li $v0, 4
syscall
exit1:
subi $t9, $t9, 1
beqz $t9, exit2
j pt2
exit2:
li $v0,10
syscall
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started