From: Lukas Krickl Date: Mon, 11 Dec 2023 20:06:18 +0000 (+0100) Subject: Labels can now appear before asm directives X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=45e736431a149d90b2d8ce61c79028f14756f144;p=ulas%2F.git Labels can now appear before asm directives --- diff --git a/src/main.c b/src/main.c index 45b5dab..5df27f4 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ #include /** - * TODO: Implement -d flag to disassemble input file + * TODO: Implement -d flag to disassemble input file * TODO: Add symbol file output * TODO: Write documentation * TODO: process defined variables inside defines correctly diff --git a/src/ulas.c b/src/ulas.c index 66e271a..666de32 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -2202,7 +2202,7 @@ void ulas_asmout(FILE *dst, const char *outbuf, unsigned long n) { } if (ulas.address < 0x14C) { - for (int i =0; i < n; i++) { + for (int i = 0; i < n; i++) { ulas.chksm = ulas.chksm - outbuf[i] - 1; } } @@ -2407,6 +2407,7 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { long other_writes = 0; const char *start = line; + const char *instr_start = start; int rc = 0; // read the first token and decide @@ -2418,6 +2419,19 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { return 0; } + // is it a label? + if (ulas_islabelname(ulas.tok.buf, strlen(ulas.tok.buf))) { + instr_start = line; + struct ulas_tok label_tok = {ULAS_INT, {ulas.address}}; + ulas_symbolset(ulas.tok.buf, -1, label_tok, 1); + ulas_tok(&ulas.tok, &line, n); + // is next token empty? + if (ulas_istokend(&ulas.tok)) { + ulas_asmlst(start, outbuf, towrite); + return 0; + } + } + if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) { const char *dirstrs[] = {ULAS_ASMSTR_ORG, ULAS_ASMSTR_SET, ULAS_ASMSTR_BYTE, ULAS_ASMSTR_STR, @@ -2482,24 +2496,8 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { } else { // is regular line in form of [label:] instruction ; comment - // is it a label? - if (ulas_islabelname(ulas.tok.buf, strlen(ulas.tok.buf))) { - const char *prev = line; - struct ulas_tok label_tok = {ULAS_INT, {ulas.address}}; - ulas_symbolset(ulas.tok.buf, -1, label_tok, 1); - - // is next token empty? - if (ulas_tok(&ulas.tok, &line, n) == 0 || - strnlen(ulas.tok.buf, ulas.tok.maxlen) == 0) { - ulas_asmlst(start, outbuf, towrite); - return 0; - } - - line = prev; - } else { - // start over for the next step... - line = start; - } + // start over for the next step... + line = instr_start; int nextwrite = ulas_asminstr(outbuf, ULAS_OUTBUFMAX, &line, n); if (nextwrite == -1) { diff --git a/tests/t0.bin b/tests/t0.bin index 6bf9469..f664486 100644 Binary files a/tests/t0.bin and b/tests/t0.bin differ diff --git a/tests/t0.s b/tests/t0.s index 52676ce..7bd61d4 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -103,3 +103,4 @@ j1: .chksm ld [hl], a +l3: .db 1