diff options
-rw-r--r-- | abs.asm | 44 | ||||
-rw-r--r-- | benchmark.asm | 67 | ||||
-rw-r--r-- | cpuid.asm | 26 | ||||
-rw-r--r-- | factorial.asm | 46 | ||||
-rw-r--r-- | leibniz.asm | 62 | ||||
-rw-r--r-- | nextpow2.asm | 46 | ||||
-rw-r--r-- | sincos.asm | 97 |
7 files changed, 388 insertions, 0 deletions
@@ -0,0 +1,44 @@ +format ELF64 + +extrn printf +extrn puts +extrn scanf +public main + +section '.text' executable + +main: + push rbp + mov rbp,rsp + + mov edi,prompt + call puts + + sub rsp,16 + + mov rsi,rsp + mov edi,readint + xor eax,eax + call scanf + + mov rax,[rsp] + + add rsp,16 + + cqo + xor rax,rdx + sub rax,rdx + + mov rsi,rax + mov edi,msg + xor eax,eax + call printf + + leave + ret + +section '.data' writable align 16 + +msg db "Absolute value is %lu",0xA,0 +prompt db "Enter a negative integer.",0 +readint db "%ld",0 diff --git a/benchmark.asm b/benchmark.asm new file mode 100644 index 0000000..e45848e --- /dev/null +++ b/benchmark.asm @@ -0,0 +1,67 @@ +format ELF64 + +extrn printf +extrn puts +extrn scanf +public main + +section '.text' executable + +main: + push rbp + mov rbp,rsp + + finit + + ; to determine how many clocks it takes to count clocks + cpuid ; force all previous instructions to complete + rdtsc ; read time stamp counter + shl rdx,32 ; shift rdx left 32 places + or rax,rdx ; timestamp in rax + mov r11,rax ; r11 stores the first timestamp + + ; get the second timestamp + cpuid ; force all previous instructions to complete + rdtsc ; read time stamp counter + shl rdx,32 ; shift rdx left 32 places + or rax,rdx ; timestamp in rax + + sub rax,r11 ; get difference in timestamps + mov r11,rax ; r11 stores clocks required to count clocks + + ; begin benchmarking + cpuid + rdtsc + shl rdx,32 + or rax,rdx + mov r10,rax ; r10 stores first timestamp + + ; code to benchmark goes here + nop + nop + + ; end code to benchmark + + ; end benchmarking, take timestamp + cpuid + rdtsc + shl rdx,32 + or rax,rdx ; rax stores second timestamp + + sub rax,r10 ; get difference in timestamps + sub rax,r11 ; subtract out time to count clocks + + ; print results + mov rsi,rax ; rsi is first argument (the number of clocks) + mov edi,msg ; rdi is second argument (location of msg) + xor eax,eax ; eax = 0 (number of floating point arguments) + call printf ; call printf and print results + + leave + ret + +section '.data' writable align 16 + +msg db "%lu clock cycles",0xA,0 + + diff --git a/cpuid.asm b/cpuid.asm new file mode 100644 index 0000000..4e573ae --- /dev/null +++ b/cpuid.asm @@ -0,0 +1,26 @@ +format ELF64 + +extrn printf +public main + +section '.text' executable + +main: + push rbp + mov rbp,rsp + + mov eax,1 + cpuid + shl rdx,32 + or rax,rdx + mov rsi,rax + mov edi,msg + xor eax,eax + call printf + + leave + ret + +section '.data' writable align 16 + +msg db "CPUID string is %lX",0xA,0 diff --git a/factorial.asm b/factorial.asm new file mode 100644 index 0000000..612d5e9 --- /dev/null +++ b/factorial.asm @@ -0,0 +1,46 @@ +format ELF64 + +extrn printf +extrn scanf +extrn puts +public main + +section '.text' executable + +main: push rbp + mov rbp,rsp + + mov edi,prompt + call puts + + sub rsp,16 + + mov rsi,rsp + mov edi,scan + xor eax,eax + call scanf + + mov rax,[rsp] + + add rsp,16 + + mov rsi,rax + lea rcx,[rax-1] +top: mul rcx + sub rcx,1 + jnz top + +print: mov edi,msg + mov rdx,rax + xor eax,eax + call printf + + leave + ret + + +section '.data' writable + +msg db "Factorial of %d is %d",0xA,0 +scan db "%d",0 +prompt db "Enter an unsigned integer.",0 diff --git a/leibniz.asm b/leibniz.asm new file mode 100644 index 0000000..e4f5653 --- /dev/null +++ b/leibniz.asm @@ -0,0 +1,62 @@ +format ELF64 + +extrn printf +public main + +section '.text' executable align 64 + +main: + push rbp + mov rbp,rsp + movdqa xmm6,dqword[denom] + movdqa xmm2,dqword[numer] + movdqa xmm3,dqword[add4] + movdqa xmm4,xmm2 + movdqa xmm5,dqword[zero] + mov r12,10000000 + +align 64 +top: + divpd xmm2,xmm6 + addpd xmm5,xmm2 + movdqa xmm2,xmm4 + addpd xmm6,xmm3 + divpd xmm2,xmm6 + addpd xmm5,xmm2 + movdqa xmm2,xmm4 + addpd xmm6,xmm3 + divpd xmm2,xmm6 + addpd xmm5,xmm2 + movdqa xmm2,xmm4 + addpd xmm6,xmm3 + divpd xmm2,xmm6 + addpd xmm5,xmm2 + movdqa xmm2,xmm4 + addpd xmm6,xmm3 + + sub r12,1 + jnz top + + mov r12,10000000 + movdqa xmm1,xmm5 + movdqa xmm0,xmm6 + haddpd xmm1,xmm1 + + mov eax,2 + mov edi,msg + call printf + + jmp top + + leave + + ret + +section '.data' writable align 16 + +denom: dq 1.0,3.0 +numer: dq 4.0,-4.0 +add4: dq 4.0,4.0 +zero: dq 0.0,0.0 + +msg db "1/%.0f - %50.48f",0xA,0 diff --git a/nextpow2.asm b/nextpow2.asm new file mode 100644 index 0000000..b319d88 --- /dev/null +++ b/nextpow2.asm @@ -0,0 +1,46 @@ +format ELF64 + +extrn printf +extrn scanf +extrn puts +public main + +section '.text' executable + +main: + push rbp + mov rbp,rsp + + mov edi,prompt + call puts + + sub rsp,16 + + mov rsi,rsp + mov edi,readint + xor eax,eax + call scanf + + mov r12,[rsp] + + add rsp,16 + + mov rax,r12 + bsr rcx,rax + mov eax,2 + shl rax,cl + + mov rdx,rax + mov rsi,r12 + mov edi,msg + xor eax,eax + call printf + + leave + ret + +section '.data' writable align 16 + +msg db "Next power of %lu is %lu",0xA,0 +readint db "%lu",0 +prompt db "Enter an integer.",0 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
+
|