2

这是原始代码:

#define CPU_PREFETCH(cache_line)            \
{ int* address = (int*) (cache_line);       \
    _asm mov edx, address                   \
    _asm prefetcht0[edx]                    \
}

#define CPU_GET_CYCLES(low)                 \
{                                           \
    _asm    rdtsc                           \
    _asm    mov dword ptr [low], eax        \
}

#define CPU_SYNC                            \
{                                           \
    _asm    mov eax, 0                      \
    _asm    cpuid                           \
}

#define CPU_CACHE_FLUSH(cache_line)         \
{ int* address = (int*) (cache_line);       \
    _asm mov edx, address                   \
    _asm clflush[edx]                       \
    _asm mfence                             \
}

感谢 Jester,我现在有了这个:

#define CPU_PREFETCH(cache_line) \
{ \
    __asm__ __volatile__ ("prefetcht0 %0" : : "m" (*(int*)cache_line)); \
}

#define CPU_GET_CYCLES(low) \
{ \
    __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "%edx"); \
}

#define CPU_SYNC \
{ \
    __asm__ __volatile__ ("cpuid" : : : "%eax", "%ebx", "%ecx", "%edx"); \
}

#define CPU_CACHE_FLUSH(cache_line) \
{ \
    __asm__ ("clflush %0; mfence" : : "m" (*(int*)cache_line)); \
}

显然,gcc 不喜欢clflush 中的 volatile 感谢大家。

我正在尝试使用 gcc 作为 dll 编译Slicing-By-8,以便可以在我的 VB6 应用程序中使用它。

4

2 回答 2

4

使用适当的内联函数会很好。无论如何,这是您的宏版本:

#define CPU_PREFETCH(cache_line) \
{ \
    __asm__ __volatile__ ("prefetcht0 %0" : : "m" (*(int*)cache_line)); \
}

#define CPU_GET_CYCLES(low) \
{ \
    __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "%edx"); \
}

#define CPU_SYNC \
{ \
    __asm__ __volatile__ ("cpuid" : : : "%eax", "%ebx", "%ecx", "%edx"); \
}

#define CPU_CACHE_FLUSH(cache_line) \
{ \
    __asm__ __volatile__ ("clflush %0; mfence" : : "m" (*(int*)cache_line)); \
}
于 2010-12-19T17:17:45.203 回答
3

与其将您的 Intel 语法转换为 AT&T,不如告诉 GCC 您只想编译 Intel 语法?

你可以这样做:

在任何其他装配线之前添加此行:

asm(".intel_syntax noprefix\n");

然后像这样运行 GCC:

gcc -o my_output_file -masm=intel my_src_file.c

感谢BiW Reversingstingduk

于 2010-12-19T08:42:02.253 回答