Add overall assemble structure
This commit is contained in:
parent
999f36facd
commit
850f3cf4f7
@ -1,8 +1,34 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#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 <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;
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "global.h"
|
||||
#include "fileio.h"
|
||||
|
||||
#define MAX_ASM_LINE_LENGTH 30
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
22
src/parser.c
22
src/parser.c
@ -3,7 +3,6 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@ -1,2 +1,6 @@
|
||||
#include "a64instruction/a64instruction.h"
|
||||
|
||||
#define OPERAND_DELIMITER ", "
|
||||
#define HALT_ASM_CMD "and x0, x0, x0"
|
||||
|
||||
a64inst_instruction *parse(char **asmLines);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user