简体   繁体   English

C内联汇编中的RDTSC导致分段错误!

[英]RDTSC in C inline assembly causes segmentation fault!

Thanks to some help of you guys I got my little inline assembler program almost there where I want it to have. 感谢你们的帮助,我得到了我的小内联汇编程序几乎就在我想要的地方。 However, there now seems to happen something very strange with the rdtsc command; 然而,现在似乎发生了一些与rdtsc命令非常奇怪的事情; basically, I get a segmentation fault when calling it. 基本上,我在调用它时会出现分段错误。

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;
}

Leaving out the rdtsc, then the program compiles and it does what it should do. 离开rdtsc,然后程序编译,它做它应该做的事情。 But adding the rdtsc line causes the segmentation fault. 但添加rdtsc行会导致分段错误。 I am running this stuff on a dual core machine and use for compilation: gcc -masm=intel test.c 我在双核机器上运行这些东西并用于编译:gcc -masm = intel test.c

Help would be appreciated! 帮助将不胜感激!

rdtsc overwrites eax and edx with the parts of the tick counter. rdtsc用tick计数器的部分覆盖eaxedx Since you loaded ( lea ) the address of timings onto edx earlier rdtsc messes up your program functioning. 由于您将timings地址( lea )加载到edx之前,因此rdtsc您的程序功能。 You could either move rdtsc upper the command chain or use registers other than eax and edx for your program functioning. 您可以将rdtsc移到命令链的上方,也可以使用eaxedx以外的寄存器来执行程序。

Besides the obvious RDTSC writing to EDX problem, you did not submit a clobber list for the asm statement. 除了明显的RDTSC写入EDX问题,你没有提交asm语句的clobber列表。 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. GCC假定您在代码执行后未在任何位置列出的每个寄存器都保持不变,并且可以使用这些寄存器在代码中保留一些值。 Look up the syntax in the GCC documentation, as I don't remember it :). 查看GCC文档中的语法,因为我不记得了:)。

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

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