Add basic assembler code
authorbylex <mbilek06@gmail.com>
Wed, 2 Oct 2024 06:07:46 +0000 (08:07 +0200)
committerbylex <mbilek06@gmail.com>
Wed, 2 Oct 2024 06:07:46 +0000 (08:07 +0200)
Assembler is still work in progress

Makefile
assembler.c [new file with mode: 0644]
assembler.h [new file with mode: 0644]
common.h [new file with mode: 0644]

index 6a6f3b3ae8bebf9de3997d452ebb9156c71c13ce..db9300c5d9e1bc20f7fa4ea3498b91c40ad28523 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,7 @@
+CFLAGS = -g -w -O0
+
 all: int ass
+
 int: 
        $(CC) $(CFLAGS) interpreter.c -o interpreter
 ass:
diff --git a/assembler.c b/assembler.c
new file mode 100644 (file)
index 0000000..09bc341
--- /dev/null
@@ -0,0 +1,99 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "assembler.h"
+
+
+
+struct inst_t
+{
+       bool contains_instruction;
+       uint8_t instruction_code;
+       uint8_t params[3];
+} instruction_default = {false, 0, 0, 0, 0};
+
+void parse_line(FILE** input_file)
+{
+       char line_buffer[256];  
+
+       int token_count = 0;
+       char* token;
+       struct inst_t inst = instruction_default;
+
+       fgets(line_buffer, 256, *input_file);
+
+       if(line_buffer[0] == ';')
+       {
+               return;
+       }
+       else
+       {
+               inst.contains_instruction = true;
+       }
+       
+       token = strtok(line_buffer, " ");
+       while(token)
+       {
+               switch(token_count)
+               {
+                       case 0:
+                               // current token is instruction code
+                               uint8_t i = 0;
+                               while(i < N_INSTRUCTIONS - 1)
+                               {
+                                       if(strcmp(token, opcodes_strings[i]) == 0)
+                                       {
+                                               inst.instruction_code = i;
+                                               break;
+                                       }
+                                       i++;
+                               }
+                               break;
+                       case 1:
+                               // current token is first parameter
+                               inst.params[0] = (uint8_t)atoi(token);
+                               break;
+                       case 2: 
+                               // current token is second parameter
+                               uint16_t token_int = atoi(token);
+                               inst.params[1] = token_int;
+                               break;
+                       case 3: 
+                               // current token is third parameter
+                               break;
+               }
+               token_count++;
+               token = strtok(NULL, " ");
+       }
+}
+
+int main(int argc, char *argv[])
+{
+
+       if(argc <= 2 || argc >= 4)
+       {
+               printf("Usage: %s <input file> <output file>\n", argv[0]);
+               return 1;
+       }
+       
+       FILE* input_file = fopen(argv[1], "r");
+       if(!input_file)
+       {
+               perror("Cannot open input file for reading\n");
+               return 1;
+       }
+
+       FILE* output_file = fopen(argv[2], "w");
+       if(!output_file)
+       {
+               perror("Cannot open output file for writing\n");
+               return 1;
+       }
+       while(!feof(input_file))
+       {
+               parse_line(&input_file);
+       }
+}
diff --git a/assembler.h b/assembler.h
new file mode 100644 (file)
index 0000000..22714f8
--- /dev/null
@@ -0,0 +1,3 @@
+#include "common.h"
+
+char opcodes_strings[N_INSTRUCTIONS][4] = {"nop", "inc", "dec", "lod", "ldl", "sav", "swp", "jmp", "jez", "hlt", "pts", "pfs"};
diff --git a/common.h b/common.h
new file mode 100644 (file)
index 0000000..cd94b24
--- /dev/null
+++ b/common.h
@@ -0,0 +1 @@
+#define N_INSTRUCTIONS 12