summaryrefslogtreecommitdiff
path: root/sincos.asm
diff options
context:
space:
mode:
Diffstat (limited to 'sincos.asm')
-rw-r--r--sincos.asm97
1 files changed, 97 insertions, 0 deletions
diff --git a/sincos.asm b/sincos.asm
new file mode 100644
index 0000000..5616c2f
--- /dev/null
+++ b/sincos.asm
@@ -0,0 +1,97 @@
+format ELF64
+
+section '.text' executable
+
+public main
+extrn puts
+extrn printf
+
+main:
+ finit
+
+ push rbp
+ mov rbp,rsp
+
+ call sin
+
+ movlpd xmm1,[y]
+ movlpd xmm0,[x] ; move floating point number into xmm0 for passage to printf
+ mov edi,sinmsg ; move formatted string to rdi (edi) (first argument) to send to printf
+ mov eax,2 ; number of floating point arguments
+ call printf ; call printf
+
+ call cos
+
+ movlpd xmm1,[y]
+ movlpd xmm0,[x]
+ mov edi,cosmsg
+ mov eax,2
+ call printf
+
+ xor eax,eax ; return 0 code
+ leave
+ ret
+
+sin:
+ fld [x] ; x
+ fmul st0,st0 ; x^2
+ fld [rf13] ; 1/13! x^2
+ fmul st0,st1 ; x^2/13! x^2
+ fsub [rf11] ; (-1/11! + x^2/13!) x^2
+ fmul st0,st1 ; (-x^2/11! + x^4/13!) x^2
+ fadd [rf9] ; 1/9! x^2
+ fmul st0,st1 ; x^2/9! x^2
+ fsub [rf7] ; (-1/7! + x^2/9!) x^2
+ fmul st0,st1 ; (-x^2/7! + x^4/9!) x^2
+ fadd [rf5] ; (1/5! - x^2/7! + x^4/9!) x^2
+ fmul st0,st1 ; (x^2/5! - x^4/7! + x^6/9!) x^2
+ fsub [rf3] ; (-1/3! + x^2/5! - x^4/7! + x^6/9!) x^2
+ fmulp st1,st0 ; (-x^2/3! + x^4/5! - x^6/7! + x^8/9!) x^2
+ fmul [x] ; (-x^3/3! + x^5/5! - x^7/7! + x^9/9!)
+ fadd [x] ; (x - x^3/3! + x^5/5! - x^7/7! + x^9/9!)
+ fstp [y] ;
+ ret
+
+cos:
+ fld [x] ; x
+ fmul st0,st0 ; x^2
+ fld [rf12] ; 1/12! x^2
+ fmul st0,st1 ; x^2/12! x^2
+ fsub [rf10] ; (-1/10! + x^2/12!) x^2
+ fmul st0,st1 ; (-x^2/10! + x^4/12!) x^2
+ fadd [rf8] ; (1/8!) x^2
+ fmul st0,st1 ; (x^2/8!) x^2
+ fsub [rf6] ; (-1/6! + x^2/8!) x^2
+ fmul st0,st1 ; (-x^2/6! + x^4/8!) x^2
+ fadd [rf4] ; (1/4! - x^2/6! + x^4/8!) x^2
+ fmul st0,st1 ; (x^2/4! - x^4/6! + x^6/8!) x^2
+ fsub [rf2] ; (-1/2 + x^2/4! - x^4/6! + x^6/8!) x^2
+ fmulp st1,st0 ; (-x^2/2 + x^4/4! - x^6/6! + x^8/8!)
+ fadd [one] ; (1 - x^2/2 + x^4/4! - x^6/6! + x^8/8!)
+ fstp [y] ;
+ ret
+
+section '.data' writable align 16
+
+; reciprocals of factorials
+rf13 dq 1.6059043836821614599392377170154e-10
+rf12 dq 2.0876756987868098979210090321201e-9
+rf11 dq 2.5052108385441718774420151415934e-8
+rf10 dq 2.7557319223985890651862166557528e-7
+rf9 dq 2.7557319223985890651862166557528e-6
+rf8 dq 2.4801587301587301587301587301587e-5
+rf7 dq 0.0001984126984126984126984126984127
+rf6 dq 0.0013888888888888888888888888888889
+rf5 dq 0.0083333333333333333333333333333333
+rf4 dq 0.0416666666666666666666666666666667
+rf3 dq 0.1666666666666666666666666666666667
+rf2 dq 0.5
+
+one dq 1.0
+
+x dq 3.14159265358979323846264338327950288419716939937510
+y dq ?
+
+sinmsg db "The sine of %.32f is %.32f",0xA,0
+cosmsg db "The cosine of %.32f is %.32f",0xA,0
+