QEMU edu PCI 设备中的奇怪寄存器十六进制值 0x010000edu
Weird register hex value 0x010000edu in QEMU edu PCI device
在QEMU edu PCI设备的sourcecode中,用C编写,进行了以下赋值:
val = 0x010000edu;
(在documentation中也有说明)。
这应该是存储在偏移量为0x00
(第一个)的设备内部寄存器中的值。 u
不是十六进制值。实际上从OS读取寄存器的内容,只显示0x010000ed
。
上面的赋值val = 0x010000edu
好像没有意义。那么,为什么会这样呢?
在测试程序中,我可以:
# include <stdio.h>
# include <inttypes.h>
void main() {
uint32_t testvalue = 0x010000edu;
printf("%08x\n", testvalue);
}
编译成功,001000ed
打印在屏幕上。但是:
uint32_t testvalue = 0x010u00ed;
会引发编译器错误:
test.c: In function ‘main’:
test.c:9:19: error: invalid suffix "u00ed" on integer constant
9 | uint32_t testvalue = 0x010u00ed;
| ^~~~~~~~~~
我不明白的是为什么 u
在这两种情况下都没有被拒绝,因为它不是 hex
数字。
C语言中的整数常量可以有一个后缀,即determines/changes整数字面量的类型。参见 cppreference integer constant。
后缀 u
或 U
与十六进制整数常量用于将文字的类型更改为 unsigned int
unsigned long int
或 unsigned long long int
。当您想指定要无符号的文字类型时,使用后缀 u
,在这种情况下,0x010000ed
可以是 int
或 long
,具体取决于体系结构,这将分配给 uint32_t
变量时进行有符号到无符号的转换。或者 someone has to follow rules,或者它可以作为该值无符号的文档。
@编辑它在 C 中是“整数常量”,在 C++ 中是“文字”
在QEMU edu PCI设备的sourcecode中,用C编写,进行了以下赋值:
val = 0x010000edu;
(在documentation中也有说明)。
这应该是存储在偏移量为0x00
(第一个)的设备内部寄存器中的值。 u
不是十六进制值。实际上从OS读取寄存器的内容,只显示0x010000ed
。
上面的赋值val = 0x010000edu
好像没有意义。那么,为什么会这样呢?
在测试程序中,我可以:
# include <stdio.h>
# include <inttypes.h>
void main() {
uint32_t testvalue = 0x010000edu;
printf("%08x\n", testvalue);
}
编译成功,001000ed
打印在屏幕上。但是:
uint32_t testvalue = 0x010u00ed;
会引发编译器错误:
test.c: In function ‘main’:
test.c:9:19: error: invalid suffix "u00ed" on integer constant
9 | uint32_t testvalue = 0x010u00ed;
| ^~~~~~~~~~
我不明白的是为什么 u
在这两种情况下都没有被拒绝,因为它不是 hex
数字。
C语言中的整数常量可以有一个后缀,即determines/changes整数字面量的类型。参见 cppreference integer constant。
后缀 u
或 U
与十六进制整数常量用于将文字的类型更改为 unsigned int
unsigned long int
或 unsigned long long int
。当您想指定要无符号的文字类型时,使用后缀 u
,在这种情况下,0x010000ed
可以是 int
或 long
,具体取决于体系结构,这将分配给 uint32_t
变量时进行有符号到无符号的转换。或者 someone has to follow rules,或者它可以作为该值无符号的文档。
@编辑它在 C 中是“整数常量”,在 C++ 中是“文字”