I have the following MIPS code that allows user input for 2 arrays and sorts the
ID: 3702694 • Letter: I
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 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
Explanation / Answer
$LC0: .ascii "Enter the size of first array : " .align 2 $LC1: .ascii "%d " .align 2 $LC2: .ascii "Enter elements in first array : " .align 2 $LC3: .ascii "
Enter the size of second array : " .align 2 $LC4: .ascii "Enter elements in second array : " .align 2 $LC5: .ascii "
Array before merged in ascending order : " .align 2 $LC6: .ascii "%d " .align 2 $LC7: .ascii "
Array merged in ascending order : " .text .align 2 .globl main .set nomips16 .set nomicromips .ent main .type main, @function main: .frame $fp,1664,$31 # vars= 1632, regs= 2/0, args= 16, gp= 8 .mask 0xc0000000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set nomacro addiu $sp,$sp,-1664 sw $31,1660($sp) sw $fp,1656($sp) move $fp,$sp .cprestore 16 movz $31,$31,$0 lw $2,%got($LC0)($28) nop addiu $4,$2,%lo($LC0) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) addiu $2,$fp,1644 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) nop lw $2,%got($LC2)($28) nop addiu $4,$2,%lo($LC2) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) sw $0,36($fp) b $L2 nop $L3: addiu $3,$fp,44 lw $2,36($fp) nop sll $2,$2,2 addu $2,$3,$2 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) lw $2,36($fp) nop addiu $2,$2,1 sw $2,36($fp) $L2: lw $2,1644($fp) lw $3,36($fp) nop slt $2,$3,$2 bne $2,$0,$L3 nop lw $2,%got($LC3)($28) nop addiu $4,$2,%lo($LC3) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) addiu $2,$fp,1648 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) nop lw $2,%got($LC4)($28) nop addiu $4,$2,%lo($LC4) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) sw $0,36($fp) b $L4 nop $L5: addiu $3,$fp,444 lw $2,36($fp) nop sll $2,$2,2 addu $2,$3,$2 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) lw $2,36($fp) nop addiu $2,$2,1 sw $2,36($fp) $L4: lw $2,1648($fp) lw $3,36($fp) nop slt $2,$3,$2 bne $2,$0,$L5 nop lw $2,%got($LC5)($28) nop addiu $4,$2,%lo($LC5) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) sw $0,36($fp) b $L6 nop $L7: lw $2,36($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $2,820($2) nop move $5,$2 lw $2,%got($LC6)($28) nop addiu $4,$2,%lo($LC6) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) lw $2,36($fp) nop addiu $2,$2,1 sw $2,36($fp) $L6: lw $3,36($fp) lw $2,40($fp) nop slt $2,$3,$2 bne $2,$0,$L7 nop lw $3,1644($fp) lw $2,1648($fp) nop addu $2,$3,$2 sw $2,40($fp) sw $0,24($fp) sw $0,28($fp) sw $0,32($fp) b $L8 nop $L12: lw $2,1644($fp) lw $3,24($fp) nop slt $2,$3,$2 beq $2,$0,$L13 nop lw $2,1648($fp) lw $3,28($fp) nop slt $2,$3,$2 beq $2,$0,$L13 nop lw $2,24($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $3,20($2) lw $2,28($fp) nop sll $2,$2,2 addiu $4,$fp,24 addu $2,$4,$2 lw $2,420($2) nop slt $2,$3,$2 beq $2,$0,$L10 nop lw $2,24($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $3,20($2) lw $2,32($fp) nop sll $2,$2,2 addiu $4,$fp,24 addu $2,$4,$2 sw $3,820($2) lw $2,24($fp) nop addiu $2,$2,1 sw $2,24($fp) b $L11 nop $L10: lw $2,28($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $3,420($2) lw $2,32($fp) nop sll $2,$2,2 addiu $4,$fp,24 addu $2,$4,$2 sw $3,820($2) lw $2,28($fp) nop addiu $2,$2,1 sw $2,28($fp) $L11: lw $2,32($fp) nop addiu $2,$2,1 sw $2,32($fp) $L8: lw $3,32($fp) lw $2,40($fp) nop slt $2,$3,$2 bne $2,$0,$L12 nop b $L13 nop $L14: lw $2,24($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $3,20($2) lw $2,32($fp) nop sll $2,$2,2 addiu $4,$fp,24 addu $2,$4,$2 sw $3,820($2) lw $2,32($fp) nop addiu $2,$2,1 sw $2,32($fp) lw $2,24($fp) nop addiu $2,$2,1 sw $2,24($fp) $L13: lw $2,1644($fp) lw $3,24($fp) nop slt $2,$3,$2 bne $2,$0,$L14 nop b $L15 nop $L16: lw $2,28($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $3,420($2) lw $2,32($fp) nop sll $2,$2,2 addiu $4,$fp,24 addu $2,$4,$2 sw $3,820($2) lw $2,32($fp) nop addiu $2,$2,1 sw $2,32($fp) lw $2,28($fp) nop addiu $2,$2,1 sw $2,28($fp) $L15: lw $2,1648($fp) lw $3,28($fp) nop slt $2,$3,$2 bne $2,$0,$L16 nop lw $2,%got($LC7)($28) nop addiu $4,$2,%lo($LC7) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) sw $0,36($fp) b $L17 nop $L18: lw $2,36($fp) nop sll $2,$2,2 addiu $3,$fp,24 addu $2,$3,$2 lw $2,820($2) nop move $5,$2 lw $2,%got($LC6)($28) nop addiu $4,$2,%lo($LC6) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) lw $2,36($fp) nop addiu $2,$2,1 sw $2,36($fp) $L17: lw $3,36($fp) lw $2,40($fp) nop slt $2,$3,$2 bne $2,$0,$L18 nop move $2,$0 move $sp,$fp lw $31,1660($sp) lw $fp,1656($sp) addiu $sp,$sp,1664 j $31 nop