classify asm line type, tokenise operands

This commit is contained in:
EDiasAlberto 2024-06-03 23:07:31 +01:00
parent ba1b614fc1
commit 036e163fe8
3 changed files with 40 additions and 4 deletions

View File

@ -1,8 +1,6 @@
#include <stdio.h>
#include <string.h>
#define MAX_ASM_LINE_LENGTH 100
//validates inputted charlist as valid filename against expected extension
int isValidFileFormat(char filename[], char expectedExtension[]){
int *pointLoc = strrchr(filename, '.');
@ -45,7 +43,7 @@ int readAssemblyFile(char inputFile[]) {
}
FILE *fp;
char savedLine[MAX_ASM_LINE_LENGTH];
char savedLine[sizeof(a64inst_instruction)];
fp = fopen(inputFile, "r");

View File

@ -14,6 +14,43 @@
// - count operands and match type/values
// - generate final a64inst and return
a64inst_instruction parser(char asmLine[]){
char *splitOperands(char* str, int operandCount, char *operands[]){
char *operandsDupe = strdup(str);
int operandCount = 0;
char *operand = strtok(operandsDupe, OPERAND_DELIMITER);
operands[0] = operand;
while (operand != NULL){
operandCount++;
operand = strtok(NULL, OPERAND_DELIMITER);
operands[operandCount] = operand;
}
return(operands);
}
a64inst_instruction *tokeniser(char asmLine[]){
a64inst_instruction *instr = malloc(sizeof(a64inst_instruction));
if (instr == NULL){
exit(EXIT_FAILURE);
}
//"opcode operand1, {operand2}, ..."
char *stringptr = strdup(asmLine);
char *opcode = strtok(stringptr, " ");
char *operands = strtok(NULL, "");
if(opcode[0]=="."){
//type is directive
} else if(opcode[strlen(opcode)-1]==":") {
//type is label
} else {
//type is instruction
int operandCount = 0;
const char *operandList[4];
splitOperands(operands, &operandCount, operandList);
}
}

View File

@ -0,0 +1 @@
#define OPERAND_DELIMITER ", "