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 "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 SDWORD ?
- coef_b SDWORD ?
- coef_c SDWORD ?
- root1 DWORD ?
- root2 DWORD ?
- dbl1 REAL4 ?
- dbl2 REAL4 ?
- temp SDWORD ?
- ;----------------------
- ;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
- ;-------------------------------
- mov eax, endTime
- sub eax, startTime
- invoke displayMessage, OFFSET timeMessage ;displaying timeMessage
- call WriteInt ;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 ;mov eax
- imul coef_a ;signed multiply by coef_a
- mov temp, eax ;mov eax to temp to store in stack
- fild temp ;st(0) = 2*a
- ;getting 4*a*c
- mov eax, coef_a ;loading coef_a into eax
- imul coef_c ;signed multiply by coef_c
- mov ebx, 4 ;move 4 into ebx
- imul ebx ;signed multiply by ebx, eax now equals 4*a*c
- mov ebx, eax ;mov 4*a*c into ebx for safe keeping
- ;getting b^2
- mov eax, coef_b ;moving coef_b into eax
- mul eax ;eax now was b^2
- ;getting b^2-4*a*c
- sub eax, ebx ;eax = b^2-4*a*c
- mov temp, eax ;mov eax to temp to store in stack
- fild temp ;st(0) = b^2-4*a*c, st(1) = 2*a
- ;getting sqrt(b^2-4*a*c)
- fsqrt ;st(0) = sqrt(b^2-4*a*c)
- ;getting -b +- sqrt(b^2-4*a*c)
- mov eax, coef_b ;mov coef_b to eax
- neg eax ;negate eax
- mov temp, eax ;mov to temp
- fild temp ;st(0) = -b, st(1) = sqrt(b^2-4*a*c), st(2) = 2*a
- fsub st(0), st(1) ;st(0) = -b - sqrt(b^2-4*a*c), st(1) = sqrt(b^2-4*a*c), st(2) = 2*a
- fild temp ;st(0) = -b, st(1) = -b - sqrt(b^2-4*a*c), st(2) = sqrt(b^2-4*a*c), st(3) = 2*a
- fadd st(0), st(2) ;st(0) = -b + sqrt(b^2-4*a*c), st(1) = -b - sqrt(b^2-4*a*c), st(2) = sqrt(b^2-4*a*c), st(3) = 2*a
- fdiv st(0), st(3) ;st(0) = (-b + sqrt(b^2-4*a*c))/2*a, st(1) = -b - sqrt(b^2-4*a*c), st(2) = sqrt(b^2-4*a*c), st(3) = 2*a
- fld st(1) ;st(0) = -b - sqrt(b^2-4*a*c), st(1) = (-b + sqrt(b^2-4*a*c))/2*a, st(2) = -b - sqrt(b^2-4*a*c), st(3) = sqrt(b^2-4*a*c), st(4) = 2*a
- fdiv st(0), st(4) ;st(0) = (-b - sqrt(b^2-4*a*c))/2*a, st(1) = (-b + sqrt(b^2-4*a*c))/2*a, st(2) = -b - sqrt(b^2-4*a*c), st(3) = sqrt(b^2-4*a*c), st(4) = 2*a
- ;-----------------------------------------------------
- dec ecx ;decrementing the ecx register
- test ecx, ecx ;testing if ecx is zero
- jnz L1 ;if not zero, repeat loop
- ;-------------------------------
- ;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 ;displaying realRoots message
- call WriteFloat ;writing what is at st(0)
- call crlf
- fld st(1) ;loading st(1) into st(0)
- call WriteFloat ;writing what is at st(0)
- 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, eax ;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, eax ;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, eax ;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