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!
rdtsc
overwrites eax
and edx
with the parts of the tick counter. Since you loaded ( lea
) the address of timings
onto edx
earlier rdtsc
messes up your program functioning. You could either move rdtsc
upper the command chain or use registers other than eax
and edx
for your program functioning.
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 :).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.