Created
June 4, 2024 05:19
-
-
Save lfmunoz/867685dff948f101e1bf3afebdfbf26b to your computer and use it in GitHub Desktop.
determine number of cyccles for cpu operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
section .data | |
fmt db "Value: %ld", 0x0a, 0 ; Format string for printf | |
iterations dq 0x3b9aca00 ; 1000000000 | |
section .bss | |
number resq 1 ; Reserve 1 quadword (64 bits or 8 bytes) for number used for printf | |
sum resq 1 ; Reserve 1 quadword (64 bits or 8 bytes) for sum | |
cyclesa resq 1 ; Reserve 1 quadword (64 bits or 8 bytes) for rdtsc output | |
cyclesb resq 1 ; Reserve 1 quadword (64 bits or 8 bytes) for rdtsc output | |
section .text | |
global main | |
extern printf | |
get_cyclesA: | |
rdtsc | |
shl rdx, 32 | |
mov rcx,rax | |
or rcx,rdx | |
mov [cyclesa], rcx ; Move the value in rax to cpucycles | |
ret ; Return to the caller (address is already on the stack) | |
get_cyclesB: | |
rdtsc | |
shl rdx, 32 | |
mov rcx,rax | |
or rcx,rdx | |
mov [cyclesb], rcx ; Move the value in rax to cpucycles | |
ret ; Return to the caller (address is already on the stack) | |
print_number: | |
lea rdi, [fmt] ; parameter 1 for printf | |
mov rsi, [number] ; Load the value at rsp (the reserved stack space) into rsi | |
xor eax, eax ; Zero out eax to indicate no floating point parameters for printf | |
call printf | |
ret ; Return to the caller (address is already on the stack) | |
main: | |
push rbp ; pushes rbp immediately below the return address. | |
mov rbp, rsp ; makes rbp point to that object (stack address) | |
sub rsp, 8 ; Align the stack to 16 bytes, i.e, you pushed rbp so you get 8 bytes misaligned | |
call get_cyclesA | |
mov edx, [iterations] ; set loop variable 1000000000 | |
add_loop: | |
mov rax, QWORD [sum] ; read memory to rax | |
add rax,0x1 ; add 1 to rax | |
mov QWORD [sum],rax ; write rax to memory | |
sub edx,0x1 ; subtract 1 from loop variable | |
jne add_loop | |
call get_cyclesB | |
mov r9, [cyclesa] | |
mov [number], r9 | |
call print_number ; print cycles start | |
mov r8, [cyclesb] | |
mov [number], r8 | |
call print_number ; print cycles end | |
mov rax, [cyclesb] | |
mov rbx, [cyclesa] | |
sub rax, rbx | |
mov [number], rax | |
call print_number ; print difference | |
mov rax, [cyclesb] | |
mov rbx, [cyclesa] | |
sub rax, rbx | |
mov r12, [iterations] | |
xor rdx, rdx | |
div r12 ; rax / r10 rax has answer remainder in rdx | |
mov [number], rax | |
call print_number ; print average | |
add rsp, 8 ; Restore the stack pointer | |
xor eax, eax ; return 0 | |
pop rbp | |
ret | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpu_cycles.elf: cpu_cycles.asm | |
nasm -f elf64 -g -F dwarf $< -o cpu_cycles.o | |
# ld -o $@ lfm.o | |
gcc -no-pie -o $@ cpu_cycles.o | |
./$@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment