From 16e52a9421bd67ba04762e4d0f53f28aa93707eb Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 14 Jun 2024 16:52:48 +0100 Subject: [PATCH] fix directive value parsing --- src/parser.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/parser.c b/src/parser.c index 395d18c..4f2f71c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -88,6 +88,26 @@ int isOperandRegister(char regStartChar) { return((regStartChar == 'x') || (regStartChar == 'w')); } +//takes inputted char array and returns the integer of the operand, skipping the first character +//e.g. for a passed "R32", it skips the 'R' and returns 32 +int getOperandNumber(char *operand){ + char operandCpy[strlen(operand)]; + strcpy(operandCpy, operand+1); + char **endptr = NULL; + int number; + if((strncmp(operandCpy, "0x", 2)==0)) { + //hex value + strcpy(operandCpy, operand+3); + number = strtol(operandCpy, endptr, 16); + } else if(operandCpy[0]=='x'){ + strcpy(operandCpy, operand+2); + number = strtol(operandCpy, endptr, 16); + } else { + number = strtol(operandCpy, endptr, 10); + } + return number; +} + int classifyDPInst(char *operandList[]){ return(isOperandRegister(operandList[1][0]) && isOperandRegister(operandList[2][0]) && @@ -188,6 +208,7 @@ void parse_instruction(char asmLine[], a64inst_instruction *instr) { if(strcmp(opcode, ".int") == 0){ // Directive instr->type = a64inst_DIRECTIVE; + instr->data.DirectiveData.value = getOperandNumber(tokens[1]); } else if(opcode[strlen(opcode)-1]== ':') { // Label @@ -230,22 +251,7 @@ void parse_instruction(char asmLine[], a64inst_instruction *instr) { } -//takes inputted char array and returns the integer of the operand, skipping the first character -//e.g. for a passed "R32", it skips the 'R' and returns 32 -int getOperandNumber(char *operand){ - char operandCpy[strlen(operand)]; - strcpy(operandCpy, operand+1); - char **endptr = NULL; - int number; - if(strncmp(operandCpy, "0x", 2)==0){ - //hex value - strcpy(operandCpy, operand+3); - number = strtol(operandCpy, endptr, 16); - } else { - number = strtol(operandCpy, endptr, 10); - } - return number; -} + void calculateAddressFormat(a64inst_instruction *instr, char *tokens[], int tokenCount) {