1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
|