From 79ac94fe8c421ea3b1d9028829c28d594249058c Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 24 Feb 2024 12:44:37 +0100 Subject: [PATCH] WIP: added basic db fallback outupt to disasm --- src/main.c | 6 +++++- src/ulas.h | 2 ++ src/uldas.c | 30 +++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 3386281..fff1c6e 100644 --- a/src/main.c +++ b/src/main.c @@ -20,7 +20,7 @@ #define ULAS_VER "0.0.1" // args without value -#define ULAS_OPTS "hvVpd" +#define ULAS_OPTS "hvVpdA" // args with value #define ULAS_OPTS_ARG "o:l:s:i:w:a:" @@ -46,6 +46,7 @@ void ulas_help(void) { ULAS_HELP("s=path", "Symbols file"); ULAS_HELP("i=path", "Add include search path"); ULAS_HELP("a=initial-address", "Initial starting address"); + ULAS_HELP("A", "Print addresses in disassembler mode"); ULAS_HELP("d", "Disassemble a file"); ULAS_HELP("w=warning", "Toggle warnings: a=all, o=overflow"); } @@ -97,6 +98,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) { case 'd': cfg->disas = 1; break; + case 'A': + cfg->print_addrs = 1; + break; case '?': break; default: diff --git a/src/ulas.h b/src/ulas.h index c0c01b1..fe29e7f 100644 --- a/src/ulas.h +++ b/src/ulas.h @@ -134,6 +134,8 @@ struct ulas_config { unsigned int org; + unsigned int print_addrs; + // all include search paths char **incpaths; unsigned int incpathslen; diff --git a/src/uldas.c b/src/uldas.c index 4e92ff2..c2339d4 100644 --- a/src/uldas.c +++ b/src/uldas.c @@ -8,7 +8,10 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr, return; } - fprintf(dst, "%s ", instr->name); + if (ulascfg.print_addrs) { + fprintf(dst, "%08x ", ulas.address); + } + fprintf(dst, " %s ", instr->name); int bi = 0; for (int i = 0; instr->tokens[i]; i++) { int dat = instr->tokens[i]; @@ -23,9 +26,9 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr, default: { const char *reg = ulas_asmregstr(dat); if (reg) { - printf("%s", reg); + printf("%s", reg); } else { - printf("%c", dat); + printf("%c", dat); } break; } @@ -35,6 +38,19 @@ void ulas_dasm_instr_fout(FILE *src, FILE *dst, const struct ulas_instr *instr, fprintf(dst, "\n"); } +// fallback if no instruction was found +void ulas_dasm_db_fout(FILE *src, FILE *dst, const char *buf, + unsigned long read) { + ulas.address++; + if (ulas.pass != ULAS_PASS_FINAL) { + return; + } + if (ulascfg.print_addrs) { + fprintf(dst, "%08x ", ulas.address); + } + fprintf(dst, ".db %x\n", buf[0] & 0xFF); +} + // returns > 1 if instruction was found, and 0 if not // the integer returned is the amount of bytes this instruction consumed int ulas_dasm_instr_check(FILE *src, FILE *dst, const struct ulas_instr *instr, @@ -63,20 +79,22 @@ int ulas_dasm_instr_check(FILE *src, FILE *dst, const struct ulas_instr *instr, case ULAS_E16: case ULAS_A16: // need 2 bytes here - if (bi + 1 >= read) { + if (bi + 1 < read) { goto fail; } bi += 2; break; default: - if (buf[bi++] != dat) { + if (buf[bi] != dat) { goto fail; } + bi++; break; } } ulas_dasm_instr_fout(src, dst, instr, buf, read); + ulas.address += i; return i; fail: return 0; @@ -115,6 +133,8 @@ int ulas_dasm_next(FILE *src, FILE *dst) { } } + ulas_dasm_db_fout(src, dst, buf, read); + fseek(src, srctell + 1, 0); return 1; } -- 2.30.2