【问题标题】:Mips single precision (involving newtons method)Mips 单精度(涉及牛顿法)
【发布时间】:2012-04-10 15:07:57
【问题描述】:

我正在上计算机拱门课程,我们正在使用单精度进行 mips。作业涉及创建牛顿法。我已经编写了所有需要的函数,但无法弄清楚代码到底出了什么问题。我也不完全确定如何将值打印到屏幕上。非常感谢所有帮助。我已经逐行写了 cmets 来解释我在做什么。

这是我的代码:

# Newtons Method
# g(x) = (-x^3) + 11
# g'(x) = -3x^2


.data
precision: .float .00001


main:
jal newtons_gee

newtons_top:

li $s2, 11      # stores 11 into the $s2 register
mtc1 $s2, $f5       # moves the value of $s1 (3) to the coproc 1
mul.s $f1,$f0,$f0   # This finds x^2 
mul.s $f1,$f0,$f1   # This finds x^3
add.s $f2,$f1,$f5   # adds the value of (x^3) + 11 and stores it into $f2 as         asked

newtons_bot:
li $s1, -3      # stores -3 into the $s1 register
mtc1 $s1, $f3       # moves the value of $s1 (-3) to the coproc 1   
mul.s $f5, $f3,$f0  # Calculating -3x in derivative of the original function
mul.s $f4,$f5,$f0   # Calculates -3x^2 and stores it in $f4 as asked

newtons_err:
jal newtons_top     # Calls newtons_top
jal newtons_bot     # Calles newtons_bot
div.s $f6,$f2,$f4   # Calculates g(Xn)/ g'(Xn) and stores it in $f6 as asked

newtons_nth:
addi $a0,$a0,1      # Increases the current iteration by 1
jal newtons_err     # Call newtons_err
sub.s $f7,$f0,$f6   # Calculate value of En
mov.s $f7,$f0       # Find the new nth 
abs.s $f3, $f3      # Flag Case
l.s $f9, precision  # Precision 
c.lt.s $f3, $f9         # set the flag if |x'-x| < 0.00001    stores in $v0
j newtons_nth       # Repeat
newtons_gee:
li $f0, 1       # Sets the Xn to 1
li $a0, 1       # Sets the current iteration
jal newtons_nth # Calls newtons_nth

【问题讨论】:

    标签: assembly mips mips32


    【解决方案1】:

    一些提示:

    • 当您调用一个函数时,您需要返回,否则您的代码将继续下一节。 Newtons_top 会遇到 newtons_bot,newtons_bot 会遇到 newtons_err,然后它们会再次被调用,导致无限循环。 标签只是标签;它们不会神奇地结束您的函数。 当您使用 jal 指令时,返回地址会自动加载到 $ra 中。因此,您可以使用jr $ra 返回。

    • 当您有嵌套调用时,例如在 newtons_err 中,您的 $ra 会被压扁。所以在使用jal之前需要先备份$ra,然后在返回之前恢复。

    • 您的循环何时结束?你没有退出条件。您正在检查精度,但之后没有条件跳转。它将永远持续下去。

    • 您想使用 11 和 -3 的值,但您没有正确地将它们加载到浮点寄存器中。当您使用 mtc1 时,您正在逐字复制这些值。问题是 11 作为整数 not 与浮点中的 11 相同。您需要额外的指令将其转换为浮点数:

      cvt.s.w $f5, $f5

      这是包含该转换的参考:http://www.doc.ic.ac.uk/lab/secondyear/spim/node20.html

    • li $f0, 1 无效。您实际上不能将立即数加载到浮点寄存器中。您可以使用与 11 和 -3 相同的方法,但仍需要上面的转换说明。

    • 1234563 > mtc1 $s2, $fX.
    • MIPS 指令集不知道打印到屏幕意味着什么。打印到屏幕涉及到与其他硬件的通信,因此要回答这个问题,您需要知道板上的硬件是什么。

    【讨论】:

    • 非常感谢您的建议!这是非常需要的帮助:)
    • 您在使用 MARS 模拟器吗?如果是这样,请检查此打印值:courses.missouristate.edu/KenVollmar/MARS/Help/SyscallHelp.html
    • 我应该把 jr $ra's 放在哪里我明白你的意思只是不确定把它们放在哪里?
    • 你可以把它放在每个子程序的末尾。所以对于 newtons_top 你可以把它放在 add.s 之后;对于 newtons_bot,您可以将其放在第二个 mul.s 之后。
    • newtons_err 和 newtons_nth 也应该有它,但您需要先将 $ra 复制到另一个寄存器,因为嵌套的 jal 会覆盖它。
    猜你喜欢
    • 2013-10-30
    • 2011-06-26
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2017-05-05
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多