Question

Hello I am getting exception 7 [bad data address] for the following findMin number in an array function. The length of the array is already in $a1, and the address of the start of the array is in $a0. I want to have the min in $v0 after all is said and done. My function works for arrays with 2 elements but shoots out exception 7 errors when using bigger sized arrays. I am offsetting the array by adding to the address instead of using something like the 4 in 4($a0) offsetting.

Was it helpful?

Solution

They key here is add $a0, $a0, $t1. Consider what that will do in the case of an array with N elements: on the second iteration you'll add 1*4, on the third iteration 2*4, and so on. So on the third iteration you'd be trying to read from array + 1*4 + 2*4 == array + 3*4 instead of array + 2*4. And on the N:th iteration you'd be trying to read from array + 1*4 + 2*4 + ... + (N-1)*4.

The sll + add before the lw should be removed. Updating the address can be done after the lw with an addiu $a0, $a0, 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top