LC-3代码段

时间:2015-03-01 18:38:07

标签: lc3

我很难理解这个特殊问题。我有答案,但我真的想知道为什么他们是这样的原因!我理解每个操作码是如何工作的,只是没有将它应用于这个问题.....

工程师正在调试她编写的程序。她正在查看程序的以下部分,并决定在内存位置0xA404处放置一个断点。从PC = 0xA400开始,她将所有寄存器初始化为零并运行程序直到遇到断点。

代码段:

0xA400 THIS1 LEA     R0, THIS1 
0xA401 THIS2 LD      R1, THIS2
0xA402 THIS3 LDI     R2, THIS5
0xA403 THIS4 LDR     R3, R0, #2
0xA404 THIS5 .FILL   xA400

遇到断点时显示寄存器文件的内容(十六进制)。

同样,我不是在寻找答案清单,而是一个解释,帮助我理解程序中究竟发生了什么。非常感谢!

1 个答案:

答案 0 :(得分:0)

如果工程师将断点放在0xa404行(在运行0xa404之前停止程序),代码将执行以下操作:

0xA400 THIS1 LEA     R0, THIS1  ; LEA loads the address of THIS1 into R0.
                                ; Since THIS1 is at memory location 0xA400, 
                                ; after this instruction R0 = 0xA400

0xA401 THIS2 LD      R1, THIS2  ; LD loads the contents of the memory at
                                ; THIS2 into R1.  Since THIS2 is this very
                                ; line its contents are this instruction,
                                ; which is 0010001111111111 in binary or
                                ; 0x23ff in hex, so after this line executes
                                ; R1 hold 0x23ff

0xA402 THIS3 LDI     R2, THIS5  ; LDI visits THIS5 and treats its value as a
                                ; new memory location to visit.  It visits 
                                ; that second location and stores its 
                                ; contents into R2. In this case, it would
                                ; look at THIS5 and see its value is 0xA400.
                                ; It would then visit 0xA400 and store its
                                ; contents in R2.  0xA400 contains the first 
                                ; line of your program which translates to
                                ; 1110000111111111 in binary, 0xe1ff in 
                                ; hex, so it stores 0xe1ff into R2.

0xA403 THIS4 LDR     R3, R0, #2 ; LDR starts from the memory location of R0,
                                ; adds 2 to that, then stores whatever it 
                                ; finds in that memory location into R3. In
                                ; this case R0 = 0xA400. It adds 2, bringing
                                ; it up to 0xA402, which is the instruction
                                ; immediately above this one.  In binary, that
                                ; instruction is 1010 0100 0000 0001, which
                                ; translates into 0xa401 so the program stores
                                ; the program stores 0xa401 into R3.

0xA404 THIS5 .FILL   xA400