0

假设一条指令aw是由 32 位结构定义的代码 010,如下所示:

bits 31-25  unused (all 0s)
bits 24-22: code
bits 21-19: argument 1
bits 18-16: argument 2
bits 15-0:  offset (a 16-bit, 2's complement number with a range of -32768 to 32767)

给定数字 8454151,我如何确定代码是否为aw

我试图将数字移动 22 位,例如 8454151 >> 22,但我一直得到 0。关于如何获取代码的位信息的任何想法(以检查它是否是aw或其他)?

4

2 回答 2

2

如果您只需要验证一条指令是否属于某个操作,则需要最少周期的代码如下:

const uint32_t mask = 7 << 22;    // Shift 3 set bits by 22 in order to build 
                                  // a mask where bits 22-24 are set.
const uint32_t inst_aw = 2 << 22; // Shift the opcode by 22 to build a comparable value

uint32_t instruction = ...;       // Your instruction word

if ((instruction & mask) == inst_aw) {
  // Do your thing
}

无论如何,如果您必须构建诸如“指令解码器”或“解释器”之类的东西,我建议使用由指令代码索引的指令名称(或函数指针)的查找表:

/*! \brief Instruction names lookup table. Make sure it has always 8 entries 
           in order to avoid access beyond the array limits */
const char *instruction_names[] = {
  /* 000 */ "Unknown instruction 000",
  /* 001 */ "Unknown instruction 001",
  /* 010 */ "AW",
  ...
  /* 111 */ "Unknown instruction 111"
};


uint32_t instruction = ...;
unsigned int opcode = (instruction >> 22) & 0x07; // Retrieving the opcode by shifting right 22 
                                                  // times and mask out all bit except the last 3

printf("Instruction is: %s", instruction_names[opcode]);
于 2013-01-25T07:43:35.610 回答
0

位移应该起作用。请务必检查数据类型。您也可以将数字与 0x01C000 “和”,然后进行比较。

于 2013-01-25T07:21:54.350 回答