我正在将一些代码从ASM转换为C++,ASM看起来像这样:
mov dword ptr miscStruct, eax
Struct看起来像:
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 [0];”。但是C++并不喜欢那样。 :(
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int'
..而且我无法编辑struct 。
最佳答案
汇编程序指令的字面翻译是这样的:
miscStruct=*(miscStruct_s *)&Info[0];
因为C++是一种类型安全的语言,而汇编程序不是,但是复制语义是相同的,因此需要强制类型转换。
关于c++ - 将int打包到C++中的位域中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6881441/