-2

I am writing a program in C to convert a hex file compiled for the LC3 processor back into assembly language.

Currently, I am trying to decode the ADD instruction.

There are two types of ADDs in LC3 assembly language:

  • add by reference: adding two registers
  • add immediate: adding a register to a hard-coded value

For example, the hex code 164F would be converted to: ADD R3, R1, R7. this is an add by reference. Conversely, the hex code for 153F would be converted to: ADD R2, R4, #-1. This is an add immediate.

The function should decode both as appropriate.

4

1 回答 1

4

我想知道是否可以对第一个函数提供一些帮助: void printAdd(int instruction);

好吧,函数本身已经假设您已经检测到操作码并相应地分派了,所以我们不需要在这里处理那部分。ADD正如你所说,指令有两种形式。指令集参考将它们的结构显示为:

ADD DR, SR1, SR2 = 0001 DR SR1 0 0 0 SR2
ADD DR, SR, IMM5 = 0001 DR SR  1 IMM5

这意味着第 5 位区分两个版本。您将需要在该位上进行分支。除此之外,提取数字只是有点麻烦,而且很简单printf,我希望你知道如何使用。就像是:

void printAdd(int instruction)
{
    printf("ADD R%d, R%d, ", (instruction >> 9) & 7, (instruction >> 6) & 7);
    if (instruction & 0x20)
    {
        printf("#%d\n", instruction & 0x1F);
    } else {
        printf("R%d\n", instruction & 7);
    }
}

扩展立即数的符号作为练习;)

于 2015-04-16T00:41:04.427 回答