exceptionassemblymipsqtspim

MIPS Exception 6 [Bad instruction address] for Newton's Method of finding Square Roots


I'm trying to implement Newton's Method for calculating integer square roots of a given k ($a0 here). I get the correct output but it is followed by an infinite series of Exception 6 errors like the following:

sqrt(32) = 5
sqrt(33) = 5
sqrt(34) = 5
sqrt(35) = 6
sqrt(36) = 6
sqrt(37) = 6
Exception occurred at PC=0x00000024
  Bad address in text read: 0x00000025
  Exception 6  [Bad instruction address]  occurred and ignored
Exception occurred at PC=0x00000028
  Bad address in text read: 0x00000028
  Exception 6  [Bad instruction address]  occurred and ignored
Exception occurred at PC=0x0000002c
  Bad address in text read: 0x0000002c
  Exception 6  [Bad instruction address]  occurred and ignored
Exception occurred at PC=0x00000030
  Bad address in text read: 0x00000030
  Exception 6  [Bad instruction address]  occurred and ignored
Exception occurred at PC=0x00000034
  Bad address in text read: 0x00000034
  Exception 6  [Bad instruction address]  occurred and ignored
Exception occurred at PC=0x00000038
  Bad address in text read: 0x00000038
  Exception 6  [Bad instruction address]  occurred and ignored

Could anyone take a look at my code and diagnose the problem? Thanks.


Solution

  • Your main function saves the value of $ra in $s0 and assumes that $s0 will remain unchanged, but the sqrt function will modify $s0.

    Either save $ra in a different register, or on the stack. Or replace the jr $s0 with:

    li $v0,10
    syscall