3

感谢你们的一些帮助,我的小内联汇编程序几乎就在我想要的地方。但是,现在 rdtsc 命令似乎发生了一些非常奇怪的事情;基本上,我在调用它时遇到了分段错误。

int timings[64*N];
int main(void)
{

    int i;

    __asm__ __volatile__ (  
       "lea edx, [timings] \n\t"  
       "rdtsc \n\t"  
       ".rept 32 \n\t"  
       "mov eax,[edx] \n\t"  
       "inc eax \n\t"  
       "mov dword ptr [edx], eax \n\t"  
       "add edx, 4 \n\t"  
       ".endr \n\t"  
    : 
    : [timings] "m" (*timings)
   );

   for(i=0; i<32; i++)
      printf("%d\n", timings[i]); 

   return 0;
}

省略 rdtsc,然后程序编译并执行它应该执行的操作。但是添加 rdtsc 行会导致分段错误。我在双核机器上运行这些东西并用于编译:gcc -masm=intel test.c

帮助将不胜感激!

4

2 回答 2

5

rdtsc用刻度计数器的部分覆盖eax和。edx由于您将 ( lea)加载timingsedx之前的地址会rdtsc扰乱您的程序运行。您可以移动到命令链的上层,也可以使用除程序功能rdtsc以外的寄存器eaxedx

于 2009-05-27T06:19:56.107 回答
0

Besides the obvious RDTSC writing to EDX problem, you did not submit a clobber list for the asm statement. GCC assumes that every register that you do not list anywhere as output/clobber remains unchanged after the execution of your code, and can use those registers to keep some values across your code. Look up the syntax in the GCC documentation, as I don't remember it :).

于 2009-07-03T09:26:16.347 回答