Instructions
Objective
Write a program using assembly language, to find both roots of a quadratic equation in assembly language.
Requirements and Specifications
Description:
Write an x86 assembler program to find the roots of a quadratic equation. This problem is the same as problem 12.6 in your text (p 590).
Some sample sessions are included in the attachment.
Suggest you test your program using this site.
Caveats:
- Program must use FPU instructions
- Program must use multiple procedures
- Up to 10% extra credit if the program is a multi-module program
Screenshots of output



Source Code
Program
INCLUDE irvine32.inc
EXTERN ReadCoefficient@0:PROC
EXTERN SolveQuadratic@0:PROC
; Redefine external symbols for convenience
ReadCoefficient EQU ReadCoefficient@0
SolveQuadratic EQU SolveQuadratic@0
.data
coefA REAL8 0.0
coefB REAL8 0.0
coefC REAL8 0.0
.code
main PROC
mov al, 'A' ; coefficient A
push eax ; pass as argument
call ReadCoefficient ; read coefficient A
add esp, 4 ; restore stack
fstp coefA ; save in coefA
mov al, 'B' ; coefficient B
push eax ; pass as argument
call ReadCoefficient ; read coefficient B
add esp, 4 ; restore stack
fstp coefB ; save in coefB
mov al, 'C' ; coefficient C
push eax ; pass as argument
call ReadCoefficient ; read coefficient C
add esp, 4 ; restore stack
fstp coefC ; save in coefC
fld coefC ; load C for subroutine
fld coefB ; load C for subroutine
fld coefA ; load C for subroutine
call SolveQuadratic ; solve the equation
exit ; terminate
main ENDP
END main
Read coefficients
INCLUDE irvine32.inc
.data
prompt BYTE "Coefficient ("
promptCoef BYTE " "
BYTE ") for Ax^2 + Bx + C: ", 0
.code
; Prompt for coefficient and return read number
; At the start: [ebp+8] = coefficient name
; At return: EAX = read number
ReadCoefficient PROC
enter 0,0
mov eax, [ebp + 8] ; load coefficient name
mov [promptCoef], al ; save coefficient name in prompt message
lea edx, prompt ; prompt user to enter coefficient
call WriteString
call ReadFloat ; read the number
leave
ret ; return the read number in float stack
ReadCoefficient ENDP
END
Solve quadratic
INCLUDE irvine32.inc
.data
Solution1 BYTE "Root1: ", 0
Solution2 BYTE "Root2: ", 0
NoSolution BYTE "This polynomial has an imaginary root", 0
coefA REAL8 0.0
coefB REAL8 0.0
coefC REAL8 0.0
const0 REAL8 0.0
const2 REAL8 2.0
const4 REAL8 4.0
B2 REAL8 0.0
fourAC REAL8 0.0
twoA REAL8 0.0
Root1 REAL8 0.0
Root2 REAL8 0.0
disc REAL8 0.0
.code
; Solve the quadratic equation
; At the start: ST0 = A, ST1 = B, ST2 = C
SolveQuadratic PROC
enter 0, 0
fstp coefA ; save coefficient A
fstp coefB ; save coefficient B
fstp coefC ; save coefficient C
fld coefB ; load B
fmul coefB ; calculate B^2
fstp B2 ; save in B^2
fld const4 ; load 4
fmul coefA ; multiply 4*A
fmul coefC ; multiply 4*A*C
fstp fourAC ; save in fourAC
fld B2 ; load B^2
fsub fourAC ; subtract B^2 - 4*A*C
fst disc ; save as discriminant
fcomp const0 ; compare discriminant with zero
fnstsw ax
sahf
jb displayImaginary ; if disc < 0, there is no solution
fld coefB ; load B
fchs ; negate to -B
fstp coefB ; update variable
fld coefA ; load A
fmul const2 ; multipy 2*a
fstp twoA ; save
fld disc ; load discriminant
fsqrt ; take square root
fstp disc ; update discriminant
fld coefB ; load -B
fadd disc ; add -B + sqrt(B^2 - 4AC)
fdiv twoA ; divide -B + sqrt(B^2 - 4AC) / 2a
fstp Root1 ; save first root
fld coefB ; load -B
fsub disc ; add -B - sqrt(B^2 - 4AC)
fdiv twoA ; divide -B - sqrt(B^2 - 4AC) / 2a
fstp Root2 ; save second root
jmp displayRoots ; display the roots
displayImaginary:
lea edx, NoSolution ; print no solution
call WriteString
call CRLF
jmp Return ; terminate
displayRoots:
lea edx, Solution1 ; print first solution
call WriteString
fld Root1 ; load first solution
call WriteFloat ; print root
call CRLF
lea edx, Solution2 ; print second solution
call WriteString
fld Root2 ; load second solution
call WriteFloat ; print root
call CRLF
Return:
leave
ret ; return the read number
SolveQuadratic ENDP
END