继续我之前的问题从 C 中的文件中读取字符串并将它们作为 lc3 分解器进行操作时遇到问题
我现在需要使用 ADD & AND 在 lc3 反汇编器中实现立即寻址模式
例如,如果一个文件包含:
1283
5105
1df7
506f
我想打印出来:
添加 r1,r2,r3
和 r0,r4,r5
添加 r6,r7,-9
和 r0,r1,15
我如何能够打印出 -9 和 15 我知道我需要将其转换为二进制补码,但不确定如何。
这是我引用 ADD 指令的 If 语句的代码,即。输出的第 3 行和第 1 行
while (fscanf(file, "%s", hexString) != EOF){
long int instruction = strtol(hexString, NULL, 16);
if (instruction >> 12 == 0b0001){ //op code is ADD
if ((instruction >> 5) & 0b001){ //is using “immediate” addressing mode
dr = (instruction >> 9) & 0b111;
sr1 = (instruction >> 6) & 0b111;
sr2 = (!instruction) & 0b11111 + 1; // this needs to convert to twos complement
printf("and r%d,r%d,%d \n", dr, sr1,sr2);
} else {
dr = (instruction >> 9) & 0b111; // turns other bits to zero
sr1 = (instruction >> 6) & 0b111;
sr2 = (instruction) & 0b111;
printf("add r%d,r%d,r%d \n", dr, sr1, sr2);
}
} else if ....
这是 lc3 指令集的副本供参考http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif