Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
59
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    "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      SDWORD  ?
  25. coef_b      SDWORD  ?
  26. coef_c      SDWORD  ?
  27. root1       DWORD   ?
  28. root2       DWORD   ?
  29. dbl1        REAL4   ?
  30. dbl2        REAL4   ?
  31. temp        SDWORD  ?
  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.     mov     eax, endTime
  63.     sub     eax, startTime
  64.     invoke  displayMessage, OFFSET timeMessage ;displaying timeMessage
  65.     call    WriteInt                           ;displaying elapsedTime
  66.     nop
  67.     exit
  68. main ENDP
  69.  
  70. ;--------------------------------------
  71. quadFormula PROC num:DWORD
  72. ;Performs the quadratic formula x
  73. ;amount of times.
  74. ;params: num: the number of times to
  75. ;               perform the operation
  76. ;--------------------------------------
  77.     mov     ecx, num        ;moving num to the ecx register
  78.     ;-------------------------------
  79.     ;getting the starting time
  80.     ;-------------------------------
  81.     call    GetTickCount    ;getting the current time
  82.     mov     startTime, eax  ;storing starting time
  83.  
  84. L1:
  85.     ;the meat of the program is here
  86.     ;-----------------------------------------------------
  87.     ;getting 2 * a
  88.     finit
  89.     mov     eax, 2          ;mov eax
  90.     imul    coef_a          ;signed multiply by coef_a
  91.     mov     temp, eax       ;mov eax to temp to store in stack
  92.     fild    temp            ;st(0) = 2*a
  93.     ;getting 4*a*c
  94.     mov     eax, coef_a     ;loading coef_a into eax
  95.     imul    coef_c          ;signed multiply by coef_c
  96.     mov     ebx, 4          ;move 4 into ebx
  97.     imul    ebx             ;signed multiply by ebx, eax now equals 4*a*c
  98.     mov     ebx, eax        ;mov 4*a*c into ebx for safe keeping
  99.     ;getting b^2
  100.     mov     eax, coef_b     ;moving coef_b into eax
  101.     mul     eax             ;eax now was b^2
  102.     ;getting b^2-4*a*c
  103.     sub     eax, ebx        ;eax = b^2-4*a*c
  104.     mov     temp, eax       ;mov eax to temp to store in stack
  105.     fild    temp            ;st(0) = b^2-4*a*c, st(1) = 2*a
  106.     ;getting sqrt(b^2-4*a*c)
  107.     fsqrt                   ;st(0) = sqrt(b^2-4*a*c)
  108.     ;getting -b +- sqrt(b^2-4*a*c)
  109.     mov     eax, coef_b     ;mov coef_b to eax
  110.     neg     eax             ;negate eax
  111.     mov     temp, eax       ;mov to temp
  112.     fild    temp            ;st(0) = -b, st(1) = sqrt(b^2-4*a*c), st(2) = 2*a
  113.     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
  114.     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
  115.     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
  116.     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
  117.     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
  118.     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
  119.     ;-----------------------------------------------------
  120.     dec     ecx             ;decrementing the ecx register
  121.     test    ecx, ecx        ;testing if ecx is zero
  122.     jnz     L1              ;if not zero, repeat loop
  123.     ;-------------------------------
  124.     ;getting the ending time
  125.     ;-------------------------------
  126.     call    GetTickCount    ;getting the ending time
  127.     mov     endTime, eax    ;calculating the elapsed time
  128.  
  129.     ret
  130. quadFormula ENDP
  131.  
  132. ;------------------------------------
  133. displayRoots PROC
  134. ;displays the numbers stored in st(0) and st(1)
  135. ;------------------------------------
  136.     invoke  displayMessage, OFFSET realRoots    ;displaying realRoots message
  137.     call    WriteFloat                          ;writing what is at st(0)
  138.     call    crlf                               
  139.     fld     st(1)                               ;loading st(1) into st(0)
  140.     call    WriteFloat                          ;writing what is at st(0)
  141.     call    crlf
  142.     ret
  143. displayRoots ENDP
  144.  
  145. ;-------------------------------------
  146. getCoefficients PROC
  147. ;prompts the user for the coefficents
  148. ;to use in the quadratic formula.
  149. ;stores them in their respective variables
  150. ;-------------------------------------
  151.     ;getting coefficent a
  152.     invoke  displayMessage, OFFSET coef_a_prompt    ;displaying coef_a_prompt
  153.     call    readInt                                 ;getting input from user
  154.     mov     coef_a, eax                             ;storing input to variable
  155.  
  156.     ;getting coefficent b
  157.     invoke  displayMessage, OFFSET coef_b_prompt    ;displaying coef_b_prompt
  158.     call    readInt                                 ;getting input from user
  159.     mov     coef_b, eax                             ;storing input to variable
  160.  
  161.     ;getting coefficent c
  162.     invoke  displayMessage, OFFSET coef_c_prompt    ;displaying coef_c_prompt
  163.     call    readInt                                 ;getting input from user
  164.     mov     coef_c, eax                             ;storing input to variable
  165.  
  166.     ret
  167. getCoefficients ENDP
  168.  
  169. ;------------------------------------
  170. displayMessage PROC string:PTR BYTE
  171. ;Displays a message to the user
  172. ;params: string: a ptr to a byte array
  173. ;------------------------------------
  174.     mov     edx, string     ;moves ptr to edx register
  175.     call    WriteString     ;displays string
  176.     ret
  177. displayMessage ENDP
  178. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement