C 内联汇编中的 RDTSC 导致分段错误!
感谢你们的帮助,我的小内联汇编程序几乎就在我想要的地方。 然而,现在 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
帮助将不胜感激!
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; 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. But adding the rdtsc line causes the segmentation fault. I am running this stuff on a dual core machine and use for compilation: gcc -masm=intel test.c
Help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
rdtsc
用滴答计数器的部分覆盖eax
和edx
。 由于您之前将timings
的地址加载 (lea
) 到edx
上,rdtsc
会扰乱您的程序功能。 您可以将rdtsc
移至命令链的上层,或者使用eax
和edx
以外的寄存器来实现程序功能。rdtsc
overwriteseax
andedx
with the parts of the tick counter. Since you loaded (lea
) the address oftimings
ontoedx
earlierrdtsc
messes up your program functioning. You could either moverdtsc
upper the command chain or use registers other thaneax
andedx
for your program functioning.除了明显的 RDTSC 写入 EDX 问题之外,您没有提交 asm 语句的破坏列表。 GCC 假定您未在任何地方列出作为输出/破坏的每个寄存器在代码执行后保持不变,并且可以使用这些寄存器在代码中保留某些值。 在 GCC 文档中查找语法,因为我不记得了:)。
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 :).