summaryrefslogtreecommitdiff
path: root/nqdasm.c
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-01-14 19:07:49 -0600
committerBobby Bingham <koorogi@koorogi.info>2017-01-15 23:19:28 -0600
commit9acd6ce40559c59ca55899215aa9d7e781b5a8b7 (patch)
treef787d6c61ac2fd5c16f9219bc652f076ac8fdf39 /nqdasm.c
parentf91057644c1f1813bf1373b5158e6b658210d570 (diff)
nqdasm: Add disassembler
Diffstat (limited to 'nqdasm.c')
-rw-r--r--nqdasm.c43
1 files changed, 43 insertions, 0 deletions
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 <stdio.h>
+
+#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;
+}
+