I have a project that entails us as a programmer, to translate a line of assembly language into machine language and calculate the decimal. We have our own sample computer with opcodes to make it simpler to calculate. My question is if the following function does what I aim to do.
In the main function, I have an array of chars labeled char binary[3] since the value returned will be three bits. Also, in the function is a char opcode[MAXLINELENGTH] which reads in from a file properly the opcode line so I point to it with *string within the function. Will this correctly find the match and fill the binary array in main with the proper 3 digit bit code?
//function should return true if the proper binary output was successfuly copied with the matching opcode
int opcodeBinary(char *string,char *binary){
if(strncmp(*string,"add"){
*binary="000";
return 1;
}
else if(strncmp(*string,"nand"){
*binary="001";
return 1;
}
else if(strncmp(*string,"lw"){
*binary="010";
return 1;
}
else if(strncmp(*string,"sw"){
*binary="011";
return 1;
}
else if(strncmp(*string,"beq"){
*binary="100";
return 1;
}
else if(strncmp(*string,"jalr"){
*binary="101";
return 1;
}
else if(strncmp(*string,"halt"){
*binary="110";
return 1;
}
else if(strncmp(*string,"noop"){
*binary="111";
return 1;
}
else{
return 0;
}
}
if(strncmp(*string,"add"){strncmp() takes 3 arguments. 2) And you probably wantif( !strncmp(*string,"add", 3) ){3) ` *binary="011";` You can't assign a char pointer to a dereferenced char pointer.