diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2017-01-15 13:36:41 -0600 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2017-01-15 23:19:46 -0600 |
commit | 2b9f5903d8f9378ed398c5c32e863468fe56140a (patch) | |
tree | 932f27a1e4a0eb54f2b99679b19c3cac2cf13491 /nqdasm.c | |
parent | df6cda6b5e9e65788bf07358830887aecfecf7b8 (diff) |
nqdasm: Factor out disassembly code from command line parsing
Diffstat (limited to 'nqdasm.c')
-rw-r--r-- | nqdasm.c | 37 |
1 files changed, 21 insertions, 16 deletions
@@ -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); |