我想制作一个程序来扫描包含以下内容的文件:
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 编译它时,没有出现编译错误。
这是一份lc3指令集供参考http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)