Create a mips assembly program that solves the quadratic for
Solution
main:
addi $sp, $sp, -12 # reserve stack space for the
# coefficients
la $a0, prmpt1 # get coefficient of x^2
jal get_coefficient
s.s $f0, 8($sp) # put it on the stack
la $a0, prmpt2 # get coefficient of x
jal get_coefficient
s.s $f0, 4($sp) # put it on the stack
la $a0, prmpt3 # get constant term
jal get_coefficient
s.s $f0, 0($sp) # put it on the stack
jal quad_solver # call the quadratic eq\'n solver
beq $v0, $zero, OK # check exit code from solver
########################################################### quad_solver
quad_solver:
.data
two: .word 2
four: .word 4
.text
l.s $f0, 8($sp) # retrieve coeffs from stack
l.s $f1, 4($sp)
l.s $f2, 0($sp)
li $v0, 0 # default to success
# calculate discriminant
mul.s $f8, $f1, $f1 # f8 = B^2
mul.s $f9, $f0, $f2 # f9 = A*C
l.s $f5, four
cvt.s.w $f5, $f5 # f5 = 4.0
mul.s $f9, $f9, $f5 # f9 = 4*A*C
# test discriminant
c.lt.s $f8, $f9 # is B^2 < 4*A*C?
bc1f isOK # if not, compute solutions
li $v0, 1 # else, set error code
jr $ra # and quit
# ... continues ...
# OK, compute solutions
isOK:
neg.s $f9, $f9 # f9 = -4*A*C
add.s $f9, $f8, $f9 # f9 = B^2 - 4*A*C
sqrt.s $f9, $f9 # f9 = sqrt(B^2 - 4*A*C)
mov.s $f7, $f1
neg.s $f7, $f7 # f7 = -B
l.s $f5, two
cvt.s.w $f5, $f5
mul.s $f8, $f5, $f0 # f8 = 2*A
add.s $f10, $f7, $f9
div.s $f10, $f10, $f8 # f10 = one root
neg.s $f9, $f9
add.s $f11, $f7, $f9
div.s $f11, $f11, $f8 # f11 = other root
jr $ra
######################################################## end quad_solver
######################################################## get_coefficient
# Prompt the user to enter an integer value. Read and return
# it. It takes the address of the prompt as its only parameter.
get_coefficient:
li $v0, 4 # system call code for printing a string = 4
syscall # call operating system to perform
# print operation
li $v0, 6 # system call code for reading a float = 6
syscall # system waits for input, puts the
# value in $f0
jr $ra
#################################################### end get_coefficient

