From 2b9f5903d8f9378ed398c5c32e863468fe56140a Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sun, 15 Jan 2017 13:36:41 -0600 Subject: nqdasm: Factor out disassembly code from command line parsing --- nqdasm.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/nqdasm.c b/nqdasm.c index 0ecc5b1..3245570 100644 --- a/nqdasm.c +++ b/nqdasm.c @@ -39,6 +39,26 @@ static void print_operand(FILE *out, unsigned bits, const struct operand *op) } } +static void disassemble(FILE *in, FILE *out) +{ + unsigned bits; + while (fscanf(in, " %x\n", &bits) > 0) { + const struct mnemonic *m = match_instruction(bits); + if (!m) { + fprintf(out, "\t.word 0x%04x\n", bits); + continue; + } + + fprintf(out, "\t%s", m->mnem); + for (int i = 0; i < 3 && m->operands[i].type; i++) { + if (i) fprintf(out, ", "); + else fprintf(out, "%*s", 8 - (int)strlen(m->mnem), ""); + print_operand(out, bits, m->operands + i); + } + fprintf(out, "\n"); + } +} + static void usage(const char *progname) { printf("usage: %s [-o output] [input]\n", progname); @@ -88,22 +108,7 @@ int main(int argc, char **argv) return 1; } - unsigned bits; - while (fscanf(in, " %x\n", &bits) > 0) { - const struct mnemonic *m = match_instruction(bits); - if (!m) { - fprintf(out, "\t.word 0x%04x\n", bits); - continue; - } - - fprintf(out, "\t%s", m->mnem); - for (int i = 0; i < 3 && m->operands[i].type; i++) { - if (i) fprintf(out, ", "); - else fprintf(out, "%*s", 8 - (int)strlen(m->mnem), ""); - print_operand(out, bits, m->operands + i); - } - fprintf(out, "\n"); - } + disassemble(in, out); fclose(in); fclose(out); -- cgit v1.2.3