8. The following C function finds the minimum of array and returns its value: int findMinimum(int* array, int length) {
Posted: Mon Jun 06, 2022 11:00 am
8. The following C function finds the minimum of array and returns its value: int findMinimum(int* array, int length) { int min = array[0]; for(int j = 1; j<length; i++){ if (array<min) { min = array: } } return min; } Assume array (base address of the array) and length are stored in $a0 and $a1 and findMinimum function returns min in $v0 register. Also assume caller has stored the return address in $ra register. Code below shows the outline of equivalent MIPS assembly code: findMinimum: END: subi $sp, $sp, 20 # allocate five 32-bit registers (20 bytes) # store $t3 SW $t3, 16($sp) SW $t2, 12($sp) # store $t2 SW $t1, 8($sp) # store $t1 SW $t0, 4($sp) # store $t0 | SW $sQ, Q($sp) # store $s0 Jw $v0, 0($a0) #min = array [0] # j = 1 # terminate loop if j-length # t0 = 1*4 # t1 = &array[0] + 1*4 addi $s0, $zero, 1 beg $50, $a1, END $1$t0, $s0, 2 add $t1, $a0, $t0 Jw $t2, 0($t1) slt_$t3, $t2, $v0 beg $t3, $zero, NEXT addi $v0, $t2, 0 # t2 = array # t3 = array < min # skip if ! (array<min) #min = array [j] # j++ NEXT: addi $50, $s0, 1 j LOOP END: 1w $t3, 16($sp) #restore St3 ไพ $t2, 12($sp) # restore $t2 Jw $t1, 8($sp) # restore $t1 1w $t0, 4($sp) #restore $t0 ไพ $50, 0($sp) # restore $50 addi $sp, $sp, 20 # deallocate stack $ra #jump to caller The code above is using 5 registers to implement findMinimum function: $s0, $t0 to $t3. Modify the code above and minimize register usage. Highlight to code above to show your solution. Hint: best solution is to use 3 registers only. LOOP: