-5

I want to get the output:

add reg1,reg2,reg3

My Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void instruction(int binary);
int binaryToDecimal(int n);

int main()
{
    int testBinary = 0001000110100011;
    instruction(testBinary);
}

void instruction(long binary)
{
    char add[4]  = "0001";
    char and[4]  = "0101";
    int binaryRep;

    if(stncmp(add, binary, 4) == 1)
    {
        int reg1;
        int reg2;
        int reg3;
        int temp3;

        binary = binary << 4 ;

        binary = binary << 3 ;
        memcpy(reg1, binary, 3);

        binary = binary << 3 ;
        memcpy(reg2, binary, 3);

        binary = binary <<6;
        memcpy(reg3, binary, 3);

        reg1 = binaryToDecimal(reg1);
        reg2 = binaryToDecimal(reg2);
        reg3 = binaryToDecimal(reg3);

        printf("add r%d, r%d, r%d", reg1,reg2,reg3);
    }

    if(stncmp(and, binary, 4) == 1)
    {
        int reg1;
        int reg2;
        int reg3;
        int temp3;

        binary = binary << 4 ;
        binary = binary << 3 ;
        memcpy(reg1, binary, 3);

        binary = binary << 3 ;
        memcpy(reg2, binary, 3);

        binary = binary <<6;
        memcpy(reg3, binary, 3);

        reg1 = binaryToDecimal(reg1);
        reg2 = binaryToDecimal(reg2);
        reg3 = binaryToDecimal(reg3);

        printf("and r%d, r%d, r%d", reg1,reg2,reg3);


    }

}

int binaryToDecimal(int n) /* Function to convert binary to decimal.*/
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(2,i);
        ++i;
    }
    return decimal;
}

But I'm getting all sorts of errors with types and stncmp. Errors: warning: implicit conversion from 'long' to 'int' changes value from 68738383881 to 18907145 [-Wconstant-conversion]

And

Warning: implicit declaration of function 'stncmp' is invalid in C99 [-Wimplicit-function-declaration] if(stncmp(add, binary, 4) == 1)

Can someone please help?

Heres a copy of lc3 instruction set for reference http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif

Landon
  • 21
  • 2
  • 8
  • What should it be? Sorry I'm new to C (Coming from python background) – Landon Oct 20 '14 at 06:48
  • From where did you get the idea that `0001001` etc. are binary literals? They are not. They are octal (as written down in possibly any decent beginner's C language tutorial!) – The Paramagnetic Croissant Oct 20 '14 at 06:49
  • 0001000110100011 is from converting a hex value to binary. – Landon Oct 20 '14 at 07:11
  • @Landon In both C and C++ - *as in Python* - binary literals are prefixed with "0b", and an integer literal starting with "0" is octal. Also, `and` and `add` are too small for what you put in them, and you can't compare strings and numbers. – molbdnilo Oct 20 '14 at 08:48

1 Answers1

0

You can not initialize variables with binary values. Starting with 0 means that the value is octal. Starting with 0x means that the value is hexademical So your initialization may be done like that:

int testBinary = 0x11A3;

As for stncmp, you can't use it ti compare char* with int. I guess you may initialize your testbinary like this for your case:

char testbinary[17] = "0001000110100011";

And then you can compare it with and.

Vladyslav
  • 786
  • 4
  • 19