我尝试将十六进制转换为十进制,同时移位,并保持符号。遇到问题让我的问题变得简单。变量以正确分解签名的指令。
void disassembleInstr(uint32_t pc, uint32_t instr) {
uint32_t opcode; // opcode field
uint32_t rs, rt, rd; // register specifiers
uint32_t shamt; // shift amount (R-type)
uint32_t funct; // funct field (R-type)
uint32_t uimm; // unsigned version of immediate (I-type)
int32_t simm; // signed version of immediate (I-type)
uint32_t addr; // jump address offset field (J-type)
opcode = instr >> 26;
rs = (instr >> 21) & 0x1f;
rt = (instr >> 16) & 0x1f;
rd = (instr >> 11) & 0x1f;
shamt = (instr >> 6) & 0x1f;
funct = (instr & 0x3f);
uimm = instr & 0xffff;
simm = (instr << 16) >> 16; // shift sign bit to left to
addr = instr & 0x3ffffff; //masked with one
cout << hex << setw(8) << pc << ": ";
switch(opcode) {
case 0x00:
switch(funct) {
case 0x00: cout << "sll " << regNames[rd] << ", " << regNames[rs] << ", " << dec << shamt; break;
case 0x03: cout << "sra " << regNames[rd] << ", " << regNames[rs] << ", " << dec << shamt; break;
case 0x08: cout << "jr " << regNames[rs]; break;
case 0x10: cout << "mfhi " << regNames[rd]; break;
case 0x12: cout << "mflo " << regNames[rd]; break;
case 0x18: cout << "mult " << regNames[rs] << ", " << regNames[rt]; break;
case 0x1a: cout << "div " << regNames[rs] << ", " << regNames[rt]; break;
case 0x21: cout << " addu " << regNames[rd] << ", " << regNames[rs] << ", " << regNames[rt]; break;
case 0x23: cout << " subu " << regNames[rd] << ", " << regNames[rs] << ", " << regNames[rt]; break;
case 0x2a: cout << " slt " << regNames[rd] << ", " << regNames[rs] << ", " << regNames[rt]; break;
default: cout << "unimplemented";
}
break;
case 0x02: cout << "j " << hex << ((pc + 4) & 0xf0000000) + addr * 4; break;
case 0x03: cout << "jal " << hex << ((pc + 4) & 0xf0000000) + addr * 4; break;
// case 0x04: cout << "beq " << regNames[rs] << ", " << regNames[rt] << ", " << + uimm; break;
// case 0x05: cout << "bne " << regNames[rs] << ", " << regNames[rt] << ", " << + uimm; break;
// case 0x09: cout << "addiu " << regNames[rt] << ", " << regNames[rs] << dec << simm; break;
// case 0x0c: cout << "andi " << regNames[rt] << ", " << regNames[rs] << dec << simm; break;
case 0x0f: /* lui */ break;
case 0x1a: cout << "trap " << hex << addr; break;
case 0x23: /* lw */ break;
case 0x2b: /* sw */ break;
default: cout << "unimplemented";
}
cout << endl;
}
以下是我收到错误输出的示例:
400000: j 400114
400004: sw $ra, fffc($sp)
400008: sw $fp, fff8($sp)
40000c: addiu $fp, $sp, 65528
400010: addiu $sp, $fp, 65124
400014: addiu $k1, $zero, 1
这是预期的输出:
400000: j 400114
400004: sw $ra, -4($sp)
400008: sw $fp, -8($sp)
40000c: addiu $fp, $sp, -8
400010: addiu $sp, $fp, -412
400014: addiu $k1, $zero, 1
编辑:已实施建议的新输出:
400000: j 400114
400004: sw $ra, fffffffc($sp)
400008: sw $fp, fffffff8($sp)
40000c: addiu $fp, $sp, -8
400010: addiu $sp, $fp, -412
400014: addiu $k1, $zero, 1
答案 0 :(得分:1)
instr
是无符号类型(uint32_t
),因此向左移动,然后向右移动将简单地清除最重要的位。它不会做你希望的标志扩展。
实际上,根据C标准,将1转换为有符号整数的符号位是未定义的行为。因此,即使instr
是带符号的数字,也不允许使用左移/右移技巧(尽管它适用于任何合理的机器)。
要完成任务而不违反任何规则,请将其替换为:
uimm = instr & 0xffff;
simm = (instr << 16) >> 16; // shift sign bit to left to
用这个:
uimm = instr & 0xffff;
simm = uimm;
if ( simm & 0x8000 )
simm -= 65536;