汇编语言编程
我正在尝试编写一个程序,输入一个小于 10 的正数并输出第一个数字的总和。例如 5 就是 5+4+3+2+1。命令包括停止、加载、存储、加法、求和、乘法、除法、输入、输出、分支、如果为 0 则分支以及如果非 0 则分支。任何人都可以在这里帮助我。我有点被困住了。我写的是:
IN n
LD n
ADD sum
STO sum
LD n
SUB one
ADD sum
STO sum
BRGT haw
LD n
BR done
haw: OUT sum
done: STOP
DC n 4
DC sum 0
DC one 1
我认为它的工作方式是加载一些数字 n 并添加 0 的总和,然后将这个总和存储为 n+sum 。然后再次加载 n 并减去 1 并将其存储为新的总和。但我需要它重复这个直到 n 达到 0。那么我该怎么做呢?
好吧,所以我需要做的是使用分支如果 0 和分支如果不是 0。我想我有它? 所以:
IN n
LD n
ADD sum
STO sum
BR CAW
CAW: LD n
SUB ONE
STO n
BRGT HAW
BZ TAW
HAW: ADD SUM
STO SUM
BR CAW
TAW: OUT SUM
DC SUM 0
DC ONE 1
DC=指定常数,但我需要重复的是减一并加上总和,直到n达到0。如果不为零则分支为BRGT,如果零则分支为BZ,分支为BR,LD为负载。我知道我需要重复什么,但我不知道如何用汇编语言做到这一点。
I am trying to write a program that inputs a positive number less than 10 and outputs the sum of the first numbers. For example 5 would be 5+4+3+2+1. The commands are Stop, Load, Store, Add, Sum, Multiply, Divide, Input, Output, Branch, Branch if 0, and branch if not 0. Can anyone help me out here. I am kind of stuck. well what I have written is:
IN n
LD n
ADD sum
STO sum
LD n
SUB one
ADD sum
STO sum
BRGT haw
LD n
BR done
haw: OUT sum
done: STOP
DC n 4
DC sum 0
DC one 1
Well the way I see it working is you load some number n and add a sum of 0 and then store this sum as n+sum. Then you load n again and subtract 1 and store that as the new sum. But I need it to repeat this until n reaches 0. So how do I do this?
Ok so what i need to do is use the branch if 0 and branch if not 0. I think I have it?
so:
IN n
LD n
ADD sum
STO sum
BR CAW
CAW: LD n
SUB ONE
STO n
BRGT HAW
BZ TAW
HAW: ADD SUM
STO SUM
BR CAW
TAW: OUT SUM
DC SUM 0
DC ONE 1
DC=Designated Constant, but what I need to repeat is the subtracting by one and adding the sum until n reaches 0. Branch if not zero is BRGT and branch if zero is BZ and branch is BR, LD is load. I know what I need to repeat but I don't know how you do this in assembly language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为这听起来像家庭作业,所以我将从一些片段开始。
你已经弄清楚这个问题的逻辑了吗? “有点卡住”可能意味着您不知道需要如何完成此操作,或者您不确定如何使用可用的说明来实现它。如果您还没有弄清楚逻辑,请考虑一下 - 您如何提出这些术语,如何将它们加在一起,以及如何处理结果?
如果你已经弄清楚了逻辑,那么哪些命令将执行每个部分?
如果你已经弄清楚
如果您有更具体的问题,请随时更新,我会回复,但一般来说,SO 的政策是提供指导,而不仅仅是为(可能的?)作业问题编写代码。
编辑:好吧,太好了,您已经有了一些代码,看起来您已经走在正确的轨道上,但还没有实现。我要问你的第一件事是你是否尝试过执行纸上的代码。选择一个任意输入(比如 5,如您的示例中所示)并一次执行一条指令单步执行代码,看看程序执行的逻辑是否遵循您在步骤 1 中提出的逻辑。现在我相信该程序不按写的那样工作;看看你是否能找出原因,如果不能,我会给你一个提示。
编辑 2:太棒了,您已经非常接近了,只需编写代码即可。你说你想重复直到 n 达到 0。那么,哪个命令会让你根据 n 是否为 0 来选择两个分支之一?
编辑3:您正确地假设您需要使用branch if 0/branch not 0,但我在您的最新代码中没有看到任何这些。我错过了什么吗?另外,在这种情况下 DC 有何用处?如果您发布密钥,以便我确切地知道您正在使用哪些说明,这可能会有所帮助。正如我之前所说,尝试手动执行你的程序 - 这将告诉你错误在哪里。我现在给您的提示是确定代码中需要重复执行的部分,并查明它是否确实被重复执行。
Since this sounds like homework, I'll start with some pieces.
Have you worked out the logic to this problem yet? "Kind of stuck" could mean you have no idea how this needs to be done, or that you're not sure how to implement it with the available instructions. If you haven't worked out the logic, consider this - how do you come up with the terms, how do you add them together, and what do you do with the results?
If you have worked out the logic, then which commands will perform each part?
If you have more specific questions feel free to update and I'll respond, but generally the policy on SO is to offer guidance rather than just write code for (possible?) homework questions.
Edit: ok great, you've got some code, and it looks like you're on the right track but not there yet. The first thing I would ask you is whether you have tried to execute the code on paper. Pick an arbitrary input (say 5, as in your example) and step through the code one instruction at a time to see if the logic that the program performs follows the logic that you came up with in step 1. Right now I believe the program doesn't work as written; see if you can find out why and if not I'll give you a hint.
Edit 2: awesome, you're so close that you already have the answer and just need to code it. You said you want to repeat until n reaches 0. So, which command will let you take one of two branches depending on whether n is 0 or not?
Edit 3: you are correct in assuming that you need to use branch if 0 / branch not 0, but I don't see any of those in your latest code. Did I miss something? Also, what is DC for in this case? It might be helpful if you post a key so I know exactly which instructions you're using. As I said before, try executing your program by hand - that will show you where the bugs are. My hint for you right now is to identify the portion of your code that needs to be executed repeatedly and find out if it actually does get executed repeatedly.
首先尝试用高级伪代码写出问题。
然后将其翻译为您的汇编语言。
您可以在此处找到有关汇编语言编程的帮助
http://www.laynetworks.com/ assembly% 20tutorials.htm
这听起来像是学校作业,所以我认为这应该足以让你继续下去。
Try writing out the problem in a high-level pseudo-code first.
Then translate it to your assembly language.
You can find help on programming in assembly language here
http://www.laynetworks.com/assembly%20tutorials.htm
This smell like a school assignment, so I think that should be enough to get you going.
考虑一下 C 语言中的情况(假设您知道这一点),
注意缺少 break 语句(第 0 种情况除外)。
这有帮助吗?
think about it in C (assuming you know it)
note the lack of break statements (except on the 0'th case).
Does this help?