Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TITLE PASS 8 QUADRATIC FORMULA                      (main.asm)
  2.  
  3. ; Description:
  4. ; Caluculates the elapsed time over perfomring the quadratic formula 1000000 times
  5.  
  6. INCLUDE Irvine32.inc
  7. .data
  8. ;-----------------------
  9. ;messages & prompts
  10. ;----------------------
  11. myMessage       BYTE    "Rhett Newton's Quadratic Formula Solver",0dh,0ah,0
  12. explanation     BYTE    "Solve x^2-3x-4 with quadratic formula using FPU",0dh,0ah,0
  13. coef_a_prompt   BYTE    "Enter coefficient a",0dh,0ah,0
  14. coef_b_prompt   BYTE    "Enter coefficient b",0dh,0ah,0
  15. coef_c_prompt   BYTE    "Enter coefficient c",0dh,0ah,0
  16. realRoots       BYTE    "Real Roots    :",0dh,0ah,0
  17. timeMessage     BYTE    "Computational time: ", 0
  18.  
  19. ;----------------------
  20. ;variables
  21. ;----------------------
  22. startTime   DWORD   ?
  23. endTime     DWORD   ?
  24. coef_a      BYTE    ?
  25. coef_b      BYTE    ?
  26. coef_c      BYTE    ?
  27. root1       DWORD   ?
  28. root2       DWORD   ?
  29. dbl1        REAL4   ?
  30. dbl2        REAL4   ?
  31.  
  32. ;----------------------
  33. ;prototypes
  34. ;----------------------
  35. displayMessage  PROTO, string:PTR BYTE
  36. quadFormula     PROTO, num:DWORD
  37. .code
  38. main PROC
  39.     call    Clrscr
  40.     finit
  41.     invoke  displayMessage, OFFSET myMessage    ;displaying welcome message
  42.     invoke  displayMessage, OFFSET explanation  ;displaying explanation
  43.  
  44.     ;--------------------------------
  45.     ;getting coefficents
  46.     ;--------------------------------
  47.     call    getCoefficients                     ;calling getCoefficients procedure
  48.    
  49.     ;------------------------------------------
  50.     ;performing the quadratic formula x times
  51.     ;------------------------------------------
  52.     invoke  quadFormula, 1000000                ;invoking quadFormula
  53.  
  54.     ;------------------------------------------
  55.     ;displaying real roots
  56.     ;------------------------------------------
  57.     call    displayRoots                        ;calling display roots
  58.  
  59.     ;-------------------------------
  60.     ;Displaying Benchmark stats
  61.     ;-------------------------------
  62.     ;calculate ending time here**********
  63.     invoke  displayMessage, OFFSET timeMessage ;displaying timeMessage
  64.     call    WriteDec                           ;displaying elapsedTime
  65.     nop
  66.     exit
  67. main ENDP
  68.  
  69. ;--------------------------------------
  70. quadFormula PROC num:DWORD
  71. ;Performs the quadratic formula x
  72. ;amount of times.
  73. ;params: num: the number of times to
  74. ;               perform the operation
  75. ;--------------------------------------
  76.     mov     ecx, num        ;moving num to the ecx register
  77.     ;-------------------------------
  78.     ;getting the starting time
  79.     ;-------------------------------
  80.     call    GetTickCount    ;getting the current time
  81.     mov     startTime, eax  ;storing starting time
  82.  
  83. L1:
  84.     ;the meat of the program is here
  85.     ;-----------------------------------------------------
  86.     ;getting 2 * a
  87.     finit
  88.     mov     eax, 2
  89.     mul     coef_a
  90.     mov     dbl2, eax
  91.     fld     dbl2            ;2*a is stored in st(0)
  92.     ;getting -4*a*c
  93.     movzx   eax, coef_a    
  94.     mul     coef_c
  95.     mov     bl, -4
  96.     mul     bl
  97.     ;storing in ebx
  98.     mov     ebx, eax
  99.     ;getting b^2
  100.     movzx   eax, coef_b
  101.     mul     eax
  102.     ;getting b^2-4*a*c
  103.     sub     eax, ebx
  104.     mov     dbl1, eax
  105.     fld     dbl1            ;storing b^2-4*a*c in st(0), st(1) = 2 * a
  106.     ;getting sqrt(b^2-4*a*c)
  107.     fsqrt
  108.     ;dividing by 2 * a
  109.     fdiv    st(0), st(2)
  110.     fld     st(0)           ;now st(0) = st(1) = sqrt(b^2-4*a*c)/2*a
  111.     movzx   ebx, coef_b
  112.     neg     ebx
  113.     mov     dbl1, ebx
  114.     fld     dbl1
  115.     fsub    st(2), st       ;subtract value of st(0) from st(2), store in st(2)
  116.     fadd    st(1), st       ;add value of st(0) to st(1), store in st(1)
  117.     ;-----------------------------------------------------
  118.     dec     ecx             ;decrementing the ecx register
  119.     test    ecx, ecx        ;testing if ecx is zero
  120.     jnz     L1              ;if not zero, repeat loop
  121.    
  122.     fld     st(2)           ;getting -b - sqrt(b^2-4*a*c)/2*a into st(0)
  123.     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)
  124.     ;-------------------------------
  125.     ;getting the ending time
  126.     ;-------------------------------
  127.     call    GetTickCount    ;getting the ending time
  128.     mov     endTime, eax    ;calculating the elapsed time
  129.  
  130.     ret
  131. quadFormula ENDP
  132.  
  133. ;------------------------------------
  134. displayRoots PROC
  135. ;displays the numbers stored in st(0) and st(1)
  136. ;------------------------------------
  137.     invoke  displayMessage, OFFSET realRoots
  138.     call    WriteFloat
  139.     call    crlf
  140.     fld     st(1)
  141.     call    WriteFloat
  142.     call    crlf
  143.     ret
  144. displayRoots ENDP
  145.  
  146. ;-------------------------------------
  147. getCoefficients PROC
  148. ;prompts the user for the coefficents
  149. ;to use in the quadratic formula.
  150. ;stores them in their respective variables
  151. ;-------------------------------------
  152.     ;getting coefficent a
  153.     invoke  displayMessage, OFFSET coef_a_prompt    ;displaying coef_a_prompt
  154.     call    readInt                                 ;getting input from user
  155.     mov     coef_a, al                              ;storing input to variable
  156.  
  157.     ;getting coefficent b
  158.     invoke  displayMessage, OFFSET coef_b_prompt    ;displaying coef_b_prompt
  159.     call    readInt                                 ;getting input from user
  160.     mov     coef_b, al                              ;storing input to variable
  161.  
  162.     ;getting coefficent c
  163.     invoke  displayMessage, OFFSET coef_c_prompt    ;displaying coef_c_prompt
  164.     call    readInt                                 ;getting input from user
  165.     mov     coef_c, al                              ;storing input to variable
  166.  
  167.     ret
  168. getCoefficients ENDP
  169.  
  170. ;------------------------------------
  171. displayMessage PROC string:PTR BYTE
  172. ;Displays a message to the user
  173. ;params: string: a ptr to a byte array
  174. ;------------------------------------
  175.     mov     edx, string     ;moves ptr to edx register
  176.     call    WriteString     ;displays string
  177.     ret
  178. displayMessage ENDP
  179. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement