Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ RDTSCP与指令顺序_C++_Assembly_X86_Inline Assembly_Osdev - Fatal编程技术网

C++ RDTSCP与指令顺序

C++ RDTSCP与指令顺序,c++,assembly,x86,inline-assembly,osdev,C++,Assembly,X86,Inline Assembly,Osdev,我正在使用rdtscp指令读取ecx寄存器以检测cpu和numa节点id(我正在开发一个操作系统) 代码如下所示 inline static long get(unsigned char *node_id = 0, unsigned char *cpu_id = 0) { unsigned int p; __asm__ __volatile__("rdtscp\n" : "=c" (p) : : "memory"); if (node_id) { *no

我正在使用
rdtscp
指令读取
ecx
寄存器以检测cpu和numa节点id(我正在开发一个操作系统)

代码如下所示

inline static long get(unsigned char *node_id = 0, unsigned char *cpu_id = 0)
{
    unsigned int p;
    __asm__ __volatile__("rdtscp\n" : "=c" (p) : : "memory");

    if (node_id) {
        *node_id = p >> 12;
    }

    if (cpu_id) {
        *cpu_id = p & 0xfff;
    }

    return 0;
}
在使用这个函数时,我有一个不可理解的行为:CPU告诉我很多异常(页面错误、一般保护错误等)。这向我表明,没有读取cpu或节点id,但如果我记录id,则一切正常,不会出现异常

因此,在代码中:

// ...
unsigned char cpu, numa;
get(&numa, &cpu);
// use cpu and numa id creates exception
但是

/。。。
无符号字符cpu,numa;
获取(&numa,&cpu);

打印(cpu);// 告诉编译器寄存器eax和edx被阻塞。将它们添加到:


看起来你没有告诉编译器eax和edx也被破坏了。是的,谢谢,这就解决了它!让我快速写一个答案……你为什么在这里重击“记忆”?此指令不会阻塞内存。我猜您在这里假设已指定英特尔方言(
-masm=Intel
);可能值得明确指出,否则这将不起作用。
// ...
unsigned char cpu, numa;
get(&numa, &cpu);
print(cpu); // <--- this makes cpu reading ok?
// use cpu and numa id is ok
__asm__ __volatile__("rdtscp\n" : "=c" (p) : : "memory", "eax", "edx");