Allie Steele
Allie Steele

Reputation: 29

C Programming : pointers/char array

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;
    }
}

Upvotes: 1

Views: 1371

Answers (3)

Sinn
Sinn

Reputation: 274

using wrong strcnmp: Passing character(dereferenced char pointer) instead of char pointer(string) you need to pass the number of characters to compare, as a third argument.

Recommendation: Use a hash, to map keywords to binary codes

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263637

No -- in fact, it shouldn't even compile. (Did you try compiling your code before posting it here?)

strncmp and strcmp return 0 if the strings are equal (and strncmp takes three arguments, not two!). To compare two strings for equality, you want something like:

if (strncmp(string, "nand", 4) == 0) {
    ...

Or, more simply:

if (strcmp(string, "nand") == 0) {
    ...

Since the string literal "nand" sets a limit on the number of characters to be compared, there's not much point in using strncmp rather than strcmp`.

Both arguments to strcmp (or the first two arguments to strncmp) are of type char*. string is already of type char*; assuming it points to (the first character of) a string, just pass string, not *string, as the first argument to strcmp.

*binary = "000"; shouldn't even compile. binary = "000"; would compile, but it wouldn't do what you want; it's a pointer assignment, and it only affects the value of binary, which is local to your function; there's no visible effect once the function returns.

You can either make the second argument to your function a char**; then the caller can do something like:

char *binary;
if (opcodeBinary(some_string, &binary) {
    /* binary now points to a string like "000" */
}

Or the caller can allocate space for the string:

char binary[5]; /* or whatever size you need */

and then inside your function, replace the assignment by a call to strcpy:

strcpy(binary, "000");

You can't return an array from a function; there are several ways you can accomplish the same thing indirectly, but they're all somewhat tedious.

Upvotes: 2

nickie
nickie

Reputation: 5818

Your function opCodeBinary does not typecheck. When assigning *binary="000", the left hand side is an lvalue of type char and the right hand side is a char*. The same is true for the first operand to strncmp. You should not prepend a * when using strings in C.

What I assume you are required to do is to generate the binary opcode that corresponds to an assembly command. You should read the command from string and write the opcode to binary. There are several things that you do wrong here:

  1. You don't have a way to move the pointers string and binary, as you consume characters from your input and produce new binary opcodes to your output. This would be useful if you were to call the function opCodeBinary repeatedly. I would suggest that you use double pointers for both parameters to opCodeBinary.

  2. I assume you should not produce opcode 000 in ASCII representation (that is, the string "000") but one byte that contains it in binary form (or, worse, just three bits).

I'd suggest the following for your opCodeBinary function. It correctly resolves (1) and (2) above, but it assumes that each opcode goes to one or more (full) bytes in your binary.

int opCodeBinary (char **string, char **binary)
{
  if (strncmp(*string, "add", 3) == 0) {
     *string += 3;
     *(*binary)++ = '\x00';
     return 1;
  }
  ...
}

Upvotes: 1

Related Questions