将int打包到C++; < >我将一些代码从ASM转换成C++,ASM看起来是这样的: mov dword ptr miscStruct, eax

将int打包到C++; < >我将一些代码从ASM转换成C++,ASM看起来是这样的: mov dword ptr miscStruct, eax,c++,bit-fields,cpuid,C++,Bit Fields,Cpuid,结构看起来像: struct miscStruct_s { uLong brandID : 8, chunks : 8, //etc } miscStruct; 在C++中有一个简单的12行填充结构吗? 到目前为止,我正在使用: miscStruct.brandID = Info[0] & 0xff; //Info[0] has the same data as eax in the ASM sample. miscStruct.chunks

结构看起来像:

struct miscStruct_s {
  uLong brandID     : 8,
  chunks            : 8,
  //etc
} miscStruct;
在C++中有一个简单的12行填充结构吗? 到目前为止,我正在使用:

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的方法?

当然,我尝试了“MISCSTRUT= CPUFIN(0)”,但C++不太喜欢这样。(

。我无法编辑结构

memcpy(&miscStruct,&CPUInfo[0],sizeof(struct miscStruct_));

应该有帮助

或者干脆

int *temp = &miscStruct;
*temp = CPUInfo[0];

这里我假设
CPUInfo
的类型是
int
。您需要使用
CPUInfo
数组的数据类型调整
temp
指针类型。只需将结构的内存地址键入数组的类型,并使用指针将值分配到其中。

直译汇编程序指令的主要部分如下:

miscStruct=*(miscStruct_s *)&Info[0];

需要强制转换,因为C++是一种类型安全的语言,而汇编程序不是,但是复制语义是相同的。

MeMCPY(& MISCSTRUT,和CPUFIN(0),SIZEOF(MISCSTRT));我最后根据您的例子做了向后处理。* RealTytPraseCo(& MistCuStt)=信息[0 ]。里利,不管是什么工作,两者之间的主要区别是你认为哪一方是“真实”长度。个人而言,我会选择内存中的对象(<代码> MistCuxt)。如果您以后将
miscStruct
更改为只有一个字符,则会损坏内存。我最后使用了:
*reinterpret_cast(&miscStruct)=Info[0];
miscStruct=*(miscStruct_s *)&Info[0];