我正在制作Intel 8080的模拟器。其中一个操作码需要通过组合b和c寄存器(均为1字节)来获得16位地址。我有一个将寄存器相邻排列的结构体。我组合这两个寄存器的方式是:
我认为我可以将它们用
另一种尝试的方法是创建一个简短的内容,并逐个设置这2个字节。
有没有更好/更安全的方法来实现我想做的事情?
using byte = char;
struct {
... code
byte b;
byte c;
... code
} state;
...somewhere in code
// memory is an array of byte with a size of 65535
memory[*reinterpret_cast<short*>(&state.b)]
我认为我可以将它们用
OR
连接起来,但这样做行不通。short address = state.b | state.c
另一种尝试的方法是创建一个简短的内容,并逐个设置这2个字节。
short address;
*reinterpret_cast<byte*>(&address) = state.b;
*(reinterpret_cast<byte*>(&address) + 1) = state.c;
有没有更好/更安全的方法来实现我想做的事情?
short
将会被适当地对齐,所以我在那里看不出问题。 - Sam Varshavchik