【问题标题】:Python to OAM Assembly Language [closed]Python 到 OAM 汇编语言 [关闭]
【发布时间】:2013-07-12 01:10:19
【问题描述】:

我正在尝试将我的 python 程序转换为汇编语言,但我不确定如何。这是python程序:

numberofscores=float(input("Please enter the number of test scores to be entered:"))
sumofscores = 0
count = 1
while count <= numberofscores :
      score1=float(input("Please enter the test score:"))
      sumofscores=sumofscores+score1
      count=count+1
average=sumofscores/numberofscores 
print average 

【问题讨论】:

  • 也许从 OAM 手册/参考开始。然后告诉我们您尝试过什么或不理解的部分。
  • 这是 OAMulator 的网站:
  • compepi.cs.uiowa.edu/cgi-bin/OAMulator2.cgi 我在构建循环或分支时遇到问题

标签: python assembly oam


【解决方案1】:

嗯,我从未听说过这个名为 One Address Machine 的教学资源,它是基于网络的 OAMulator123,它支持 OAMPL (OAM 编程语言)和(编译)为OAM Assembly

所以我作弊了..
首先,我“解析”了你的 python 代码(我也不知道 python)并将它翻译成OAMPL

PRINT "Please enter the number of test scores to be entered:"
READ numberofscores
sumofscores = 0
LOOP numberofscores
  PRINT "Please enter the test score:"
  READ testscore
  sumofscores = (+ sumofscores testscore)
END
PRINT "the answer is:"
PRINT (/ sumofscores numberofscores)
EXIT

注意:我没有翻译您的float,因为它应该是int(尽管每个分数都可以是浮点数)。经过一些测试,我发现2.5 的输入无论如何都被“读取”为浮点数。我还丢弃了count 变量(因为LOOP 指令在没有它的情况下工作)和average 变量(因为我不需要它)...

单击compile 呈现(这可能是您的答案):

# Emitted by the OAMPL compiler
1.  BR  5   # branch to program block
# Variable storage allocation block
2. numberofscores,  NOOP    # variable numberofscores
3. sumofscores, NOOP    # variable sumofscores
4. testscore,   NOOP    # variable testscore
# Begin OAM program block
# OAMPL: PRINT "Please enter the number of test scores to be entered:"
5.  SET "Please enter the number of test scores to be entered:"
6.  STA stdout
# OAMPL: READ numberofscores
7.  LDA stdin
8.  STA numberofscores
# OAMPL: sumofscores = 0
9.  SET 0
10. STA sumofscores
# OAMPL: LOOP numberofscores
11. LDA numberofscores
12. BR L13
13. NOOP    # loop counter
# OAMPL: PRINT "Please enter the test score:"
14. SET "Please enter the test score:"
15. STA stdout
# OAMPL: READ testscore
16. LDA stdin
17. STA testscore
# OAMPL: sumofscores = (+ sumofscores testscore)
18. LDA testscore
19. STA 21
20. BR 22
21. NOOP    # intermediate value
22. LDA sumofscores
23. ADD 21
24. STA sumofscores
# OAMPL: END
25. LDA 13
26. DEC
27. L13,    STA 13
28. BRP 14
# OAMPL: PRINT "the answer is:"
29. SET "the answer is:"
30. STA stdout
# OAMPL: PRINT (/ sumofscores numberofscores)
31. LDA numberofscores
32. STA 34
33. BR 35
34. NOOP    # intermediate value
35. LDA sumofscores
36. DIV 34
37. STA stdout
# OAMPL: EXIT
38. HLT

鉴于input (one per line)

3
71.4
33
21.6

注意:对询问考试成绩数量的问题回答“3”,然后对询问个人考试成绩的问题回答“71.4”、“33”和“21.6”。 (这不是交互式输入,这让我呆了 15 分钟……这件事可能会从合并 META II 中受益匪浅)

如果我 execute 上面的 OAM 程序集(编译后!!)并将上面的输入提供给它,那么输出呈现:

Please enter the number of test scores to be entered:
Please enter the test score:
Please enter the test score:
Please enter the test score:
the answer is:
42

...神圣的 c..答案是 42这是所有问题的答案...
(这很酷.. 没有可用文档,猜测编程语言...这是我回答过的最酷的问题)

希望这会有所帮助!

PS:请在下面的 cmets 中添加一些指向此 OAMPL 和 OAM 程序集的文档/语法的链接!!! 在线文档的 (1, 2, 3) 我发现并没有真正的帮助(例如,'hello world' 示例中的WRITE 不起作用..等等,我不知道 OAMPL 支持什么语法,除了cheat-sheet found here)。

【讨论】:

  • 我会的!有没有办法将输入也打印到屏幕上?
  • 大写的 PS 部分适用于阅读它的任何人,并且有 OAMPL、OAM 汇编和 OAM 指令集参考:我*喜欢拥有这些数据,否则我们在 SO 上将无法帮助未来的学生。* 如果您能在这里分享,我(当然还有其他人)将不胜感激!要将输入打印到屏幕上,您只需执行以下操作:PRINT varname(例如:PRINT numberofscores)。
  • 既然你不接受这个答案,请问你不喜欢它的什么地方?另外,请不要忘记将我们链接到您承诺的文档!