问题描述
我在LC3机器上编写一个汇编程序。
I am writing an assembly program on the LC3 machine.
我的汇编程序是乘以R2和R3和结果保存在R1的LC3程序。
My assembly program is an LC3 program that multiplies R2 and R3 and stores the result in R1.
下面是我的源$ C $ c(带评论)
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
我的测试用例乘以4 * 3的结果应该是12和应当被存储在R1中。然而,当我运行这个程序在我LC3模拟器,这是我得到的输出
My test case is multiplying 4 * 3. The result should be 12 and that should be stored in R1. However when I run this program in my LC3 simulator, this is what i get for the output
R3拥有正确的值时结束,但R1拥有-1 ....有谁看到一个问题,我的code?我确信在开始清除R1和继续增加R3至R1和存储结果到R1,而在这种情况下,计数器,R3或3是大于零。
R3 holds the correct value at the end but R1 holds -1.... Does anyone see an issue with my code? 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.
推荐答案
暂停
只是一个伪指令为用于暂停机器TRAP指令。
HALT
is just a "pseudo-instruction" for a TRAP instruction used to halt the machine.
您可以这样写:
TRAP x25 ;HALT the machine
但这种方式,你需要记住的陷阱向量的位置,在这种情况下 X25
。因此,最好是只使用暂停
代替。
But in this way you need to remember the position in the TRAP vector, in this case x25
. So is better to just use HALT
instead.
其他常见陷阱也有pseduo指令:在
, OUT
等
Others common TRAPs also have pseduo-instructions: IN
, OUT
, etc.
我asume要存储结果的地方。你可以这样做:
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
---------------------编辑---------------------- ----
关于你的最后一个问题(在评论)
About you last question (in comments):
如果HALT停止我的程序,怎么会Reslt .fill伪指令X0000运行
然后呢?
这更是一个关于汇编是如何工作的问题。
This is more a question about how assemblers works.
答案是因为:组装时间 = 执行时间
指令是在大会的时间考虑。
Directives are considered at Assembly Time.
事实上,组装时间由两关:
In fact, Assembly Time is composed in two passes:
- 解析符号创建一个符号表
- 转换的说明真正可执行/机code使用符号表中。
这是实现装配一个很常见的方式,和LC3汇编器是不是例外。
This is a very common way to implement assemblers, and the LC3 assembler is not the exception.
这篇关于为什么不是我的汇编程序设置R1到正确的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!