Related questions
This is my MIPS Assembly Program: its supposed to define two integer arrays 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 sizes as inputs and makes 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.
.data
array1: .word 3, 7, 9, 11, 15, 21 # First sorted array
array2: .word 1, 4, 6, 14, 18, 19 # Second sorted array
size: .word 6 # Size of each array
merged: .space 48 # Space for merged array (2 * 6 * 4 bytes)
output: .asciiz "Merged array: "
comma: .asciiz ", "
.text
.globl main
main:
# Load size of the arrays
la $t0, size
lw $t1, 0($t0) # $t1 = size of each array (6)
# Call merge function
la $a0, array1 # $a0 = first array
la $a1, array2 # $a1 = second array
la $a2, merged # $a2 = merged array
move $a3, $t1 # $a3 = size of arrays
jal merge # Call merge function
# Display merged array
li $v0, 4 # Display string syscall
la $a0, output # Load address of output string
syscall
li $t2, 0 # Index for merged array
display_loop:
lw $a0, 0($a2) # Load value from merged array
li $v0, 1 # Display integer syscall
syscall
addi $t2, $t2, 1 # Increment index
addi $a2, $a2, 4 # Move to the next element in merged array
# Check if it's the last element to avoid trailing comma
bge $t2, $a3, end_display
li $v0, 4 # Display string syscall
la $a0, comma # Load address of comma string
syscall
j display_loop # Continue displaying
end_display:
# Display a newline
li $v0, 11 # Display char syscall
li $a0, 10 # ASCII newline
syscall
li $v0, 10 # Exit syscall
syscall
merge:
# Inputs: $a0 = array1, $a1 = array2, $a2 = merged, $a3 = size
move $t0, $zero # Index for array1
move $t1, $zero # Index for array2
move $t2, $zero # Index for merged array
merge_loop:
# Check if we've finished merging
bge $t0, $a3, copy_array2
bge $t1, $a3, copy_array1
# Load elements from both arrays
lw $t4, 0($a0) # $t4 = array1[$t0]
lw $t5, 0($a1) # $t5 = array2[$t1]
# Compare and merge
blt $t4, $t5, insert_array1
j insert_array2
insert_array1:
sw $t4, 0($a2) # merged[$t2] = array1[$t0]
addi $t0, $t0, 1 # Increment index for array1
j increment_index
insert_array2:
sw $t5, 0($a2) # merged[$t2] = array2[$t1]
addi $t1, $t1, 1 # Increment index for array2
increment_index:
addi $t2, $t2, 1 # Increment index for merged array
addi $a2, $a2, 4 # Move to the next position in merged array
j merge_loop
copy_array1:
# Copy remaining elements from array1
bge $t0, $a3, merge_end # If finished, jump to end
lw $t4, 0($a0) # Load from array1
sw $t4, 0($a2) # Store in merged
addi $t0, $t0, 1 # Increment array1 index
addi $t2, $t2, 1 # Increment merged index
addi $a2, $a2, 4 # Move to the next position in merged array
j copy_array1
copy_array2:
# Copy remaining elements from array2
bge $t1, $a3, merge_end # If finished, jump to end
lw $t5, 0($a1) # Load from array2
sw $t5, 0($a2) # Store in merged
addi $t1, $t1, 1 # Increment array2 index
addi $t2, $t2, 1 # Increment merged index
addi $a2, $a2, 4 # Move to the next position in merged array
j copy_array2
merge_end:
jr $ra # Return from merge
#end of program
===========================================================================================
However I am getting this output "Merged array: 1735550285, 1629512805, 2036429426, 738205754, 32, 0"
which is wrong
please help debug
to generate a solution
a solution
- (Practice) Write a C++ program that adds equivalent elements of the two-dimensional arrays named first and second. Both arrays should have two rows and three columns. For example, element [1][2] of the resulting array should be the sum of first [1][2]andsecond[1][2]. The first and second arrays should be initialized as follows: first second 16 18 23 24 52 77 54 9111 16 19 59arrow_forward(Practice) a. Write a C++ program that adds the values of all elements in the val array used in Exercise 2 and displays the total. b. Modify the program written for Exercise 3a to display the total of each row separately.arrow_forward(Program) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters, such as zerocount, onecount, twocount, and so forth, to 0. Then generate a large number of pseudorandom integers between 0 and 9. Each time 0 occurs, increment zerocount; when 1 occurs, increment onecount; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of time they occurred.arrow_forward
- (Practice) State whether the following are valid function names and if so, whether they’re mnemonic names that convey some idea of the function’s purpose. If they are invalid names, state why. powerdensity m1234 newamp 1234 abcd total tangent absval computed b34a 34ab volts$ a2B3 while minVal sine $sine cosine speed netdistance sum return stackarrow_forward(Practice) Write array declarations, including initializers, for the following: a. A list of 10 integer voltages: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82 b. A list of five double-precision slopes: 11.62, 13.98, 18.45, 12.68, and 14.76 c. A list of 100 double-precision distances; the first six distances are 6.29, 6.95, 7.25, 7.35, 7.40, and 7.42 d. A list of 64 double-precision temperatures; the first 10 temperatures are 78.2, 69.6, 68.5, 83.9, 55.4, 67.0, 49.8, 58.3, 62.5, and 71.6 e. A list of 15 character codes; the first seven codes are f, j, m, q, t, w, and zarrow_forwardMark the following statements as true or false. A double type is an example of a simple data type. (1) A one-dimensional array is an example of a structured data type. (1) The size of an array is determined at compile time. (1,6) Given the declaration: int list[10]; the statement: list[5] - list[3] * list[2]; updates the content of the fifth component of the array list. (2) If an array index goes out of bounds, the program always terminates in an error. (3) The only aggregate operations allowable on int arrays are the increment and decrement operations. (5) Arrays can be passed as parameters to a function either by value or by reference. (6) A function can return a value of type array. (6) In C++, some aggregate operations are allowed for strings. (11,12,13) The declaration: char name [16] = "John K. Miller"; declares name to be an array of 15 characters because the string "John K. Miller" has only 14 characters. (11) The declaration: char str = "Sunny Day"; declares str to be a string of an unspecified length. (11) As parameters, two-dimensional arrays are passed either by value or by reference. (15,16)arrow_forward
- (Data processing) The answers to a true-false test are as follows: T T F F T. Given a twodimensional answer array, in which each row corresponds to the answers provided on one test, write a function that accepts the two-dimensional array and number of tests as parameters and returns a one-dimensional array containing the grades for each test. (Each question is worth 5 points, so the maximum possible grade is 25.) Test your function with the following data:arrow_forward4. Repeat Programming Exercise 3 by declaring numl, num2, and num3, and average of type double. Store 75.35 into numl, -35.56 into num2, and 15.76 into num3.arrow_forward
- Text book imageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrText book imageSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningText book imageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- Text book imageNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningText book imageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTText book imageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,