Search code examples
assemblymipsmips32

Can someone look to see if i am doing my Newtons Method in MIPS correctly?(Assembly)


I am new to this and i am trying to do it without using co-processors. Any suggestions? I really need to do good on this assignment! This code is supposed to be in MIPS assembly in which codes newtons method and contains the a-e points. I keep getting my code to asemble but it drops off the bottom.....i am not sure what that means though.

#Part 1
# (A) 
# g(x) = 0
# x = 7^(1/3)
# g(x) = x^3 - 7
# g'(x) = 3x^2
.data
x: .float 1.0
bb: .float 7.0
c: .float 3.0
epsilom: .float 0.00001
divide: .float 2.0

#=======================================
# (B)
# Write a MIPS function newtons_top that takes 1 “floating-point argument”: 
# $f0   xn, the current estimate of the root 
# It computes your function g(xn) = g($f0), and leaves the result in $f2.  Do not 
# modify $f0’s value.  (This would be a good place to document your g(x).)
newtons_top:
l.s $f0, x
l.s $f1, bb
mul.s $f5, $f0, $f0
mul.s $f0, $f5, $f0
sub.s $f2, $f0, $f1

#=======================================
# (C)
# $f0   xn, the current estimate of the root 
# It computes your function g’(xn) = g’($f0), and leaves the result in $f4.  Do not 
# modify $f0’s value.  (This would be a good place to document your g’(x).)
newtons_bottom:
l.s $f3, c
mul.s $f5, $f0, $f0
mul.s $f4, $f5, $f3


#======================================
#(D)
#  Write a MIPS function newtons_err that takes 1 “floating-point argument”: 
# $f0   xn, the current estimate of the root 
# It computes the error term ?n from (Eq. 3), and leaves the result in $f6.  Do not 
# modify $f0’s value.  Hint: Call your previous function
newtons_err:
div.s $f6,$f2,$f4


#========================================
#(E)

newtons_nth:
div.s $f2, $f0, $f1  # $f2 gets n/x
add.s $f2, $f2, $f1  # $f2 gets n/x + x
div.s $f2, $f2, divide  # $f2 gets x'=(n/x + x)/2
sub.s $f3, $f2, $f1  # $f3 gets x'-x
abs.s $f3, $f3       # $f3 gets |x'-x|
c.lt.s $f3, epsilom     # set the flag if |x'-x| < 0.00001
li $v0, 1


#===========================================
#(F)
#  Write a MIPS function newtons_gee that takes no arguments, and does this: 
#• Initializes x0 = 1.0 from (Eq. 1), by setting $f0 to 1.0. 
# • Repeatedly calls newtons_nth until the return value is non-zero. 
# Remember to pass the current  n in $a0 (even if your implementation 
# doesn’t use it for printing)

newtons_gee:
l.s $f0, 1
beqz $t3, newtons_nth

#=============================================
# Main Block

main:
j newtons_gee
syscall 

Solution

  • Here's a start.

    The following code is nonsense:

    newtons_gee:
        l.s $f0, 1
        beqz $t3, newtons_nth
    

    To call a function in MIPS assembly you should be using the jal instruction and not beqz. $t3 has an undefined value (probably non-zero) when main jumps to newtons_gee, so the branch is not taken and control returns back to main.

    You need to go back and re-read your notes on how to do function calls.