为什么我的汇编程序没有将 r1 设置为正确的值? [英] Why isn't my assembly program setting r1 to the correct value?

查看:26
本文介绍了为什么我的汇编程序没有将 r1 设置为正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 LC3 机器上编写汇编程序.

I am writing an assembly program on the LC3 machine.

我的汇编程序是一个 LC3 程序,它将 R2 和 R3 相乘并将结果存储在 R1 中.

My assembly program is an LC3 program that multiplies R2 and R3 and stores the result in 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

我的测试用例乘以 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.... 有没有人看到我的代码有问题?我确保在开始时清除 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.

推荐答案

HALT 只是用于停止机器的 TRAP 指令的伪指令".

HALT is just a "pseudo-instruction" for a TRAP instruction used to halt the machine.

你可以写:

TRAP x25  ;HALT the machine

但通过这种方式,您需要记住 TRAP 向量中的位置,在本例中为 x25.所以最好只使用 HALT 代替.

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.

其他常见的TRAP也有伪指令:INOUT

Others common TRAPs also have pseduo-instructions: IN, OUT, etc.

我假设您想将结果存储在某个地方.你可以这样做:

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 指令将如何运行那么?

If HALT stops my program, how will Reslt .FILL x0000 directive run then?

这更多是关于汇编器如何工作的问题.

This is more a question about how assemblers works.

答案是因为:汇编时间 != 执行时间

指令在汇编时被考虑.

实际上,Assembly Time 由两遍组成:

In fact, Assembly Time is composed in two passes:

  1. 解析符号创建符号表
  2. 使用符号表将指令转换为真正可执行/机器代码".

这是实现汇编器的一种非常常见的方式,LC3 汇编器也不例外.

This is a very common way to implement assemblers, and the LC3 assembler is not the exception.

这篇关于为什么我的汇编程序没有将 r1 设置为正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆