Add overall assemble structure

This commit is contained in:
sBubshait 2024-06-11 23:46:40 +01:00
parent 999f36facd
commit 850f3cf4f7
6 changed files with 54 additions and 9 deletions

View File

@ -1,8 +1,34 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "a64instruction/a64instruction.h"
#include "parser.h" #include "parser.h"
#include "fileio.h" #include "fileio.h"
#include "parser.h"
#include "twopassassembly.c"
int main(int argc, char **argv) { 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 <file_in> <file_out>");
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; return EXIT_SUCCESS;
} }

View File

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "global.h"
#include "fileio.h"
#define MAX_ASM_LINE_LENGTH 30 #define MAX_ASM_LINE_LENGTH 30

View File

@ -5,5 +5,7 @@
#define EXIT_FAILURE 1 #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 #endif

View File

@ -3,7 +3,6 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "parser.h" #include "parser.h"
#include "a64instruction/a64instruction.h" #include "a64instruction/a64instruction.h"
//takes input string, read from asm file and returns //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 //takes inputted assembly line and returns a
//pointer to an abstract representation of the instruction //pointer to an abstract representation of the instruction
a64inst_instruction *parser(char asmLine[]){ void parser_instruction(char asmLine[], a64inst_instruction *instr) {
int numOperands = 0; int numOperands = 0;
a64inst_instruction *instr = malloc(sizeof(a64inst_instruction));
if (instr == NULL){ if (instr == NULL){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(strcmp(asmLine, HALT_ASM_CMD) == 0){ if(strcmp(asmLine, HALT_ASM_CMD) == 0){
instr->type = a64inst_HALT; instr->type = a64inst_HALT;
return(instr); return;
} }
//"opcode operand1, {operand2}, ..." //"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;
}

View File

@ -1,2 +1,6 @@
#include "a64instruction/a64instruction.h"
#define OPERAND_DELIMITER ", " #define OPERAND_DELIMITER ", "
#define HALT_ASM_CMD "and x0, x0, x0" #define HALT_ASM_CMD "and x0, x0, x0"
a64inst_instruction *parse(char **asmLines);

View File

@ -178,11 +178,12 @@ word ldl(a64inst_instruction cI) {
return out; return out;
} }
void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr[]) { word *secondPass(a64inst_instruction instrs[], int numInstrs, st* table) {
// TODO: // TODO:
// iterate over instructions again, this time replacing labels // iterate over instructions again, this time replacing labels
// with values from symbol table // with values from symbol table
// after a line has had all the values replaced, assemble it and append // after a line has had all the values replaced, assemble it and append
word *arr = (word*)malloc(sizeof(word) * numInstrs);
int index = 0; int index = 0;
for (int i = 0; i < numInstrs; i++) { for (int i = 0; i < numInstrs; i++) {
a64inst_instruction cI = instrs[i]; a64inst_instruction cI = instrs[i];
@ -221,5 +222,5 @@ void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr
break; break;
} }
} }
return; return arr;
} }