Assembler instruction: rdtsc

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

Could someone help me understand the assembler given in https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

It goes like this:

uint64_t msr; asm volatile ( "rdtsc\n\t"    // Returns the time in EDX:EAX.                "shl $32, %%rdx\n\t"  // Shift the upper bits left.                "or %%rdx, %0"        // 'Or' in the lower bits.                : "=a" (msr)                :                : "rdx");

How is it different from:

uint64_t msr; asm volatile ( "rdtsc\n\t"                : "=a" (msr));

Why do we need shift and or operations and what does rdx at the end do?

EDIT: added what is still unclear to the original question.

  • What does "\n\t" do?
  • What do ":" do?
    • delimiters output/input/clobbers...
  • Is rdx at the end equal to 0?

Just to recap. First line loads the timestamp in registers eax and edx. Second line shifts the value in eax and stores in rdx. Third line ors the value in edx with the value in rdx and saves it in rdx. Fourth line assigns the value in rdx to my variable. The last line sets rdx to 0.

  • Why are the first three lines without ":"?
    • They are a template. First line with ":" is output, second is optional input and third one is optional list of clobbers (changed registers).
  • Is a actually eax and d - edx? Is this hard-coded?

Thanks again! :)

EDIT2: Answered some of my questions...

回答1:

uint64_t msr; asm volatile ( "rdtsc\n\t"    // Returns the time in EDX:EAX.                "shl $32, %%rdx\n\t"  // Shift the upper bits left.                "or %%rdx, %0"        // 'Or' in the lower bits.                : "=a" (msr)                :                : "rdx");

Because the rdtsc instruction returns it's results in edx and eax, instead of a straight 64-bit register on a 64-bit machine (See the intel system's programming manual for more information; it's an x86 instruction), the 2nd instruction shifts the rdx register to the left 32 bits so that edx will be on the upper 32 bits instead of the lower 32 bits.
"=a" (msr) will move the contents of eax into msr (the %0), i.e. into the lower 32 bits of it, so in total you have edx (higher 32 bits) and eax (lower 32 bits) into rdx which is msr.
rdx is a clobber which will represent the msr C variable.

It's similar to doing the following in C:

static inline uint64_t rdtsc(void) {     uint32_t eax, edx;     asm volatile("rdtsc\n\t", "=a" (eax), "=d" (edx));     return (uint64_t)eax | (uint64_t)edx << 32; }

And:

uint64_t msr; asm volatile ( "rdtsc\n\t"                : "=a" (msr));

This one, will just give you the contents of eax into msr.

EDIT:

1) "\n\t" is for the generated assembly to look clearer and error-free, so that you don't end up with things like movl $1, %eaxmovl $2, %ebx
2) Is rdx at the end equal to 0? The left shift does this, it removes the bits that are already in rdx.
3) Is a actually eax and d - edx? Is this hard-coded? Yes, there is a table that describes what characters represents what register, e.g. "D" would be rdi, "c" would be ecx, ...



回答2:

rdtsc returns timestamp in a pair of 32-bit registers (EDX and EAX). First snippet combines them into single 64-bit register (RDX) which is mapped to msr variable.

Second snippet is the wrong one. I'm not sure about what will happen: either it won't be compiled at all, or only part of msr variable will be updated.



转载请标明出处:Assembler instruction: rdtsc
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!