0

I'm not entirely sure where to turn to so I thought I'd give here a go. I was wondering if any of you could help me out with this MIPS code I've designed, yet will not give me the results I'm aiming for. The purpose is to essentially utilize a given input, denoted x, and then provide a specific amount of iterations, denoted n, where each iteration calculates the approximation to the square root using Newton's square root approximation. So far this is my code below. I have the feeling that the register values, namely the ones outside of my loop label are not reaching to the inside because I'm utilizing my jal instruction incorrectly. I apologize in advance for the messy use of random registers. Any help or tips would be very appreciated!

.data
prompt: .asciiz "Enter a floating point value for x: "
prompt1: .asciiz "\nPlease enter the number of iterations, n: "
prompt2: .asciiz "\nx approximated is: "
.text


li $v0, 4           #Asks for input x
la $a0, prompt
syscall

li $v0, 6           #User inputs x as float
syscall
mov.s $f0, $f3  #X stored into f3


li $v0, 4           #Asks for # of iterations
la $a0, prompt1
syscall

li $v0, 5                  #Gathers iterations n
syscall
move $s6, $v0           #Saves n iterations to $s6
mtc1 $s6, $f1               #Register $f1 holds the iterative values of n
cvt.s.w $f27, $f1           #Register 27 now holds float-point transferred value of integer entered previously
syscall


div.s $f25, $f30, $f30      #Creates $f25 = 1
add.s $f25, $f25, $f25      #Creates $f25 = 2

li $t5, 0                   #Initializes $t5 = 0

jal loop
loop: 
addi $t5, $t5, 1
mov.s $f3, $f5         #F5 holds register contents of f27, thus f3 is input x           
div.s $f5, $f5, $f27        #F5 holds f5/f27, thus f5 is now x/n    
add.s $f5, $f27, $f5        #F5 holds f27 + new f5 => n + x/n
div.s $f5, $f5, $f25        #F5 holds f27/f25 => (n + x/n)/2            

li $v0, 4               #Loads v0=4 to print prompt2
la $a0, prompt2      
syscall

li $v0, 2                #Loads v0 = 2 to print the float approximation
mov.s $f12, $f5 
syscall

beq $t5, $s6, done
jr $ra
done:
  • 1
    Use the debugger built-in to your MARS or SPIM to single-step your code and look at register values. That's what they're for. – Peter Cordes Apr 07 '18 at 07:26
  • I don't know much about MIPS; but to me it looks like the `jal loop` is in the wrong place (before the loop, and not at the end of the loop), causing the `jal` to do nothing at all (either jumping to `loop:` or not jumping and ending up at `loop:` anyway), and causing the loop to only ever be executed once. – Brendan Apr 07 '18 at 07:29
  • 1
    @Brendan `jal` is similar to `call` on other CPUs (except the return address is not put into stack, but written into `$ra` register). So "calling" the label after the call instruction in newbie code is by 99% chance a bug a shows the OP didn't yet fully understand how code-flow works in assembly. – Ped7g Apr 07 '18 at 10:22

0 Answers0