Add support for memory keywords in assembler
authorbylex <mbilek06@gmail.com>
Wed, 16 Oct 2024 07:01:40 +0000 (09:01 +0200)
committerbylex <mbilek06@gmail.com>
Wed, 16 Oct 2024 07:01:40 +0000 (09:01 +0200)
assembler.c

index 8f2a146f62b5f46c989edb0c5c663b56690d6d6e..9e1731ed0cc2b0b296599d4ac4ad6bd478e1d8c9 100644 (file)
@@ -7,14 +7,15 @@
 
 #include "assembler.h"
 
-
+uint16_t mem[65536];
 
 struct inst_t
 {
        bool contains_instruction;
        uint8_t instruction_code;
        uint8_t params[3];
-} instruction_default = {false, 0, 0, 0, 0};
+       uint16_t addr;
+} instruction_default = {false, 0, 0, 0, 0, 0};
 
 struct label_t
 {
@@ -155,16 +156,15 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int
                token_count++;
                token = strtok(NULL, " ");
        }
+       inst.addr = current_addr;
+       memcpy(&mem[current_addr], &inst.instruction_code, 4);
        current_addr = current_addr + 2;
        return inst;
 }
 
-void write_instruction(FILE** output_file, struct inst_t inst)
+void write_instructions(FILE** output_file)
 {
-       if(inst.contains_instruction)
-       {
-               fwrite(&inst.instruction_code, sizeof(uint8_t), 4, *output_file);
-       }
+       fwrite(&mem, sizeof(uint16_t), 65536, *output_file);
 }
 
 int main(int argc, char *argv[])
@@ -236,6 +236,7 @@ int main(int argc, char *argv[])
        rewind(input_file);
        while(fgets(line_buffer, 256, input_file))
        {
-               write_instruction(&output_file, parse_line(line_buffer, labels, &labels_len, labels_index));
+               parse_line(line_buffer, labels, &labels_len, labels_index);
        }
+       write_instructions(&output_file);
 }