无法从C中的文件中读取字符串并将其作为lc3解析器进行操作

时间:2014-10-19 22:22:28

标签: c string types lc3

我想创建一个扫描文件的程序,内容为:

1283

5105

lc3中两条指令的十六进制代码:

添加r1,r2,r3

和r0,r4,r5

我希望我的程序能够阅读此文件并在屏幕上打印两条相应的说明,有人可以告诉我它有什么问题

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

使用gcc编译时没有出现complie错误。

以下是参考http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif

的lc3指令集的副本

0 个答案:

没有答案