summaryrefslogtreecommitdiff
path: root/nqdasm.c
diff options
context:
space:
mode:
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;
+}
+