From fab4047d22907f8c63c19dcb4f447617bc5284a7 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Wed, 12 Jun 2024 17:48:23 +0100 Subject: [PATCH] Update fileio, skip new lines --- src/fileio.c | 9 ++++++++- src/parser.c | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index f2f47fd..cd4fcc6 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -35,11 +35,13 @@ int countLines(char *filename) { int count = 0; char c; + char prevC = '\n'; while ((c = fgetc(file)) != EOF) { - if (c == '\n') { + if (c == '\n' && prevC != '\n') { count++; } + prevC = c; } return count; @@ -71,6 +73,11 @@ char **readAssemblyFile(char filename[], int lineCount) { exit(EXIT_FAILURE); } + if (*buffer == '\n') { + // Skip empty lines. + continue; + } + lines[currentLine] = malloc(strlen(buffer) + 1); if (lines[currentLine] == NULL) { fprintf(stderr, "Error: Could not allocate memory to store the assembly line"); diff --git a/src/parser.c b/src/parser.c index ba29ccf..39e0e3b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -213,7 +213,8 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { strcpy(stringptr, asmLine); char *opcode = strtok(stringptr, " "); - char *operands = strtok(NULL, ""); + char *operands = strtok(stringptr, ""); + if(strcmp(opcode, ".int") == 0){ //type is directive @@ -223,8 +224,8 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { //type is label //add to symbol table instr->type = a64inst_LABEL; - char *opcodeCpy = NULL; - opcodeCpy = strcpy(opcodeCpy, opcode); + char opcodeCpy[strlen(opcode)]; + strcpy(opcodeCpy, opcode); char *labelData = strtok(opcodeCpy, ":"); instr->data.LabelData.label = labelData; } else {