简体   繁体   English

汇编rdtsc x64 ubuntu

[英]Assembly rdtsc x64 ubuntu

I'm trying to use rdtsc function but i've got weird numbers. 我正在尝试使用rdtsc函数,但我有奇怪的数字。 I'm trying to call this function from C code and pass the tick back to function. 我正试图从C代码调用此函数并将滴答传递回函数。 Can you tell me if im doing it right or not ? 你能告诉我,我是否正确行事?

Asm code: Asm代码:

.text

.globl czas
.type czas, @function

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

C code: C代码:

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 . 即使在64位模式下, rdtsc返回edxeax结果,但C调用约定期望结果为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. 它应该是UINT64。 rdtsc return low 32 bits in EAX and high 32 bits in EDX register. rdtsc在EAX中返回低32位,在EDX寄存器中返回高32位。 So is you wont result in RAX than perform: 所以你不会导致RAX而不是执行:

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

after executing rdtsc instruction. 执行rdtsc指令后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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