Labels can now appear before asm directives
authorLukas Krickl <lukas@krickl.dev>
Mon, 11 Dec 2023 20:06:18 +0000 (21:06 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 11 Dec 2023 20:06:18 +0000 (21:06 +0100)
src/main.c
src/ulas.c
tests/t0.bin
tests/t0.s

index 45b5dabee93bfc7917fe1eed1bb5d115a7efb57e..5df27f4db579f1d416564d49ed7f9b5504a202cc 100644 (file)
@@ -7,7 +7,7 @@
 #include <getopt.h>
 
 /**
- * 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
index 66e271aff3f31e7e979221c3ec55412176e8a414..666de32518f2afc857bd55366a3e4ca408ba9b67 100644 (file)
@@ -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) {
index 6bf94696b3478dd994192e42f8e77c6226e625e2..f6644864d8c80c98fe0e1d78496dc41fd0905fa9 100644 (file)
Binary files a/tests/t0.bin and b/tests/t0.bin differ
index 52676cee92000a05fd6d35f17e735c930528a989..7bd61d4771f622f70736d2cfe2aef038788354ae 100644 (file)
@@ -103,3 +103,4 @@ j1:
 .chksm
 
   ld [hl], a
+l3: .db 1