我正在将一些代码从ASM转换为C++,ASM看起来很简单:
mov dword ptr miscStruct, eax这座建筑看起来像:
struct miscStruct_s {
uLong brandID : 8,
chunks : 8,
//etc
} miscStruct;有一种简单的一-两行方式来填充C++中的结构体吗?到目前为止我使用的是:
miscStruct.brandID = Info[0] & 0xff; //Info[0] has the same data as eax in the ASM sample.
miscStruct.chunks = ((Info[0] >> 8) & 0xff);这一切都很好,但我必须填充大约9-10个这样的位域结构,其中一些有30多个字段。因此,这样做的结果是将10行代码转化为100+,这显然不是很好。
那么,在C++中是否有一种简单、干净的复制ASM的方法呢?
当然,我尝试了"miscStruct =CPUInfo“,但不幸的是,C++并不喜欢这一点。:(
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int'..And我无法编辑结构。
发布于 2011-07-30 06:10:24
汇编程序指令的直译如下:
miscStruct=*(miscStruct_s *)&Info[0];之所以需要转换,是因为C++是一种类型安全的语言,而汇编程序不是,但是复制语义是相同的。
发布于 2011-07-30 06:07:46
memcpy (&miscStruct, &CPUInfo[0], sizeof (struct miscStruct_s));
应该能帮上忙。
或者简单的
int *temp = &miscStruct;
*temp = CPUInfo[0];在这里,我假设CPUInfo的类型是int。您需要使用temp数组的数据类型来调整CPUInfo指针类型。只需将结构的内存地址键入数组的类型,并使用指针将值赋值到其中。
https://stackoverflow.com/questions/6881441
复制相似问题