假设指令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
还是其他的东西)?
答案 0 :(得分: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]);
答案 1 :(得分:0)
有点转变应该有效。务必检查数据类型。您也可以使用0x01C000“和”数字然后进行比较。