Quadratic Equation Solver!: C BX Ax Ac B B
Quadratic Equation Solver!: C BX Ax Ac B B
b 2 − 4ac
This is a good excuse to examine the floating-point facilities available for MIPS
programmers.
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
MIPS Floating-Point Architecture! Procedures! 2
MIPS includes two coprocessors that support specialized execution features. One is
dedicated to computations involving floating-point values:
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Selected MIPS Floating-Point Instructions! Procedures! 3
.float
.double directives for declaring 32- and 64-bit floating-point data
There are also many instructions for conversion, comparison and branching. Here is a
sampling of the single-precision instructions; each has a double-precision analog.
The MIPS Architecture for Programmers, Volume II is an excellent reference for the
complete MIPS instruction set.
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Quadratic Solver Design! Procedures! 5
When the solver begins execution, the stack will be in the following logical state:
a
b
sp c
...
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Quadratic Solver Call! Procedures! 6
################################################################ main
main:
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Quadratic Solver! Procedures! 7
########################################################### quad_solver
quad_solver:
.data
two: .word 2
four: .word 4
.text
# 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
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Quadratic Solver! Procedures! 8
# 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
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
get_coefficient! Procedures! 9
######################################################## 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:
jr $ra
#################################################### end get_coefficient
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!
Leaf vs Non-leaf Procedures! Procedures! 10
So far we have only considered leaf procedures, that is, procedures that do not make calls
themselves.
Non-leaf procedures must save their return address before executing a jal, and then
restore that value before executing a return. For example:
######################################################### get_quadratic
get_quadratic:
addi $sp, $sp, -4 # preserve return address on the stack
sw $ra, ($sp)
Computer Science Dept Va Tech January 2006! Intro Computer Organization! ©2006 McQuain & Ribbens!