From 9acd6ce40559c59ca55899215aa9d7e781b5a8b7 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sat, 14 Jan 2017 19:07:49 -0600 Subject: nqdasm: Add disassembler --- nqdasm.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 nqdasm.c (limited to 'nqdasm.c') diff --git a/nqdasm.c b/nqdasm.c new file mode 100644 index 0000000..4969f47 --- /dev/null +++ b/nqdasm.c @@ -0,0 +1,43 @@ +#include + +#include "mnemonics.h" + +static const struct mnemonic *match_instruction(unsigned bits) +{ + for (int i = 0; i < MNEM_LAST; i++) { + const struct mnemonic *m = mnemonics + i; + if ((bits & m->mask) == m->bits) return m; + } + return NULL; +} + +static void print_operand(unsigned bits, const struct operand *op) +{ + switch (op->type) { + case REG: printf( "r%d", 7 & (bits >> op->shift)); break; + case REGPTR: printf("@r%d", 7 & (bits >> op->shift)); break; + case IMM: printf( "%d", (uint8_t)(bits >> op->shift)); break; + case PCOFF: printf( "%d", (int8_t)(bits >> op->shift)); break; + } +} + +int main() +{ + unsigned bits; + while (scanf(" %x\n", &bits) > 0) { + const struct mnemonic *m = match_instruction(bits); + if (!m) { + fprintf(stderr, "could not decode instruction 0x%04x\n", bits); + continue; + } + + printf("\t%s", m->mnem); + for (int i = 0; i < 3 && m->operands[i].type; i++) { + printf("%s", i ? ", " : " "); + print_operand(bits, m->operands + i); + } + printf("\n"); + } + return 0; +} + -- cgit v1.2.3