Skip to content

Commit

Permalink
cstool: align assembly code for x86
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Oct 11, 2016
1 parent 8005951 commit 4568869
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions cstool/cstool.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ int main(int argc, char **argv)
uint64_t address = 0;
cs_insn *insn;
cs_err err;
bool x86_arch = false;

if (argc != 3 && argc != 4) {
usage(argv[0]);
Expand Down Expand Up @@ -182,32 +183,38 @@ int main(int argc, char **argv)
}

if (!strcmp(mode, "x16")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_16, &handle);
}

if (!strcmp(mode, "x32")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_32, &handle);
}

if (!strcmp(mode, "x64")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_64, &handle);
}

if (!strcmp(mode, "x16att")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_16, &handle);
if (!err) {
cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
}
}

if (!strcmp(mode,"x32att")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_32, &handle);
if (!err) {
cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
}
}

if (!strcmp(mode,"x64att")) {
x86_arch = true;
err = cs_open(CS_ARCH_X86, CS_MODE_64, &handle);
if (!err) {
cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
Expand Down Expand Up @@ -242,16 +249,22 @@ int main(int argc, char **argv)

count = cs_disasm(handle, assembly, size, address, 0, &insn);
if (count > 0) {
size_t j;
int i;

printf("\n");
for (j = 0; j < count; j++) {
printf("%"PRIx64 "\t", insn[j].address);
for (i = 0; i < insn[j].size; i++) {
printf("%02x", insn[j].bytes[i]);
size_t i;

for (i = 0; i < count; i++) {
int j;
printf("%"PRIx64" ", insn[i].address);
for (j = 0; j < insn[i].size; j++) {
printf("%02x", insn[i].bytes[j]);
}
// X86 instruction size is variable.
// align assembly instruction after the opcode
if (x86_arch) {
for (; j < 16; j++) {
printf(" ");
}
}
printf("\t%s\t%s\n", insn[j].mnemonic, insn[j].op_str);
printf(" %s\t%s\n", insn[i].mnemonic, insn[i].op_str);
}
cs_free(insn, count);
} else {
Expand Down

0 comments on commit 4568869

Please sign in to comment.