Skip to content

Commit

Permalink
x86: print negative number in memory reference address (more friendly…
Browse files Browse the repository at this point in the history
…). issue reported by @pancake
  • Loading branch information
aquynh committed Nov 2, 2014
1 parent c2d353c commit b87f855
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
29 changes: 22 additions & 7 deletions arch/X86/X86ATTInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,29 @@ static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
int64_t DispVal = MCOperand_getImm(DispSpec);
if (MI->csh->detail)
MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;
if (DispVal || (!MCOperand_getReg(IndexReg) && !MCOperand_getReg(BaseReg))) {
if (DispVal < 0) {
SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
if (DispVal) {
if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {
if (DispVal < 0) {
if (DispVal < -HEX_THRESHOLD)
SStream_concat(O, " -0x%"PRIx64, -DispVal);
else
SStream_concat(O, " -%"PRIu64, -DispVal);
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, "0x%"PRIx64, DispVal);
else
SStream_concat(O, "%"PRIu64, DispVal);
}
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, "0x%"PRIx64, DispVal);
else
SStream_concat(O, "%"PRIu64, DispVal);
// only immediate as address of memory
if (DispVal < 0) {
SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, "0x%"PRIx64, DispVal);
else
SStream_concat(O, "%"PRIu64, DispVal);
}
}
}
}
Expand Down
41 changes: 27 additions & 14 deletions arch/X86/X86IntelInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,26 +617,39 @@ static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
NeedPlus = true;
}

if (!MCOperand_isImm(DispSpec)) {
if (NeedPlus)
SStream_concat0(O, " + ");
} else {
if (MCOperand_isImm(DispSpec)) {
int64_t DispVal = MCOperand_getImm(DispSpec);
if (MI->csh->detail)
MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;
if (DispVal || (!MCOperand_getReg(IndexReg) && !MCOperand_getReg(BaseReg))) {
if (DispVal) {
if (NeedPlus) {
SStream_concat0(O, " + ");
}

if (DispVal < 0) {
SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
if (DispVal < 0) {
if (DispVal < -HEX_THRESHOLD)
SStream_concat(O, " - 0x%"PRIx64, -DispVal);
else
SStream_concat(O, " - %"PRIu64, -DispVal);
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, " + 0x%"PRIx64, DispVal);
else
SStream_concat(O, " + %"PRIu64, DispVal);
}
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, "0x%"PRIx64, DispVal);
else
SStream_concat(O, "%"PRIu64, DispVal);
// memory reference to an immediate address
if (DispVal < 0) {
SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
} else {
if (DispVal > HEX_THRESHOLD)
SStream_concat(O, "0x%"PRIx64, DispVal);
else
SStream_concat(O, "%"PRIu64, DispVal);
}
}

} else {
// DispVal = 0
if (!NeedPlus) // [0]
SStream_concat0(O, "0");
}
}

Expand Down
1 change: 1 addition & 0 deletions suite/x86odd.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CODE32_MEMREF += b"\xa1\x23\x01\x00\x00"
CODE32_MEMREF += b"\xa1\x00\x00\x00\x00"
CODE32_MEMREF += b"\xa1\xdd\xfe\xff\xff"
CODE32_MEMREF += b"\x8b\x04\x91"


_python3 = sys.version_info.major == 3
Expand Down

0 comments on commit b87f855

Please sign in to comment.