Guest User

Untitled

a guest
Nov 10th, 2016
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. ;
  4. ; file: quad.asm
  5.  
  6. ; function quadratic
  7. ; finds solutions to the quadratic equation:
  8. ;       a*x^2 + b*x + c = 0
  9. ; C prototype:
  10. ;   int quadratic( double a, double b, double c,
  11. ;                  double * root1, double *root2 )
  12. ; Parameters:
  13. ;   a, b, c - coefficients of powers of quadratic equation (see above)
  14. ;   root1   - pointer to double to store first root in
  15. ;   root2   - pointer to double to store second root in
  16. ; Return value:
  17. ;   returns 1 if real roots found, else 0
  18.  
  19. %define a               qword [ebp+8]
  20. %define b               qword [ebp+16]
  21. %define c               qword [ebp+24]
  22. %define root1           dword [ebp+32]
  23. %define root2           dword [ebp+36]
  24. %define disc            qword [ebp-8]
  25. %define one_over_2a     qword [ebp-16]
  26.  
  27. segment .data
  28. MinusFour       dw      -4
  29.  
  30. segment .bss
  31.  
  32.  
  33.  
  34. segment .text
  35.         global  _quadratic
  36. _quadratic:
  37.         push    ebp
  38.         mov     ebp, esp
  39.         sub     esp, 16         ; allocate 2 doubles (disc & one_over_2a)
  40.         push    ebx             ; must save original ebx
  41.  
  42.         fild    word [MinusFour]; stack -4
  43.         fld     a               ; stack: a, -4
  44.         fld     c               ; stack: c, a, -4
  45.         fmulp   st1             ; stack: a*c, -4
  46.         fmulp   st1             ; stack: -4*a*c
  47.         fld     b
  48.         fld     b               ; stack: b, b, -4*a*c
  49.         fmulp   st1             ; stack: b*b, -4*a*c
  50.         faddp   st1             ; stack: b*b - 4*a*c
  51.         ftst                    ; test with 0
  52.         fstsw   ax
  53.         sahf
  54.         jb      no_real_solutions ; if disc < 0, no real solutions
  55.         fsqrt                   ; stack: sqrt(b*b - 4*a*c)
  56.         fstp    disc            ; store and pop stack
  57.         fld1                    ; stack: 1.0
  58.         fld     a               ; stack: a, 1.0
  59.         fscale                  ; stack: a * 2^(1.0) = 2*a, 1
  60.         fdivp   st1             ; stack: 1/(2*a)
  61.         fst     one_over_2a     ; stack: 1/(2*a)
  62.         fld     b               ; stack: b, 1/(2*a)
  63.         fld     disc            ; stack: disc, b, 1/(2*a)
  64.         fsubrp  st1             ; stack: disc - b, 1/(2*a)
  65.         fmulp   st1             ; stack: (-b + disc)/(2*a)
  66.         mov     ebx, root1
  67.         fstp    qword [ebx]     ; store in *root1
  68.         fld     b               ; stack: b
  69.         fld     disc            ; stack: disc, b
  70.         fchs                    ; stack: -disc, b
  71.         fsubrp  st1             ; stack: -disc - b
  72.         fmul    one_over_2a     ; stack: (-b - disc)/(2*a)
  73.         mov     ebx, root2
  74.         fstp    qword [ebx]     ; store in *root2
  75.         mov     eax, 1          ; return value is 1
  76.         jmp     short quit
  77.  
  78. no_real_solutions:
  79.         ffree   st0             ; dump disc off stack
  80.         mov     eax, 0          ; return value is 0
  81.  
  82. quit:
  83.         pop     ebx
  84.         mov     esp, ebp
  85.         pop     ebp
  86.         ret
Advertisement
Add Comment
Please, Sign In to add comment