Noah the Goodra avatar

MIPS SQRT using Babylonian method

jfmherokiller

Published: 03 Oct 2017 › Updated: 03 Oct 2017MIPS SQRT using Babylonian method

MIPS SQRT using Babylonian method

.data

accuracy: .double  0.000001

starting: .double 1.0

DivideByTwo: .double 2.0

Zero: .double 0.0

.text

main:

    l.d $f2,starting # load starting end point

    l.d $f8,accuracy # load the accuracy constant into $f8:$f9

    l.d $f16,DivideByTwo

    li  $v0, 7           # service 7 is read double it stores the read double in $f0:$f1

    syscall #execute syscall 

    mov.d $f10,$f0 #save original value

    nop

    #check if input is less then zero and end if true

    l.d $f4,Zero

    c.lt.d $f0,$f4

    bc1f whileLoop

    j EndMe

whileLoop:

sub.d   $f6,$f0,$f2 #subtract the input double eg $f0:$f1 - $f2:$f3 or input - y

c.lt.d  $f8,$f6     # 0.000001 < (input-y)

nop

bc1f EndLoop #if true then end the loop

nop

add.d $f0,$f0,$f2 # x = (x + y)

div.d $f0,$f0,$f16 # x = x/2

div.d $f2,$f10,$f0 # y= orginal/x

jal PrintArea #print x and y

nop

b whileLoop #branch back to top of while loop

    

    

EndLoop:    

    jal PrintArea

    nop

    li $v0,10 #exit syscall

    syscall

    j EndMe

    

PrintArea:

li $a0,0xA0 # load new line char into $a0

li $v0,3 

mov.d $f12,$f0 #print x

syscall

li $v0,11 #print new line char

syscall

mov.d $f12,$f2 #print y

syscall

li $v0,11  #print new line char

syscall

jr $ra

EndMe:

Leave MIPS SQRT using Babylonian method to:

Written by

nerdy gooey coder that posts furry content sometimes

Read more #mips posts


Best Posts From Noah the Goodra

We have not curated any of jfmherokiller's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Noah the Goodra