views:

795

answers:

2

Hi

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!

+5  A: 

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.

sharptooth
Cant believe how stupid I am. Completely ignored that the high part of the time stamp counter is stored in edx. Thanks so much for helping me out here, it is just to early in the morning ;)
A: 

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 input/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 :).

stormsoul