简体   繁体   English

为什么我的汇编程序没有将r1设置为正确的值?

[英]Why isn't my assembly program setting r1 to the correct value?

I am writing an assembly program on the LC3 machine. 我正在LC3机器上编写汇编程序。

My assembly program is an LC3 program that multiplies R2 and R3 and stores the result in R1. 我的汇编程序是一个LC3程序,它将R2和R3相乘并将结果存储在R1中。

Here is my source code(with comments) 这是我的源代码(带注释)

;Sets pc to this address at start of program 
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them 
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone 
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
HALT
.END

My test case is multiplying 4 * 3. The result should be 12 and that should be stored in R1. 我的测试用例乘以4 *3。结果应为12,并将其存储在R1中。 However when I run this program in my LC3 simulator, this is what i get for the output 但是,当我在LC3仿真器中运行该程序时,这就是我得到的输出 在此处输入图片说明

R3 holds the correct value at the end but R1 holds -1.... Does anyone see an issue with my code? R3末尾保留正确的值,但R1保留-1。...有人看到我的代码有问题吗? I made sure to clear R1 at the beginning and to keep adding R3 to R1 and storing the result to R1 while the counter, R3, or 3 in this case is greater than zero. 我确保在开始时清除R1,并继续将R3加到R1并将结果存储到R1,而此时计数器R3或3大于零。

HALT is just a "pseudo-instruction" for a TRAP instruction used to halt the machine. HALT只是用于停止机器的TRAP指令的“伪指令”。

You could write: 您可以这样写:

TRAP x25  ;HALT the machine

But in this way you need to remember the position in the TRAP vector, in this case x25 . 但是以这种方式,您需要记住TRAP向量中的位置,在这种情况下为x25 So is better to just use HALT instead. 因此最好只使用HALT

Others common TRAPs also have pseduo-instructions: IN , OUT , etc. 其他常见的TRAP也具有pseduo指令: INOUT等。

I asume you want to store your results somewhere. 我假设您要将结果存储在某个地方。 You could do something like: 您可以执行以下操作:

;Sets pc to this address at start of program 
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them 
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone 
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
ST R1, Result         ;STORE R1 at Result
HALT
Result .FILL x0000    ;this will be x000C=12h after execution
.END

---------------------EDIT-------------------------- - - - - - - - - - - -编辑 - - - - - - - - - - - - -

About you last question (in comments): 关于您的最后一个问题(在评论中):

If HALT stops my program, how will Reslt .FILL x0000 directive run then? 如果HALT停止我的程序,那么Reslt .FILL x0000指令将如何运行?

This is more a question about how assemblers works. 这更多是关于汇编程序如何工作的问题。

The answer is because: Assembly Time != Execution Time 答案是因为: 汇编时间 != 执行时间

Directives are considered at Assembly Time. 指令在汇编时考虑。

In fact, Assembly Time is composed in two passes: 实际上,组装时间由两步组成:

  1. Resolve symbols creating a symbol table 解决符号创建符号表的问题
  2. Convert instructions to "truly executable/machine code" using the symbol table. 使用符号表将指令转换为“真正的可执行文件/机器代码”。

This is a very common way to implement assemblers, and the LC3 assembler is not the exception. 这是实现汇编程序的一种非常常见的方式,LC3汇编程序也不例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM