Я пытаюсь создать простой эмулятор сборки. Я пытаюсь создать функцию set; функция будет принимать 2 параметра (массивы String) arg1 & arg2. , он затем сравнивает строки с массивом строк, который является индексом для arraylist указателей функций.Ошибка шины при попытке передать int указателю
Проблема, с которой я столкнулась, - это когда я пытаюсь установить значение регистра. Я пробовал много вариантов линии:
*register_ptr[i] = *(int*)(atoi(arg2));
без успеха; есть ли что-то, чего я не понимаю?
код, мы надеемся, будет более ясно:
int opcode_set(char* opcode, char *arg1, char *arg2)
{
int i, j;
//printf("%d\n", (int)arg2);
for(i=0;i<4;i++)
{
if(!strcmp(arg1, register_str[i]))
{
for(j=0;j<4;j++)
{
if(!strcmp(arg2, register_str[i]))
{
*register_ptr[i] = *(int*)register_ptr[j];
}
else
{
*register_ptr[i] = *(int*)(atoi(arg2));
}
}
}
}
//printf("%d", *register_ptr[i]);
INSP++; /* NOP does not do anything except moving the instruction pointer to the next instruction */
return (0);
}
EDIT: объявления для register_str & register_ptr:
const char *register_str[] = {"REGA", "REGB", "REGC", "REGX"};
int *register_ptr[]={®A, ®B, ®C, ®X};
я использую два массива для того, чтобы решить, какой опкод, массив строк , и массив указателей функций, я индексирую строки и использую одно и то же место индекса для вызова функции:
int exec_instruction(char *instruction){
int i; //used for indexing
/* the line below may be useful for debugging to see the current instruction*/
/*printf("executing line: %s", instruction);*/
/* three variables could be used to extract opcode and
arguments from the variable instruction */
char *opcode = NULL;
char *arg1 = NULL;
char *arg2 = NULL ;
char *copy = NULL;
/* we need here some functionality to extract opcode,
arg1 and arg2 from the string instruction*/
copy = strdup(instruction);
opcode = strtok(copy," \n\r");
arg1 = strtok(NULL," \n\r");
arg2 = strtok(NULL," \n\r");
/* Now we have to call the right function corresponding
to the right opcode For example: */
for(i = 0; i < 10; i++)
{
if(!strcmp(opcode_str[i], opcode))
(*opcode_func[i])(opcode,arg1,arg2);
}
/* for demonstration purpose we execute NOP independent of input
this line must go in your implementation */
(*opcode_func[0])("NOP",NULL,NULL);
/* return value should be 0 if execution was ok otherwise -1*/
return(0);
}
Два массива я говорил:
const char *opcode_str[] = {"NOP", "SET", "AND", "OR", "ADD", "SUB", "SHL", "SHR", "JMP", "PRT"};
opcode_function opcode_func[] = { &opcode_nop, &opcode_set, &opcode_and, &opcode_or, &opcode_add, &opcode_sub, &opcode_shl, &opcode_shr, &opcode_jmp, &opcode_prt };
Что именно находится в arg2? И является ли допустимым адресом памяти для чтения int? –
Вы уверены, что не имеете в виду 'register_ptr [i] = (int *) (atoi (arg2));'? –
arg2 будет строкой с константой (номерами) или регистром (REGA, REGB, REGC, REGX). я не понимаю, что вы имеете в виду по второму вопросу – Babbleshack