diff --git a/src/assemble.c b/src/assemble.c index 856a097..967b02e 100755 --- a/src/assemble.c +++ b/src/assemble.c @@ -1,8 +1,34 @@ #include #include +#include "a64instruction/a64instruction.h" #include "parser.h" #include "fileio.h" +#include "parser.h" +#include "twopassassembly.c" int main(int argc, char **argv) { + // Check the arguments + if (argc < 3) { + fprintf(stderr, "Error: A source file and an object output file are required. Syntax: ./assemble "); + return EXIT_FAILURE; + } + + // Load the source file into memory + char **source = readAssemblyFile(argv[1]); + + // Parse the source file + a64inst_instruction *instructions = parse(source); + + // First Pass: Create the symbol table + st *table = firstPass(instructions, 1000); // 1000 is just a temp fix. + + // Second Pass: Assemble the instructions + word *binary = secondPass(instructions, 1000, table); // 1000 is just a temp fix. + + // Write the binary to the output file + writeBinaryFile(binary, argv[2], 1000); // 1000 is just a temp fix. + + /* TODO: FREE MEMORY!! */ + return EXIT_SUCCESS; } diff --git a/src/fileio.c b/src/fileio.c index 85fd8d1..8be2e38 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -1,5 +1,7 @@ #include #include +#include "global.h" +#include "fileio.h" #define MAX_ASM_LINE_LENGTH 30 diff --git a/src/fileio.h b/src/fileio.h index a2d4262..88e9cca 100644 --- a/src/fileio.h +++ b/src/fileio.h @@ -5,5 +5,7 @@ #define EXIT_FAILURE 1 -extern byte *fileio_loadBin(const char *filePath, size_t memorySize); +char **readAssemblyFile(char inputFile[]); +int writeBinaryFile(word instrs[], char outputFile[], int numInstrs); + #endif diff --git a/src/parser.c b/src/parser.c index 8cb6609..a66b6f3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3,7 +3,6 @@ #include #include #include "parser.h" - #include "a64instruction/a64instruction.h" //takes input string, read from asm file and returns @@ -188,16 +187,15 @@ void tokeniseOperands(char* str, int *operandCount, char *operands[], int *numOp //takes inputted assembly line and returns a //pointer to an abstract representation of the instruction -a64inst_instruction *parser(char asmLine[]){ +void parser_instruction(char asmLine[], a64inst_instruction *instr) { int numOperands = 0; - a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); if (instr == NULL){ exit(EXIT_FAILURE); } if(strcmp(asmLine, HALT_ASM_CMD) == 0){ instr->type = a64inst_HALT; - return(instr); + return; } //"opcode operand1, {operand2}, ..." @@ -252,7 +250,19 @@ a64inst_instruction *parser(char asmLine[]){ } - return(instr); - } +// Takes an array of strings, each string representing an assembly instruction. +// Returns an array of a64inst_instruction pointers, each representing an instruction. +// Note. The array of strings must be NULL-terminated???? +a64inst_instruction *parse(char **asmLines) { + a64inst_instruction *instructions = malloc(sizeof(a64inst_instruction) * 1000); + + int i = 0; + while (asmLines[i] != NULL) { + parser_instruction(asmLines[i], &instructions[i]); + i++; + } + + return instructions; +} diff --git a/src/parser.h b/src/parser.h index e303b58..1f7ab70 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,2 +1,6 @@ +#include "a64instruction/a64instruction.h" + #define OPERAND_DELIMITER ", " #define HALT_ASM_CMD "and x0, x0, x0" + +a64inst_instruction *parse(char **asmLines); diff --git a/src/twopassassembly.c b/src/twopassassembly.c index e6899d5..b4ecdec 100644 --- a/src/twopassassembly.c +++ b/src/twopassassembly.c @@ -178,11 +178,12 @@ word ldl(a64inst_instruction cI) { return out; } -void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr[]) { +word *secondPass(a64inst_instruction instrs[], int numInstrs, st* table) { // TODO: // iterate over instructions again, this time replacing labels // with values from symbol table // after a line has had all the values replaced, assemble it and append + word *arr = (word*)malloc(sizeof(word) * numInstrs); int index = 0; for (int i = 0; i < numInstrs; i++) { a64inst_instruction cI = instrs[i]; @@ -221,5 +222,5 @@ void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr break; } } - return; + return arr; }