d. t pointsl The following C function finds the maximum of array and returns its
ID: 3906119 • Letter: D
Question
d. t pointsl The following C function finds the maximum of array and returns its value int findMaximumCint* array, int length) int max array[0]; for( int í = 1; imax) max array[i]: ength: i++){ return max; Assume array (base address of the array) and length are stored in Sa0 and $al and findMaximum function returns max in $v0 register. Also assume caller has stored the return address in Sra register. Translate this function to MIPS assembly code. Note: Make sure your code is not overriding any register, unless you store a backup on stack (starting at $sp) and restore at the end.Explanation / Answer
Give below is the MIPS equivalent code. Please do rate if it helped. Thank you
findMaximum:
#save registers on stack
sub $sp, $sp, 12 #allocate space to store 3 registers s0 s1 and s2
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
li $s0, 1 #counter i
lw $s1, 0($a0) #max = array[0]
loop:
bgt $s0, $a1, end_loop #if i > length, goto end_loop
lw $s2, 0($a0) #get current array element
ble $s2, $s1, next #array[i] <= max
move $s1, $s2 #update max
next:
add $a0, $a0, 4 #next array location
add $s0, $s0, 1 #i++
b loop
end_loop:
move $v0, $s1 #store the max in v0 , return value
#restore registers
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
add $sp, $sp, 12
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.