首页 > 解决方案 > 牛顿求平方根方法的 MIPS 异常 6 [错误指令地址]

问题描述

我正在尝试实现牛顿法来计算给定 k 的整数平方根(此处为 $a0)。我得到了正确的输出,但随后出现了一系列无限的异常 6 错误,如下所示:

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

任何人都可以看看我的代码并诊断问题吗?谢谢。

标签: exceptionassemblymipsqtspim

解决方案


您的main函数保存$rain的值$s0并假设该值$s0将保持不变,但该sqrt函数将修改$s0.

要么保存$ra在不同的寄存器中,要么保存在堆栈中。或替换为jr $s0

li $v0,10
syscall

推荐阅读