From df81112b8369eeca5788a6f28c6b6b85ca911a95 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 1 Jun 2020 15:15:17 -0400 Subject: Did some more refactoring to the assembler. - Refactored the symbol, and fixup table to now use a linked list - Added support for local symbols to the assembler. - Rewrote SuBEditor, and SuBAsm to use local symbols. --- asmmon.c | 138 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 76 insertions(+), 62 deletions(-) (limited to 'asmmon.c') diff --git a/asmmon.c b/asmmon.c index f9fbd7a..585d443 100644 --- a/asmmon.c +++ b/asmmon.c @@ -16,9 +16,8 @@ char *comment[MAX_TOK]; uint16_t incl[MAX_TOK]; line *lines; line *last_line; -struct line tln[MAX_TOK]; -struct symbol *symbols[MAX_TOK]; -struct fixup *fixups[MAX_TOK]; +symbol *symbols = 0; +fixup *fixups = 0; static char tstr[2048]; @@ -112,8 +111,8 @@ char *showbits(uint64_t value, uint8_t bitnum, uint8_t dbg) { } void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, uint8_t dbg) { - line *s = find_line(start, dbg); - line *e = find_line(end, dbg); + line *s = (!all) ? find_line(start, dbg) : lines ; + line *e = (!all) ? find_line( end, dbg) : last_line; uint8_t j = 0; uint8_t flags = 0; uint8_t isstr; @@ -127,22 +126,18 @@ void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, u char mne_lower[4]; char ch[6]; - if (all) { - s = lines; - e = last_line; - } - do { - token *t = s->tok; + token *tok = s->tok; + token *t; uint8_t am = 0xFF; uint8_t rs = 0xFF; if (dbg) { printf("list(): "); } if (ln) { - printf("%u\t", s->linenum); + printf("%u\t\t", s->linenum); } else if (addr) { - printf("$%"PRIX64":\t", s->addr); + printf("$%"PRIX64":\t\t", s->addr); } spaces = s->sspace; tabs = s->stab; @@ -156,10 +151,10 @@ void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, u tabs--; } } - for (; t && t->id != TOK_COMMENT; t = t->next) { + for (; tok && tok->id != TOK_COMMENT && tok >= t; tok = tok->next) { + t = tok; switch (t->id) { - case TOK_DIR : printf(".%s ", dir_t[t->type]); break; - case TOK_STRING: printf("\"%s\"", t->str) ; break; + case TOK_DIR : printf(".%s ", dir_t[t->type] ); break; case TOK_OPCODE: for (; j < 3; j++) { mne_lower[j] = tolower(mne[t->byte][j]); @@ -169,10 +164,11 @@ void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, u printf("%s", mne_lower); am = t->type; t = t->next; - if (t->id == TOK_RS) { + if (t && t->id == TOK_RS) { rs = t->type; printf("%s", rs_t[t->type]); } + putchar(' '); switch (am) { case IMM : putchar('#'); break; case IND : @@ -182,45 +178,65 @@ void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, u break; case TOK_SYM: case TOK_LABEL: - printf("%s", symbols[t->word]->name); + if (t->type == 1) { + putchar('@'); + } + printf("%s", (t->sym) ? t->sym->name : "unknown"); if (t->id == TOK_LABEL) { putchar(':'); } else if (t == s->tok && t->id == TOK_SYM) { printf(" = "); } break; - case TOK_HEX: printf("$%"PRIX64, t->qword); break; - case TOK_DEC: printf( "%"PRIu64, t->qword); break; - case TOK_BIN: - if (rs != 0xFF) { - bitnum = (rs << 3); - } else { - opsize += (t->qword <= 0x000000FF) + 0; - opsize += (t->qword > 0x000000FF) + 1; - opsize += (t->qword > 0x0000FFFF) + 2; - opsize += (t->qword > 0xFFFFFFFF) + 3; - if (opsize) { - bitnum = bitsize[opsize-1]; + case TOK_HEX: + if (t->id == TOK_HEX) { + printf("$%02"PRIX64, t->qword); + } else if (t->id == TOK_DEC) { + case TOK_DEC: printf( "%"PRIu64, t->qword); + } else if (t->id == TOK_BIN) { + case TOK_BIN: if (rs != 0xFF) { + bitnum = (rs << 3); + } else { + opsize += (t->qword <= 0x000000FF) + 0; + opsize += (t->qword > 0x000000FF) + 1; + opsize += (t->qword > 0x0000FFFF) + 2; + opsize += (t->qword > 0xFFFFFFFF) + 3; + if (opsize) { + bitnum = bitsize[opsize-1]; + } } + printf("%%%s", showbits(t->qword, bitnum, dbg)); + bitnum = 0; + opsize = 0; + } else if (t->id == TOK_STRING) { + case TOK_STRING: + printf("\"%s\"", (t->str) ? t->str : ""); + } else if (t->id == TOK_CHAR) { + case TOK_CHAR: j = 0; + switch (t->byte) { + default : ch[j++] = t->byte; break; + case '\n': ch[j++] = '\\'; ch[j++] = 'n' ; break; + case '\r': ch[j++] = '\\'; ch[j++] = 'r' ; break; + case '\b': ch[j++] = '\\'; ch[j++] = 'b' ; break; + case '\\': ch[j++] = '\\'; ch[j++] = '\\'; break; + case '\'': ch[j++] = '\\'; ch[j++] = '\''; break; + case '\"': ch[j++] = '\\'; ch[j++] = '\"'; break; + } + ch[j] = '\0'; + j = 0; + printf("\'%s\'", ch); } - printf("%%%s", showbits(t->qword, bitnum, dbg)); - bitnum = 0; - opsize = 0; - break; - case TOK_CHAR: - j = 0; - switch (t->byte) { - default : ch[j++] = t->byte; break; - case '\n': ch[j++] = '\\'; ch[j++] = 'n' ; break; - case '\r': ch[j++] = '\\'; ch[j++] = 'r' ; break; - case '\b': ch[j++] = '\\'; ch[j++] = 'b' ; break; - case '\\': ch[j++] = '\\'; ch[j++] = '\\'; break; - case '\'': ch[j++] = '\\'; ch[j++] = '\''; break; - case '\"': ch[j++] = '\\'; ch[j++] = '\"'; break; + if (t->next) { + switch (t->next->id) { + case TOK_STRING: + case TOK_HEX: + case TOK_BIN: + case TOK_DEC: + case TOK_CHAR: + printf(", "); + break; + } } - ch[j] = '\0'; - j = 0; - printf("\'%s\'", ch); break; case TOK_EXPR: switch (t->type) { @@ -269,8 +285,8 @@ void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, u tabs--; } } - if (t->id == TOK_COMMENT) { - printf(";%s", t->str); + if (tok && tok->id == TOK_COMMENT) { + printf(";%s", (tok->str) ? tok->str : ""); } puts(""); s = s->next; @@ -308,7 +324,6 @@ int asmmon(const char *fn) { uint8_t dbg = 0; uint8_t isinclude = 0; uint16_t tmp_lineidx = 0; - init_symbol(); while (!done) { char *cmd; char *arg = malloc(sizeof(char *)*128); @@ -358,9 +373,7 @@ int asmmon(const char *fn) { if (fp2 != NULL) { fclose(fp2); } - if (lines) { - free_lines(); - } + cleanup(); return 2; case 0x02: viewmem(address); @@ -428,15 +441,16 @@ int asmmon(const char *fn) { } break; case 0x08: - if (!inc_file) { + if (!inc_count) { printf("Assembling %s\n", (strcasecmp(fn, "stdin")) ? fn : "typed in program."); + } + if (!inc_file) { + puts("Now assembling."); + bc.progsize = 0; + bc.datasize = 0; assemble(lines, &bc, dbg); progsize = bc.progsize; datasize = bc.datasize; - printf("Finished assembling %s\n", (strcasecmp(fn, "stdin")) ? fn : "typed in program."); - printf("%"PRIu64"/$%"PRIX64" bytes of program code.\n", progsize, progsize); - printf("%"PRIu64"/$%"PRIX64" bytes of data.\n", datasize, datasize); - putchar('\n'); } isinclude = (inc_file != 0); if (inc_file) { @@ -445,7 +459,6 @@ int asmmon(const char *fn) { sprintf(fn2, "%s/%s", path, string[incl[inc_count]]); isinclude = (inc_file != 0); inc_file--; - inc_count++; if (inc_file && fp2 != NULL) { fclose(fp2); } @@ -453,8 +466,11 @@ int asmmon(const char *fn) { if (fp2 == NULL) { free(path); fclose(fp); + cleanup(); return 2; } + printf("Including %s\n", string[incl[inc_count++]]); + inc_count++; } if (!isinclude) { puts("Finished assembling."); @@ -517,8 +533,6 @@ int asmmon(const char *fn) { if (fp2 != NULL) { fclose(fp2); } - if (lines) { - free_lines(); - } + cleanup(); return 0; } -- cgit v1.2.3-13-gbd6f