Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I develop a function SQRT in MIPS assembly program for SPIM simulator, wh

ID: 3890328 • Letter: H

Question

How do I develop a function SQRT in MIPS assembly program for SPIM simulator, which will compute the square-root of a given number? My program should accept a number x from the screen/console input, which can be a negative or a positive number as well as it can be an integer or a real number. Algorithm for SQRT: will basically be doing a binary search for the given number range. For example, if we are to find SQRT (5): then search the mid value of the range 5 [i.e. middle point of 0 to 5] = 2.5. Now, 2.5 * 2.5 = 6.25 which is greater than 5.0. So, the answer must be within 0 to 2.5, so then again take the mid-point of [0 to 2.5] which is 1.25. Check 1.25 * 1.25 = 1.5625 which is less than 5.0. So the answer must be within 1.25 to 2.5 and similarly take the midpoint of that range and check. By keep on doing the steps repeatedly, once I get a mid-point y for which y × y = 5.0 or, is acceptably very close to 5.0, then that y will be your answer. For a given number x, my program i) must print all the calculated mid-points on the output screen and ii) print (and indicate) the square-root of x.

Explanation / Answer

.data x: .float 123.0 y: .float 0.0 one: .float 1.0 zero: .float 0.0 two: .float 2.0 precision: .float 0.000000001 counter: .word 0 intro: .asciiz "Calculates the SQRT of the Input for X in .data section!" inputMSG: .asciiz "Input: " newLine: .asciiz " " inmid:.asciiz "Mid point: " resultMSG: .asciiz "Result: " errorMSG: .asciiz "ERROR: Input for x < 0 not allowed." .text main: #Initialization l.s $f0, zero # zero l.s $f1, one # l.s $f2, two # two = 2.0 l.s $f3, x # x l.s $f4, y # y = result register l.s $f18, precision lw $t1, counter #maintains the count of how many times it has been looped and quits after 100 iterations li $t0, 0 #Set t0 for index of stack add.s $f11, $f3, $f0 #set z to x + 0 addi $sp, $sp, 4 #Print intro, with inputMSG + input X li $v0, 4 la $a0, intro syscall li $v0, 4 la $a0, newLine syscall li $v0, 4 la $a0, inputMSG syscall li $v0, 2 l.s $f12, x syscall li $v0, 4 la $a0, newLine syscall #-------Algorithm Here--------- #Branch for error if x < 0 c.eq.s $f3, $f0 bc1t zeroError c.lt.s $f3, $f0 bc1t zeroError #------------------------- loop: sub.s $f16, $f11, $f4 #$f16 = z - y c.eq.s $f16, $f0 #if difference is equal to zero then answer found bc1t loop3 c.lt.s $f16, $f18 #if difference is less than precision, end bc1t loop3 add.s $f13, $f11, $f4 # m = y + z div.s $f13, $f13, $f2 # divide m/2 li $v0, 4 la $a0, inmid syscall li $v0, 2 mov.s $f12, $f13 syscall la $a0, newLine li $v0, 4 syscall s.s $f13, ($sp) # add midpoint to stack addi $sp, $sp, 4 #increment stack by 4 mul.s $f6, $f13, $f13 #m * m = $f6 sub.s $f7, $f6, $f3 #subtract m^2 - x #branch if m^2 - x = 0, then the answer was found c.eq.s $f7, $f0 bc1t loop3 beq $t1, 100, loop3 #quit the program after 100 iterations #branch if m^2 - x < 0, then jump to loop2 c.lt.s $f7, $f0 bc1t loop2 #continue to loop1 if m^2 > 0 loop1: mov.s $f11, $f13 #set z to m (new upper limit) addi $t1, $t1, 1 #increment counter j loop loop2: mov.s $f4, $f13 #set y to m (new lower limit) addi $t1, $t1, 1 #increment counter j loop #----------Output Result-------------- loop3: la $a0, resultMSG li $v0, 4 syscall mov.s $f12, $f13 #print result in $f12 li $v0, 2 syscall li $v0, 10 syscall #exit #------------Print Error MSG for x < 0------------- zeroError: #Print a newline la $a0, newLine li $v0, 4 syscall #Print an Error Here la $a0, errorMSG li $v0, 4 syscall li $v0, 10 syscall #exit .end
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote