handle directive parsing

This commit is contained in:
EDiasAlberto 2024-06-14 20:34:44 +01:00
parent 3b3cda2d26
commit 19b98a3c51

View File

@ -169,6 +169,25 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *tokens[], in
}
//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'){
number = strtol(operandCpy+1, endptr, 16);
} else {
number = strtol(operandCpy, endptr, 10);
}
return number;
}
void parse_instruction(char asmLine[], a64inst_instruction *instr) {
if (instr == NULL){
@ -188,6 +207,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 +250,6 @@ 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) {