Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

. data prompt _ a: . asciiz Enter value for a: prompt _ b: . asciiz Enter value for b: prompt _ c:

.data
prompt_a: .asciiz "Enter value for a: "
prompt_b: .asciiz "Enter value for b: "
prompt_c: .asciiz "Enter value for c: "
output_solution: .asciiz "Quadratic Equation Solver v0.1 by F. Last
"
invalid_input: .asciiz "Invalid input. Please enter a non-zero value for a and/or b.
"
imaginary_roots: .asciiz "Roots are imaginary.
"
solution: .asciiz "x1=%.2f, x2=%.2f
"
.text
.globl main
main:
# Print output header
li $v0,4
la $a0, output_solution
syscall
# Prompt user for coefficients
jal prompt_coefficients
# Calculate discriminant
l.d $f4,0($f12) # Load a into $f4
l.d $f6,0($f14) # Load b into $f6
l.d $f8,0($f16) # Load c into $f8
mul.d $f10, $f6, $f6 # b^2
mul.d $f12, $f4, $f8 # 4ac
mul.d $f12, $f12, $f2 # 4ac * a
sub.d $f10, $f10, $f12 # b^2-4ac
mov.d $f12, $f10 # Store discriminant
# Check for non-quadratic equation
beq $f4, $f0, not_quadratic
beq $f6, $f0, not_quadratic
# Check if roots are imaginary
blez $f10, imaginary
# Calculate roots
sqrt.d $f10, $f10
l.d $f12,0($f12) # Load -b into $f12
neg.d $f12, $f12 # -b
add.d $f10, $f12, $f10
div.d $f10, $f10, $f4 # x1=(-b + sqrt(b^2-4ac))/2a
l.d $f12,0($f12) # Load -b into $f12
sub.d $f12, $f12, $f10
div.d $f12, $f12, $f4 # x2=(-b - sqrt(b^2-4ac))/2a
# Print solutions
li $v0,2
mov.d $f12, $f10
syscall
li $v0,4
la $a0, solution
syscall
mov.d $f12, $f10
syscall
j exit
not_quadratic:
# Print error message for non-quadratic equation
li $v0,4
la $a0, invalid_input
syscall
j exit
imaginary:
# Print error message for imaginary roots
li $v0,4
la $a0, imaginary_roots
syscall
j exit
prompt_coefficients:
# Prompt for coefficient a
li $v0,4
la $a0, prompt_a
syscall
li $v0,7
syscall
mfc1 $f12, $v0
# Prompt for coefficient b
li $v0,4
la $a0, prompt_b
syscall
li $v0,7
syscall
mfc1 $f14, $v0
# Prompt for coefficient c
li $v0,4
la $a0, prompt_c
syscall
li $v0,7
syscall
mfc1 $f16, $v0
jr $ra
exit:
# Exit program
li $v0,10
syscall

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago