2

我正在将一些代码从 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[0];” 但不幸的是,C++ 不喜欢这样。:(

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int'

..而且我无法编辑 struct

4

2 回答 2

1

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

应该有帮助。

或者干脆

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

Here i have assumed that the type of CPUInfo is int . You need to adjust the temp pointer type with the datatype of the CPUInfo array. Simply typecast the memory address for the structure to the type of the array and assign the value into there using the pointer.

于 2011-07-30T06:07:46.690 回答
1

The literal translation of the assembler instruction is this:

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

The casts are needed because C++ is a type-safe language, whereas assembler isn't, but the copying semantics are identical.

于 2011-07-30T06:10:24.357 回答