Search code examples
cassemblyxcode4llvm

LLVM complains about assembler error "Unexpected token in memory operand"


I'm doing a study assignment to measure memory access time on my machine. To determine the clock cycles on our machines, we have been given the following C snippet:

static inline void getcyclecount(uint64_t* cycles)
{
    __asm __volatile(
                     "cpuid            # force all previous instruction to complete\n\t"
                     "rdtsc            # TSC ->  edx:eax \n\t"
                     "movl %%edx, 4(0) # store edx\n\t"
                     "movl %%eax, 0(0) # store eax\n\t"
                     : : "r"(cycles) : "eax", "ebx", "ecx", "edx");
}

However, when I try to compile this (XCode 4, using "Apple LLVM Compiler 2.1"), it results twice in the error "Unexpected token in memory operand" at the "\t" of the rdtsc resp. first movl instruction line.

I know basic assembler, but have no clue about the C inline assembler format.

Does anyone of you know what could be the issue with this code?

Thanks!


Solution

  • Assuming this is GCC inline assembly syntax, you're missing a % in the memory operand:

    __asm __volatile(
                     "cpuid            # force all previous instruction to complete\n\t"
                     "rdtsc            # TSC ->  edx:eax \n\t"
                     "movl %%edx, 4(%0) # store edx\n\t"
                     "movl %%eax, 0(%0) # store eax\n\t"
                     : : "r"(cycles) : "eax", "ebx", "ecx", "edx");