; PROGRAM: ROOTS OF QUADRATIC EQUATION
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg db 'Enter a, b, c (ax^2+bx+c): $'
err_msg db 'No Roots Exist!$'
space db ' $'
comma db ', $'
minus db '-$'
rootsr db 'Roots: $'
nl db 0dh,0ah,'$'
root db 5 dup('$')
root1 db 00h
root2 db 00h
sign1 db 00h
numa db 00h
numb db 00h
numc db 00h
sum db 00h
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
print msg
read numa
print space
read numb
print space
read numc
;Find b sqaure - 4ac
mov bl, numb
mov al, bl
mul bl
mov dx, ax ;b^2 in dx
mov al, numa
mov bl, 04h
mul bl
mov bl, numc
mul bl ;4ac in ax
sub dx, ax ;dx is b^2 - 4ac
cmp dx, 00h
jge ROOTSEXIST
print nl
print err_msg
jmp FINISH
ROOTSEXIST:
;Find root of dx
mov al, dl
mov cl, 00h
mov bl, 01h
REPSUB:
cmp al, 00h
je STOP_E
jl STOP_LT
sub al, bl
inc cl
add bl, 02h
jmp REPSUB
STOP_LT:
dec cl
STOP_E:
mov al, cl ;al holds root b2-4ac
;find -b+-sqrt(bsq-4ac)
mov bl, numb
mov dl, bl
cmp al, bl
jge SIGNOK1
mov sign1, 01h
SIGNOK1:
sub bl, al ;-b+sqr(..)
mov root1, bl
add al, dl ;-b-sqr(..)
mov root2, al
;calculate 2a
mov al, numa
mov bl, 02h
mul bl
mov bl, al ;bl holds 2a
;calculate -b../2a
mov al, root1
mov ah, 00h
div bl
mov root1, al
mov ah, 00h
mov al, root2
div bl
mov root2, al
mov al, root1
mov ah, 00h
mov si, offset root
call hex2ascii
print nl
print rootsr
cmp sign1, 00h
je NOMINUS1
print minus
NOMINUS1:
print root
print comma
mov si, offset root
mov al, root2
mov ah, 00h
call hex2ascii
print minus
print root
FINISH:
mov ah, 4ch
int 21h
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
add al, 30h
mov [si], al
inc si
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>q2
Enter a, b, c (ax^2+bx+c): 01 04 04
Roots: -02, -02
Z:\S5IT\masm>q2
Enter a, b, c (ax^2+bx+c): 01 05 06
Roots: -02, -03
Z:\S5IT\masm>q2
**************************
0 comments:
Post a Comment