Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE PASS 8 QUADRATIC FORMULA (main.asm)
- ; Description:
- ; Caluculates the elapsed time over perfomring the quadratic formula 1000000 times
- INCLUDE Irvine32.inc
- .data
- ;-----------------------
- ;messages & prompts
- ;----------------------
- myMessage BYTE "Rhett Newton's Quadratic Formula Solver",0dh,0ah,0
- explanation BYTE "Solve x^2-3x-4 with quadratic formula using FPU",0dh,0ah,0
- coef_a_prompt BYTE "Enter coefficient a",0dh,0ah,0
- coef_b_prompt BYTE "Enter coefficient b",0dh,0ah,0
- coef_c_prompt BYTE "Enter coefficient c",0dh,0ah,0
- realRoots BYTE "Real Roots :",0dh,0ah,0
- timeMessage BYTE "Computational time: ", 0
- ;----------------------
- ;variables
- ;----------------------
- startTime DWORD ?
- endTime DWORD ?
- coef_a BYTE ?
- coef_b BYTE ?
- coef_c BYTE ?
- root1 DWORD ?
- root2 DWORD ?
- dbl1 REAL4 ?
- dbl2 REAL4 ?
- ;----------------------
- ;prototypes
- ;----------------------
- displayMessage PROTO, string:PTR BYTE
- quadFormula PROTO, num:DWORD
- .code
- main PROC
- call Clrscr
- finit
- invoke displayMessage, OFFSET myMessage ;displaying welcome message
- invoke displayMessage, OFFSET explanation ;displaying explanation
- ;--------------------------------
- ;getting coefficents
- ;--------------------------------
- call getCoefficients ;calling getCoefficients procedure
- ;------------------------------------------
- ;performing the quadratic formula x times
- ;------------------------------------------
- invoke quadFormula, 1000000 ;invoking quadFormula
- ;------------------------------------------
- ;displaying real roots
- ;------------------------------------------
- call displayRoots ;calling display roots
- ;-------------------------------
- ;Displaying Benchmark stats
- ;-------------------------------
- ;calculate ending time here**********
- invoke displayMessage, OFFSET timeMessage ;displaying timeMessage
- call WriteDec ;displaying elapsedTime
- nop
- exit
- main ENDP
- ;--------------------------------------
- quadFormula PROC num:DWORD
- ;Performs the quadratic formula x
- ;amount of times.
- ;params: num: the number of times to
- ; perform the operation
- ;--------------------------------------
- mov ecx, num ;moving num to the ecx register
- ;-------------------------------
- ;getting the starting time
- ;-------------------------------
- call GetTickCount ;getting the current time
- mov startTime, eax ;storing starting time
- L1:
- ;the meat of the program is here
- ;-----------------------------------------------------
- ;getting 2 * a
- finit
- mov eax, 2
- mul coef_a
- mov dbl2, eax
- fld dbl2 ;2*a is stored in st(0)
- ;getting -4*a*c
- movzx eax, coef_a
- mul coef_c
- mov bl, -4
- mul bl
- ;storing in ebx
- mov ebx, eax
- ;getting b^2
- movzx eax, coef_b
- mul eax
- ;getting b^2-4*a*c
- sub eax, ebx
- mov dbl1, eax
- fld dbl1 ;storing b^2-4*a*c in st(0), st(1) = 2 * a
- ;getting sqrt(b^2-4*a*c)
- fsqrt
- ;dividing by 2 * a
- fdiv st(0), st(2)
- fld st(0) ;now st(0) = st(1) = sqrt(b^2-4*a*c)/2*a
- movzx ebx, coef_b
- neg ebx
- mov dbl1, ebx
- fld dbl1
- fsub st(2), st ;subtract value of st(0) from st(2), store in st(2)
- fadd st(1), st ;add value of st(0) to st(1), store in st(1)
- ;-----------------------------------------------------
- dec ecx ;decrementing the ecx register
- test ecx, ecx ;testing if ecx is zero
- jnz L1 ;if not zero, repeat loop
- fld st(2) ;getting -b - sqrt(b^2-4*a*c)/2*a into st(0)
- fld st(2) ;getting -b + sqrt(b^2-4*a*c)/2*a into st(0) //-b - sqrt(b^2-4*a*c)/2*a st(1)
- ;-------------------------------
- ;getting the ending time
- ;-------------------------------
- call GetTickCount ;getting the ending time
- mov endTime, eax ;calculating the elapsed time
- ret
- quadFormula ENDP
- ;------------------------------------
- displayRoots PROC
- ;displays the numbers stored in st(0) and st(1)
- ;------------------------------------
- invoke displayMessage, OFFSET realRoots
- call WriteFloat
- call crlf
- fld st(1)
- call WriteFloat
- call crlf
- ret
- displayRoots ENDP
- ;-------------------------------------
- getCoefficients PROC
- ;prompts the user for the coefficents
- ;to use in the quadratic formula.
- ;stores them in their respective variables
- ;-------------------------------------
- ;getting coefficent a
- invoke displayMessage, OFFSET coef_a_prompt ;displaying coef_a_prompt
- call readInt ;getting input from user
- mov coef_a, al ;storing input to variable
- ;getting coefficent b
- invoke displayMessage, OFFSET coef_b_prompt ;displaying coef_b_prompt
- call readInt ;getting input from user
- mov coef_b, al ;storing input to variable
- ;getting coefficent c
- invoke displayMessage, OFFSET coef_c_prompt ;displaying coef_c_prompt
- call readInt ;getting input from user
- mov coef_c, al ;storing input to variable
- ret
- getCoefficients ENDP
- ;------------------------------------
- displayMessage PROC string:PTR BYTE
- ;Displays a message to the user
- ;params: string: a ptr to a byte array
- ;------------------------------------
- mov edx, string ;moves ptr to edx register
- call WriteString ;displays string
- ret
- displayMessage ENDP
- END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement