add operation on a pointer value C -


i'm having problems trying create assembly emulator program in c. there 5 registers: rega, regb, regc, regx , insp, , 10 instructions: nop, set, , (bitwise &), or (bitwise |), add, sub, shl(<< left), shr(>>), jmp.

the program reads instructions file; lines containing instruction , 2 arguments. in cases 1st argument register name (e.g. rega) , 2nd argument can register name or integer.

i'm using sscanf instructions file.

i'm having trouble add, sub, shl , shr functions. add function is:

int opcode_add(char* opcode, char *arg1, char *arg2){     int i, j;     for(i = 0; < max_register; i++){          if (strcmp(register_str[i],arg1) == 0){              for(j = 0; j <=max_register; j++){                  if(strcmp(register_str[j],arg2) == 0){                      *register_ptr[i] = *register_ptr[i] + *register_ptr[j];                      break;                 }else {                     *register_ptr[i] = *register_ptr[i] + atoi(arg2);                  }                    }          }     }     insp++;      return (0);  } 

the function works if 2 register arguments passed. example:

set rega 1 set regb 2 add rega regb 

but not if register , integer passed. example:

set rega 2 add rega 1 

the problem @ line:

*register_ptr[i] = *register_ptr[i] + atoi(arg2); 

i tried doing this:

int y = *register_ptr[i]; int k = atoi(arg2); int result = y+k; *register_ptr[i] = result; 

but didn't work.

you didn't what didn't work - however, there @ least 1 error in way have structured inner loop , if. whenever inner loop sees register not match second operand, else block executed - all registers before 1 specified second operand added (and if second operand int, all registers added to). contents of else block must moved after loop, , must executed if inner loop didn't find register.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -