WIP: instructions
authorLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 16:17:56 +0000 (17:17 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 16:17:56 +0000 (17:17 +0100)
include/ulas.h
src/ulas.c

index 98bf2195b0d66b7af83e6ec208420bbe06bec516..5feb5ab2ee797de40f3c483c81a91131ccb5a0c3 100644 (file)
@@ -7,6 +7,7 @@
 
 #define ULAS_PATHMAX 4096
 #define ULAS_LINEMAX 4096
+#define ULAS_OUTBUFMAX 64
 #define ULAS_MACROPARAMMAX 9
 
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
@@ -147,6 +148,9 @@ struct ulas {
   // internal counter
   // used whenever a new unique number might be needed
   int icntr;
+
+  FILE *verbout;
+  FILE *symsout;
 };
 
 extern struct ulas ulas;
index 6f402f57fd00bcb0314fafa0cec1a70fec12082a..451968d947fec380dc13000f7589d091702347e8 100644 (file)
@@ -1303,18 +1303,26 @@ int ulas_intexpr(const char **line, unsigned long n, int *rc) {
   return ulas_intexpreval(expr, rc);
 }
 
-int ulas_asminstr(FILE *dst, const char *line, unsigned long n) {
+int ulas_asmimisc(FILE *dst, const char *line, unsigned long n) {}
+
+// assembles an instruction, writes bytes into dst
+// returns bytes written or -1 on error
+int ulas_asminstr(char *dst, unsigned long max, const char *line,
+                  unsigned long n) {
   int rc = 0;
 
   return rc;
 }
 
 int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
+  // this buffer is written both to dst and to verbose output
+  char *outbuf[ULAS_OUTBUFMAX];
+  memset(outbuf, 0, ULAS_OUTBUFMAX);
+  long towrite = 0;
+
   const char *start = line;
   int rc = 0;
 
-  fprintf(dst, "%s", line);
-
   // read the first token and decide
   ulas_tok(&ulas.tok, &line, n);
 
@@ -1358,13 +1366,22 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
 
   } else {
     // is regular line in form of [label:] instruction ; comment
-    if (ulas_asminstr(dst, line, n) == -1) {
+    if ((towrite += ulas_asminstr(outbuf, ULAS_OUTBUFMAX, line, n)) == -1) {
       ULASERR("Unable to assemble instruction\n");
       rc = -1;
       goto fail;
     }
+
+    // TODO: place marker when a label was unresolved
+    // write down byte location in dst as well as line number in verbout so we
+    // can fix them later
   }
 
+  fwrite(outbuf, 1, towrite, dst);
+
+  // TODO: verbose output <address> <bytes>\tline
+  fprintf(dst, "%08X\t%s", ulas.address, start);
+
 fail:
   return rc;
 }