[英]Why isn't my assembly program setting r1 to the correct value?
我正在LC3机器上编写汇编程序。
我的汇编程序是一个LC3程序,它将R2和R3相乘并将结果存储在R1中。
这是我的源代码(带注释)
;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
我的测试用例乘以4 *3。结果应为12,并将其存储在R1中。 但是,当我在LC3仿真器中运行该程序时,这就是我得到的输出
R3末尾保留正确的值,但R1保留-1。...有人看到我的代码有问题吗? 我确保在开始时清除R1,并继续将R3加到R1并将结果存储到R1,而此时计数器R3或3大于零。
HALT
只是用于停止机器的TRAP指令的“伪指令”。
您可以这样写:
TRAP x25 ;HALT the machine
但是以这种方式,您需要记住TRAP向量中的位置,在这种情况下为x25
。 因此最好只使用HALT
。
其他常见的TRAP也具有pseduo指令: IN
, OUT
等。
我假设您要将结果存储在某个地方。 您可以执行以下操作:
;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
- - - - - - - - - - -编辑 - - - - - - - - - - - - -
关于您的最后一个问题(在评论中):
如果HALT停止我的程序,那么Reslt .FILL x0000指令将如何运行?
这更多是关于汇编程序如何工作的问题。
答案是因为: 汇编时间 != 执行时间
指令在汇编时考虑。
实际上,组装时间由两步组成:
这是实现汇编程序的一种非常常见的方式,LC3汇编程序也不例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.