2

Just want to know about a function in MIPS that can take under root of any number in my MIPS program.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • What do you want to know about it? – Erik Eidt Mar 31 '21 at 15:30
  • Do you mean *square* root? Or do you mean arbitrary roots like `pow(x, 1.0/3)` – Peter Cordes Mar 31 '21 at 17:14
  • Ok seriously, what is going on with MIPS answers in the last 2 or 3 days? There have been floods of new-user answers to MIPS questions, including ones that were many years old, some of them already answered, most of the rest already answered in comments. Is there a class assignment to find and answer a MIPS question on SO? – Peter Cordes Mar 31 '21 at 17:18
  • Duplicate of [Finding square root of an integer on MIPS assembly](https://stackoverflow.com/q/17868029) – Peter Cordes Mar 31 '21 at 19:24

4 Answers4

0

You can use simplified version of Newton Method to find roots of integer

x=N
iterate 20 times:
x'=(x+N/x) /2
x=x'

Mips implementation

.data
.text
main:
    li $t0,25        #N

    move $t1,$t0     #x
    li $t4,0    #loop variable
sqrLoop:
    #Newton Formula
    div $t3, $t0, $t1   # N/x 
    add $t1, $t3, $t1   # x + N/x 
    div $t1, $t1, 2     # (x + N/x)/2 

    #loop
    add $t4, $t4, 1 
    blt $t4, 20, sqrLoop 


end:

     li $v0,10
    syscall
Aahad
  • 1
  • If you're going to use integer math (not FP), use `srl` by 1 to divide by 2 instead of another slow `div`. – Peter Cordes Mar 31 '21 at 17:11
  • Also, loop with `add $t4, $t4, -1` / `bne $zero, $t4 sqrLoop` to count $t4 *down* toward zero. (BNE between two registers is a true hardware instruction; BNE against an immediate constant is only a pseudo-instruction that has to put `20` in a register first). – Peter Cordes Mar 31 '21 at 17:11
0

try this out.

   #DATA

   .data

   square: .asciiz "Enter the number you wish to find the square root for: "
   answer: .asciiz "The answer is: "
   newline: .asciiz "\n"

   #Text

   .text
   .globl main

    main:
    li $v0, 4               #Prompt user for input
    la $a0, square
    syscall
    
    li $v0, 5               #Receive said input
    syscall
    move $a0, $v0
    
    move $t4, $zero         #Move variables to t registers
    move $t1, $a0
    
    addi $t0, $zero, 1      #Set $t0 to 1
    sll $t0, $t0, 30        #Bit Shift $t0 left by 30
    
    #For loop
    loop1:
        slt $t2, $t1, $t0
        beq $t2, $zero, loop2   
        nop
        
        srl $t0, $t0, 2         #Shift $t0 right by 2
        j loop1
        
    loop2:
        beq $t0, $zero, return  
        nop
        
        add $t3, $t4, $t0       #if $t0 != zero add t0 and t4 into t3
        slt $t2, $t1, $t3       
        beq $t2, $zero, else1   
        nop
        
        srl $t4, $t4, 1         #shift $t4 right by 1
        j loopEnd
        
    else1:
        sub $t1, $t1, $t3       #Decrement $t1 by $t3
        srl $t4, $t4, 1         #Shift $t4 right by 1
        add $t4, $t4, $t0       #then add $t0 to that
        
    loopEnd:
        srl $t0, $t0, 2         #shift $t0 to the right
        j loop2
        
    return:
        li $v0, 4               #print out the answer then exit
        la $a0, answer
        syscall
    
        li $v0, 1
        move $a0, $t4
        syscall
        
        li $v0, 10
        syscall
Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • Same algorithm as [Finding square root of an integer on MIPS assembly](https://stackoverflow.com/a/19306324) and very similar code with worse comments and less clear label names. Different registers in a couple cases, and added NOPs to work on a MIPS with branch-delay slots. (But only after `b` not `j` instructions?) Perhaps just a coincidence of translating the same pseudo-code / C. – Peter Cordes Mar 31 '21 at 19:29
-2

.data

.text

.globl main

.ent main

Sqrt:

move $v1, $a1 # $v0 = x = N li $t0, 0 # counter

sqrLoop:

div $t8, $a1, $v1 # N/x add $v1, $t8, $v1 # x + N/x div $v1, $v1, 2 # (x + N/x)/2 add $t0, $t0, 1 blt $t0, 20, sqrLoop

jr $ra

.end Sqrt

This is a function when called will take out the square root of the number . Just call the function by writing "jal Sqrt" where necessary or needed .

  • 3
    Welcome to Stack Overflow. Please read the [editing help](https://stackoverflow.com/editing-help) and format your code as code. – David Buck Mar 31 '21 at 14:26
-2

You can try this algorithm, which gives the integer smaller than or equal to the square root of your number.

Suppose you want the square root of n. Then keep repeating the following calculations:

x = (x + n/x) / 2

Choose x = n to start and keep repeating until x stops changing.

Here is the following MIPS program library you can add in your program.

#SquareRoot.s

#DATA
.data

square: .asciiz "Enter the number you wish to find the square root for: "
answer: .asciiz "The answer is: "
newline: .asciiz "\n"

#Text

.text
.globl main

main:
    li $v0, 4               #Prompt user for input
    la $a0, square
    syscall
    
    li $v0, 5               #Receive said input
    syscall
    move $a0, $v0
    
    move $t4, $zero         #Move variables to t registers
    move $t1, $a0
    
    addi $t0, $zero, 1      #Set $t0 to 1
    sll $t0, $t0, 30        #Bit Shift $t0 left by 30
    
    #For loop
    loop1:
        slt $t2, $t1, $t0
        beq $t2, $zero, loop2   
        nop
        
        srl $t0, $t0, 2         #Shift $t0 right by 2
        j loop1
        
    loop2:
        beq $t0, $zero, return  
        nop
        
        add $t3, $t4, $t0       #if $t0 != zero add t0 and t4 into t3
        slt $t2, $t1, $t3       
        beq $t2, $zero, else1   
        nop
        
        srl $t4, $t4, 1         #shift $t4 right by 1
        j loopEnd
        
    else1:
        sub $t1, $t1, $t3       #Decrement $t1 by $t3
        srl $t4, $t4, 1         #Shift $t4 right by 1
        add $t4, $t4, $t0       #then add $t0 to that
        
    loopEnd:
        srl $t0, $t0, 2         #shift $t0 to the right
        j loop2
        
    return:
        li $v0, 4               #print out the answer then exit
        la $a0, answer
        syscall
    
        li $v0, 1
        move $a0, $t4
        syscall
        
        li $v0, 10
        syscall
Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • 2
    This code block is identical (except for a bit of indenting near the top) to [@Ahmed Asif's answer](https://stackoverflow.com/questions/66888931/mips-how-to-find-under-root/66892302#66892302) which was posted a couple minutes before this. It implements a binary bit-at-a-time algorithm that's *not* the same as `x = (x + n/x) / 2` Newton iterations. (Algorithm looks like one of the ones in [Fastest Integer Square Root in the least amount of instructions](https://stackoverflow.com/q/31117497)). So it's possibly plagiarized, and incorrectly describes the algorithm it implements. – Peter Cordes Mar 31 '21 at 17:25