此汇编代码的 C++ 等效项是什么

mao*_*r03 2 c++ assembly rdtsc

我正在尝试弄清楚如何在 C++ 中读取此汇编代码。

这是代码:

unsigned __int64 high_perf_time;
unsigned __int64 *dest = &high_perf_time;
__asm 
{
    _emit 0xf        // these two bytes form the 'rdtsc' asm instruction,
    _emit 0x31       //  available on Pentium I and later.
    mov esi, dest
    mov [esi  ], eax    // lower 32 bits of tsc
    mov [esi+4], edx    // upper 32 bits of tsc
}
__int64 time_s     = (__int64)(high_perf_time / frequency);  // unsigned->sign conversion should be safe here
__int64 time_fract = (__int64)(high_perf_time % frequency);  // unsigned->sign conversion should be safe here
Run Code Online (Sandbox Code Playgroud)

我知道0xf 0x31rdtsc eax, edx,但是什么是mov esi,dest?我怎样才能用 C++ 编写它?

Mar*_*nau 6

如果你想了解这三个指令的作用:

以下三个指令:

    mov esi, dest
    mov [esi  ], eax    // lower 32 bits of tsc
    mov [esi+4], edx    // upper 32 bits of tsc
Run Code Online (Sandbox Code Playgroud)

...相当于以下 C++ 代码:

    uint32_t * esi = (uint32_t *)dest;
    esi[0] = eax;
    esi[1] = edx;
Run Code Online (Sandbox Code Playgroud)

因为 x86 CPU 是“little-endian”,所以这等于:

    *dest = (((__int64_t)edx)<<32) + (uint32_t)eax;
Run Code Online (Sandbox Code Playgroud)

...但是,由于无法使用 C++ 直接访问eax和寄存器,因此此操作必须在汇编代码中完成。edx