简体   繁体   中英

Assembly rdtsc x64 ubuntu

I'm trying to use rdtsc function but i've got weird numbers. I'm trying to call this function from C code and pass the tick back to function. Can you tell me if im doing it right or not ?

Asm code:

.text

.globl czas
.type czas, @function

czas:
pushq %rbp
movq %rsp, %rbp
xor %rax,%rax;
cpuid
rdtsc
popq %rbp
ret

C code:

unsigned long long Czas;
Czas=czas();

rdtsc returns result in edx : eax even in 64 bit mode, but C calling convention expects result in rax . You have to pack the result yourself. Note you don't typically need a stack frame for this. Something like:

cpuid
rdtsc
shl $32, %rdx
or %rdx, %rax
ret

What kind of type is your function? It should be UINT64. rdtsc return low 32 bits in EAX and high 32 bits in EDX register. So is you wont result in RAX than perform:

  shl   rdx, 32     //left shift for 32 bits
  or    rax, rdx    //Compose both registers in 64 bit RAX

after executing rdtsc instruction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM