2

I want to make a program that scans a file with contents of:

1283

5105

The hexcodes for two instructions in lc3:

add r1,r2,r3

and r0,r4,r5

I want my program to read this file and print the two corresponding instructions on the screen can someone please tell me what's wrong with it

 #include <stdio.h>
 #include <stdbool.h>
 #include <string.h>
     int main(int argc, char *argv[]) {


FILE *file;
char hexString[5];
int dr, sr1, sr2, instruction;
file = fopen(argv[1], "r");

while (fscanf(file, "%d", hexString) != EOF){

    unsigned short int instruction = (unsigned short)strtol(hexString, NULL, 16);

        if (instruction >> 12 == 0b0001){ //op code is ADD

            dr = (instruction >> 9) & 0b111; // turns other bits to zero
            sr1 = (instruction >> 6) & 0b111;
            sr2 = (instruction) & 0b111;

            printf("add r%d r%d r%d", dr, sr1, sr2);

        } else if (instruction >> 12 == 0b0101){//op code is AND

            dr = (instruction >> 9) & 0b111;
            sr1 = (instruction >> 6) & 0b111;
            sr2 = (instruction) & 0b111;

            printf("and r%d r%d r%d", dr, sr1, sr2);
        }
}
fclose(file);
}

No complie errors come up when I compile it using gcc.

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

  • Welcome to StackOverflow. Could you provide the actual output of your program, so we can see what's wrong? As it stands, I can see one small typo: the `printf` statement in your first if block prints an AND instruction, but you're checking for an ADD – Jason Baker Oct 19 '14 at 22:30
  • @JasonBaker nothing is outputted with above code, the loop is not working. and thanks for the typo mention. Just to confirm their is no compile errors however I think the way I declared instruction is wrong. – Bilbo Baggins Oct 19 '14 at 22:34
  • Could someone suggest an alternative way to do this? If my way is not correct/interpretable? – Bilbo Baggins Oct 19 '14 at 22:36
  • You are reading the hex string with `fscanf` as an integer (`%d`). Read it as a string and it ought to work. – Jongware Oct 19 '14 at 22:40
  • I believe that `0b0001` isn't proper C syntax. What programming language are you writing your code in? – fuz Oct 19 '14 at 22:57
  • @FUZxxl Its works I left shift the hex code 12 places and convert all the first digits to zero to compare with the corresponding string? of the AND or ADD instruction set. I'm very much a beginner in C so I understand its not the most optimal way to get a solution. – Bilbo Baggins Oct 19 '14 at 23:00
  • @FUZxxl: it's [binary literal](http://stackoverflow.com/questions/9665078/enter-binary-instead-of-hex) notation. – Jongware Oct 20 '14 at 00:09
  • 2
    @Jongware So it's not standard C. Bilbo Baggins, you shouldn't use this notation. It's not guaranteed to work on any compiler but gcc. “It works on my machine” is often a rather weak thing when programming in C. – fuz Oct 20 '14 at 06:19

0 Answers0