LC3 N装配广场 [英] LC3 Assembly Square of N

查看:133
本文介绍了LC3 N装配广场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个计算数字平方并将其存储在r0中的lc3汇编程序,该整数作为参数提供并位于r1中,我在调试时注意到的问题是在第一个过程中通过它最初添加2,但第二次通过它未能将另一个2添加到r0-我的代码在任何帮助下都值得赞赏

Hi I'm trying to write a lc3 assembly program which computes the square of a number and stores it in r0, the integer is given as a parameter and is located in r1, the problem i noticed while debugging is during the first pass it initially adds 2, but the second pass it fails to add another 2 to r0 - My code is below any help is appreciated

           .orig x3FF8
      ld r1,n
    ld r5,n

  square
 add r2,r1,#0

  add r5,r5,#-1
add r0,r2,#0
brzp square
brn theend

  theend


halt
 n .fill #2

 .end

我的最终代码感谢提供帮助的用户:

my final code thanks to the user who helped:

    .orig x3FF8
     ld r1,n
    ld r5,n

   square


  add r2, r2,r1

  add r5,r5,#-1

  brp square


  theend


 halt
  n .fill #4

 .end

推荐答案

如果我没有记错LC-3语法,add r2,r1,#0会执行r2 = r1 + 0,因此它实际上并没有添加到r2中,只是用r1.

If I remember LC-3 syntax correctly, add r2,r1,#0 does r2 = r1 + 0, so it was never actually adding to r2, just overwriting it with r1.

您希望在循环中进行类似的操作以初始化r2.

You want something like that outside the loop to initialize r2.

但是在循环内部,您需要add r2, r2, r1来执行r2 = r2 + r1,即r2 += r1.

But inside the loop, you want add r2, r2, r1 which does r2 = r2 + r1, i.e. r2 += r1.

我不明白为什么循环中也有add r0,r2,#0. 如果要在r0中获得最终结果,请首先在r0中进行累加.如果那应该是总和,那么您有同样的错误.

I don't understand why you have add r0,r2,#0 inside the loop as well. If you want the final result in r0, accumulate it in r0 in the first place. Of if that was supposed to be a sum of sums, then you have the same bug.

还请注意,add r5,r5,#-1必须是最后一个,因此从其为循环分支设置条件代码标志,而不是从add r0, r0, r2或循环内需要的其他任何地方设置.

Also note that add r5,r5,#-1 needs to be last so condition code flags are set from it for the loop branch, not from add r0, r0, r2 or whatever else you need inside the loop.

也:brn theend完全没用:theend在下一行,执行继续到下一行.您不必跳过源代码中的空白!

Also: brn theend is totally useless: theend is on the next line, and execution continues to the next line on its own. You don't have to jump over whitespace in the source!

这篇关于LC3 N装配广场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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