Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

skeleton code: ### We need to allocate memory to store the original array ### Later on, we should check this array is really reversed la

image text in transcribed

skeleton code:

### We need to allocate memory to store the original array ### Later on, we should check this array is really reversed la $t0, length sw $s1, 0($t0) sll $s2, $s1, 2 li $v0, 9 #system call code for allocating memory add $a0, $zero, $s2 #set a0 to be number of bytes requested syscall la $t1, addr1 sw $v0, 0($t1) #store the allocated address to addr add $t0, $zero, $zero add $t2, $zero, $s1 la $a0, list loop1: addi $t2, $t2, -1 sll $t3, $t0, 2 add $t3, $t3, $a0 sll $t4, $t2, 2 add $t4, $t4, $v0 lw $t5, 0($t3) sw $t5, 0($t4) addi $t0, $t0, 1 bne $t2, $zero, loop1 ### We want to allocated memory from heap to save the registers ### We need 36 bytes since we need to save 8 saved registers and also $sp li $v0, 9 #system call code for allocating memory addi $a0, $zero, 36 #set a0 to be number of bytes requested syscall add $t0, $v0, $zero #save the allocated address to $t0 la $t1, addr sw $t0, 0($t1) #store the allocated address to addr sw $s0, 0($t0) sw $s1, 4($t0) sw $s2, 8($t0) sw $s3, 12($t0) sw $s4, 16($t0) sw $s5, 20($t0) sw $s6, 24($t0) sw $s7, 28($t0) sw $sp, 32($t0) add $t1, $zero, $zero la $a0, list add $a1, $t1, $zero addi $a2, $s1, -1 jal reverse ### Here we need to check whether the results are correct la $t1, addr lw $t0, 0($t1) lw $t2, 0($t0) bne $s0, $t2, incorrect lw $t2, 4($t0) bne $s1, $t2, incorrect lw $t2, 8($t0) bne $s2, $t2, incorrect lw $t2, 12($t0) bne $s3, $t2, incorrect lw $t2, 16($t0) bne $s4, $t2, incorrect lw $t2, 20($t0) bne $s5, $t2, incorrect lw $t2, 24($t0) bne $s6, $t2, incorrect lw $t2, 28($t0) bne $s7, $t2, incorrect lw $t2, 32($t0) bne $sp, $t2, incorrect ### Now we check whether the array is really sorted la $t0, length lw $s1, 0($t0) la $a0, list la $t0, addr1 lw $s0, 0($t0) loop2: addi $s1, $s1, -1 sll $t1, $s1, 2 add $t2, $a0, $t1 add $t3, $s0, $t1 lw $t4, 0($t2) lw $t5, 0($t3) bne $t4, $t5, incorrect bne $s1, $zero, loop2 li $v0, 4 #system call code for Print String la $a0, pass #load address of pass into $a0 syscall li $v0, 10 syscall incorrect: li $v0, 4 #system call code for Print String la $a0, wrong #load address of wrong into $a0 syscall li $v0, 10 syscall ### fill in your code below reverse: # procedure to reverse an array, $a0: address of the array, $a1: i, $a2: j jr $ra
9. (20 points) Translate the following recursive procedure to MIPS instructions. void reverse(int array[], int i, int j) int temp; if(i >j) return; temp- arrayi]; array[il - arrayil: arrayoil temp; reverse(array, i+ 1, -1); Note load arraylil to $s0, and load arraylil to $s1 in your code. Moreover, the 3 arguments for the procedure are in $a0, $a1 and $a2 respectively

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

Solve each equation in Exercises 114 by factoring. 4x 13x = -3

Answered: 1 week ago