***MIPS*** Solve a quadratic equation in two linked files. Write a function to c
ID: 3718692 • Letter: #
Question
***MIPS***
Solve a quadratic equation in two linked files. Write a function to calculate the roots of any quadratic equation of the form ax +bx+c. a, b, and c are all single-precision floating point values and are passed to the function on the stack. The order you put the arguments on the stack is up to you When you return, there should be three items on the stack, two floating point values (referred to below as r1 and r2) and a Status integer. Status indicates the nature of the results returned. Again, the order you put the three values on the stack is up to you. Status 0:2 real roots in r1 & r2 1 1 real root in r1; r2 can be ignored 2: 2 complex roots of the form (r1 t ir2) 3 no roots computed (error); r1 and r2 can be ignored Your program should prompt for the values a, b, and c, then label and print the results. For the purposes of this function, assume two numbers that are within 0.001 are the same Submit the Following Filels: roots.asm, roots_main.asm Required Input: Three 32-bit floating point numbers (a, b, and c) Required Output Your output should look something like the following examples. Note this is three runs. Quadratic solver a: 3.8 b: 4.5 C: 1.2 There are two roots: e.23107084351742913, 1.7310708435174291 Quadratic solver a: 2.e b: 4.e C: 2.e There is one root: -1.00 Quadratic solver a: 4.3 b: -2.45 C: 2.e There are two complex roots: e. 284883721 +/-?.619643878ìExplanation / Answer
.data
prompt: .ascii "This program will calculate the roots of a given quadratic "
.asciiz "equation. "
proA: .ascii "Please input a (coefficient of x"
.byte 178 #squared symbol
.asciiz "): "
proB: .asciiz "Please input b (coefficient of x) : "
proC: .asciiz "Please input c (constant) : "
exp: .asciiz " The equation is of the form "
strAdd: .asciiz " + "
noroots: .asciiz "This equation has no real roots "
oneroot: .asciiz "This equation has one distinct root "
tworoot: .asciiz "This equation has two distinct roots "
yourone: .asciiz "Your one distinct root is "
yourtwo: .asciiz "Your two distinct roots are "
strand: .asciiz " and "
def: .float 2.0
def2: .float 4.0
def3: .float 0.0
.text
#####################################################################
# This section is for printing prompts and getting #
# inputs from the user #
#####################################################################
main: la $a0, prompt($0) #print(prompt)
addi $v0, $0, 4
syscall
la $a0, proA($0) #print(proA)
syscall
addi $v0, $0, 6 #f1==readFloat()
syscall
add.s $f1, $f0, $f5
la $a0, proB($0) #print(proB)
addi $v0, $0, 4
syscall
addi $v0, $0, 6 #f2==readFloat()
syscall
add.s $f2, $f0, $f5
la $a0, proC($0) #print(proC)
addi $v0, $0, 4
syscall
addi $v0, $0, 6 #f3==readFloat()
syscall
add.s $f3, $f0, $f5
la $a0, exp($0) #print(exp)
addi $v0, $0, 4
syscall
addi $v0, $0, 2 #print($f1);
add.s $f12, $f5, $f1
syscall
#####################################################################
# This section delegates reviewing user input to #
# a method called printeq. I did this to shorten #
# the still long opening part of this program #
# #
# Keep eye on the ball. Determinent(d) = b^2 - 4(a)(c) #
# If d < 0 no roots, if d=0 one root, else two roots #
#####################################################################
sw $ra, 0($sp) #push $ra
addi $sp, $sp, -4
jal printeq #run print equation sub
addi $sp, $sp, 4 #pop $ra
lw $ra, 0($sp)
lwc1 $f0, def2($0) #f0 = 4($f1)($f3)
mul.s $f0, $f0, $f1
mul.s $f0, $f0, $f3
mul.s $f3, $f2, $f2 #f3 = f2 ^2 <--C isn't actually needed ever again
sub.s $f0, $f3, $f0 #f0 = $f3 - $f0 --> f0 = b^2 - 4ac
addi $v0, $0, 4 #set to service 4 now... save three lines of repeated code in a moment
c.lt.s $f0, $f5 #if f0<0 jump norealroots
bc1t subNoRealRoots
#####################################################################
# at this point I'm pushing all variables and calling #
# sqrt because I'll need to do this reguardless of the #
# rest of the decision structure. In fact the decision #
# structure will work just as well with the sqrt. #
#####################################################################
sw $ra, 0($sp) #push ra #
swc1 $f1, 4($sp) #push f1 (a)
swc1 $f2, 8($sp) #push f2 (b)
addi $sp, $sp, -12
jal sqrt #f0 = sqrt(f0)
addi $sp, $sp, 12
lw $ra 0($sp) #pop ra
lwc1 $f1 4($sp) #pop f1
lwc1 $f2 8($sp) #pop f2
neg.s $f1, $f1 #f1 = -f1 (-b)
lwc1 $f3, def($0) #f3 = 2
mul.s $f1, $f1, $f3 #f2 *= 2 (2a)
#
#####################################################################
#continue if statement
c.eq.s $f0, $f5 #if f0=0 jump oneroot
bc1t subOneRealRoot
subTwoRoots: #else: there are two roots...
la $a0, tworoot #print(tworoot) -- v4 is already 4
syscall
add.s $f3, $f2, $f0 #f3 = -b + sqrt
sub.s $f4, $f2, $f0 #f4 = -b - sqrt
div.s $f3, $f3, $f1 #f3 /= 2a
div.s $f4, $f4, $f1 #f4 /= 2a
la $a0, yourtwo($0) #print(yourtwo)
syscall
addi $v0, $0, 2 #print(f3)
add.s $f12, $f5, $f3
syscall
la $a0, strand($0) #print(strand)
addi $v0, $0, 4
syscall
addi $v0, $0, 2 #print(f4)
add.s $f12, $f5, $f4
syscall
j exit
subOneRealRoot: #This is what happens when descriminate is =0
#(b+- 0) / 2a = b/2a
la $a0, oneroot
syscall
div.s $f0, $f2, $f1 #f0 = f2 / f1 --> b / 2a
la $a0, yourone #print(yourone)
syscall
addi $v0, $0, 2 #print(f0)
add.s $f12, $f0, $f5
syscall
j exit
subNoRealRoots: #This is what happens when descriminate is <0
la $a0, noroots($0) #print(noroots)
syscall
exit: jr $ra #exit program
#####################################################################
#===================================================================#
#| The following are all subs and functions (methods) |#
#| sqrt($f0) returns the square root of $f0. |#
#| printeq($f1, $f2, $f3) prints out the current equation |#
#===================================================================#
#####################################################################
# Square Root Function: This function returns the square #
# root of a number passed in $f0. Returned number is stored #
# in f0. This method works recursively without need for #
# storing previous variables. #
# #
# args: $f0 --> number to get the sqrt of #
# Consumes: f0, f1, f2, f3, f4 #
# Returns: f0 #
#####################################################################
sqrt: lwc1 $f1, def($0) #f1= f3 = 2
add.s $f3, $f5, $f1
c.eq.s $f0, $f5 #if f0 != 0 start sqrt loop (sqrtr)
bc1f sqrtr #else :
jr $ra #return 0 --> F12 is already 0
sqrtr: #recursive part
div.s $f2, $f0, $f1 #f1 = ((f0 / f1)+f1)/2
add.s $f1, $f1, $f2
div.s $f1, $f1, $f3
c.eq.s $f1, $f4 #if (f1 = f4)--> same f1 twice jump endsqrt
bc1t esqrt
add.s $f4, $f5, $f1 #else f4 = f1
j sqrtr
esqrt: add.s $f0, $f5, $f1 #set f0 = f1 as stated in contract
jr $ra #return f0
#####################################################################
# Print Equation Function, This method prints out the equation #
# ax^2 + bx + c with the user's inputs in f1, f2, and f3 #
# respectively representing a, b and c #
# #
# args: f1 = a, f2 = b, f3 = c #
# consumes: Nothing #
# returns: null #
#####################################################################
printeq:
addi $v0, $0, 11 #print x
addi $a0, $0, 120
syscall
addi $a0, $0, 178 #print("^2")
syscall
la $a0, strAdd($0) #print(strAdd);
addi $v0, $0, 4
syscall
addi $v0, $0, 2 #print($f2)
add.s $f12, $f5, $f2
syscall
addi $v0, $0, 11 #print("x")
addi $a0, $0, 120
syscall
la $a0, strAdd($0) #print(strAdd);
addi $v0, $0, 4
syscall
add.s $f12, $f5, $f3 #print($f3)
addi $v0, $0, 2
syscall
addi $a0, $0, 10 #print(" ");
addi $v0, $0, 11
syscall
jr $ra #return
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.