Improve parsing of labels
authorbylex <mbilek06@gmail.com>
Thu, 10 Oct 2024 13:39:31 +0000 (15:39 +0200)
committerbylex <mbilek06@gmail.com>
Thu, 10 Oct 2024 13:39:31 +0000 (15:39 +0200)
Also partially add support for organization of memory

assembler.c

index 10728d0b510e8fba32e902c853b62805d835c6d1..8f2a146f62b5f46c989edb0c5c663b56690d6d6e 100644 (file)
@@ -22,9 +22,8 @@ struct label_t
        uint16_t addr;
 };
 
-struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int* labels_len)
+struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int* labels_len, unsigned int labels_index)
 {
-       static unsigned int labels_index = 0;
        static uint16_t current_addr = 0;
 
        int token_count = 0;
@@ -36,7 +35,7 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int
                return inst;
        }
        else if(line_buffer[0] == '$')
-       {
+       { /*
                struct label_t label;
 
                int i = 0;
@@ -58,7 +57,12 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int
                        labels = realloc(labels, (*labels_len) * sizeof(struct label_t));
                }
                labels[labels_index] = label;
-               labels_index++;
+               labels_index++; */
+               return inst;
+       }
+       else if(line_buffer[0] == '.')
+       {
+               current_addr = atoi(&line_buffer[1]);
                return inst;
        }
        else
@@ -188,11 +192,50 @@ int main(int argc, char *argv[])
                return 1;
        }
 
-       unsigned int line_num = 0;
+       unsigned int labels_index = 0;
+       unsigned int current_addr = 0;
+
        char line_buffer[256];  
+
+       
+       while(fgets(line_buffer, 256, input_file))
+       {
+               if(line_buffer[0] == ';');// do nothing
+               else if(line_buffer[0] == '$')
+               {
+                       struct label_t label;
+
+                       int i = 0;
+                       while(i <  256)
+                       {
+                               if(isspace(line_buffer[i]))
+                               {
+                                       line_buffer[i] = '\0';
+                               }
+                               i++;
+                       }
+
+                       label.label_name = malloc(256);
+                       strcpy(label.label_name, line_buffer);
+                       label.addr = current_addr;
+                       if(labels_index + 1 == labels_len)
+                       {
+                               labels_len = labels_len + 16;
+                               labels = realloc(labels, (labels_len) * sizeof(struct label_t));
+                       }
+                       labels[labels_index] = label;
+                       labels_index++;
+               }
+               else if(line_buffer[0] == '.')
+               {
+                       current_addr = atoi(&line_buffer[1]);
+               }
+               else current_addr = current_addr + 2;
+       }
+
+       rewind(input_file);
        while(fgets(line_buffer, 256, input_file))
        {
-               line_num++;
-               write_instruction(&output_file, parse_line(line_buffer, labels, &labels_len));
+               write_instruction(&output_file, parse_line(line_buffer, labels, &labels_len, labels_index));
        }
 }