MUST BE IN MIPS Problem Description: Solve a quadratic equation in two linked fi
ID: 3718935 • Letter: M
Question
MUST BE IN MIPS
Problem Description:
Solve a quadratic equation in two linked files. Write a function to calculate the roots of any quadratic equation of the form ax2+bx+c, where the floating-point values a, b and c are passed to the function on the stack. Status, an integer value, should indicate the nature of the results returned as follows:
0 : 2 real roots in $r1 & $r2
1 : 1 real root in $r1
2: 2 complex roots of the form ($r1 ± i $r2)
3 : no roots computer (error)
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 File(s):
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.0
b: 4.5
c: -1.2
There are two roots: 0.23107084351742913, -1.7310708435174291
Quadratic Solver
a: 2.0
b: 4.0
c: 2.0
There is one root: -1.00
Quadratic Solver
a: 4.3
b: -2.45
c: 2.0
There are two complex roots: 0.284883721 +/- 0.619643078i
Explanation / Answer
#include <stdio.h>
int main(void)
{
double a, b, c, root1, root2;
extern int quad_roots(double, double, double, double*, double*);
printf("Enter quad constants a, b, c: ");
scanf("%lf %lf %lf",&a, &b, &c);
if (quad_roots(a, b, c, &root1, &root2))
printf("Root1 = %lf and root2 = %lf ", root1, root2);
else
printf("There are no real roots. ");
return 0;
}
%define a qword[EBP+8]
%define b qword[EBP+16]
%define c qword[EBP+24]
%define root1 dword[EBP+32]
%define root2 dword[EBP+36]
segment .text
global quad_roots
quad_roots:
enter 0,0
fld a ; a
fadd ST0 ; 2a
fld a ; a,2a
fld c ; c,a,2a
fmulp ST1 ; ac,2a
fadd ST0 ; 2ac,2a
fadd ST0 ; 4ac,2a
fchs ; -4ac,2a
fld b ; b,-4ac,2a
fld b ; b,b,-4ac,2a
faddp ST1 ; b*b-4ac,2a
ftst ; compare (b*b-4ac) with 0
fstsw AX ; store status word in AX
sahf
jb no_real_roots
fsqrt ; sqrt(b*b-4ac),2a
fld b ; b,sqrt(b*b-4ac),2a
fchs ; -b,sqrt(b*b-4ac),2a
fadd ST1 ; -b+sqrt(b*b-4ac),sqrt(b*b-4ac),2a
fdiv ST2 ; -b+sqrt(b*b-4ac)/2a,sqrt(b*b-4ac),2a
mov EAX,root1
fstp qword[EAX] ; store root1
fchs ; -sqrt(b*b-4ac),2a
fld b ; b,sqrt(b*b-4ac),2a
fsubp ST1 ; -b-sqrt(b*b-4ac),2a
fdivrp ST1 ; -b-sqrt(b*b-4ac)/2a
mov EAX,root2
fstp qword[EAX] ; store root2
mov EAX,1 ; real roots exist
jmp short done
no_real_roots:
sub EAX,EAX ; EAX = 0 (no real roots)
done:
leave
ret
28: fmulp ST1 ; b*b,-4ac,2a
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.