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.
Asked
Active
Viewed 143 times
-1
-
What line is it crashing on? – Oliver Charlesworth May 05 '14 at 22:57
-
I believe it is the lw $t5, 0($a0) in the loop label. The main thing that is throwing me off is that how could it work for an array of size 2 but not greater? – SoftwareDev1 May 05 '14 at 23:37
1 Answers
2
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
.

Michael
- 57,169
- 9
- 80
- 125
-
Thank you so much! So since I wasn't going back to the original start address of the array, I was trying to access a spot in the array which doesn't exist? Would my code have been correct if I used the same starting address each time? – SoftwareDev1 May 06 '14 at 15:29
-
Just changed the add $a0, $a0, $t1 to add $t6, $a0, $t1 to preserve the $a0 starting address of the array and it works! Thanks again. – SoftwareDev1 May 06 '14 at 15:47