Write a program to calculate roots of equation using quadratic formula using MIPS simulator.
5x2 -4x-12=0
# 5x^2-4x-12=0
# x1=2.0
# x2=-1.2
.data
Rezult1Prompt: .asciiz "\nx1 = "
Rezult2Prompt: .asciiz "\nx2 = "
fourFloat: .float -4.0
fiveFloat: .float 5.0
twelveFloat: .float -12.0
two: .word 2
four: .word 4
# code segment
.text
.globl main
main:
lwc1 $f0, fiveFloat # $f0 = 5.0
lwc1 $f1, fourFloat # $f1 = -4.0
lwc1 $f2, twelveFloat # $f2 = -12.0
# calculate discriminant B^2-4AC
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: B^2 < 4*A*C
c.lt.s $f8, $f9 # B^2 < 4*A*C?
bc1f YesSolution # if not, compute solutions
j notSolution # else, not solutions
# compute solutions
YesSolution:
neg.s $f9, $f9 # -4*A*C
add.s $f9, $f8, $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 = x1
neg.s $f9, $f9
add.s $f11, $f7, $f9
div.s $f11, $f11, $f8 # f11 = x2
li $v0, 4 # printing string
la $a0,Rezult1Prompt # "\nx1 = "
syscall
li $v0, 2 # system call code for printing a float
mov.s $f12,$f10
syscall
li $v0, 4 # printing a string
la $a0,Rezult2Prompt
syscall
li $v0, 2 # system call code for printing a float
mov.s $f12,$f11
syscall
notSolution:
li $v0, 10 # finished .. stop .. return
syscall # make the syscall
Comments
Leave a comment