0

我正在尝试将我的 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 
4

1 回答 1

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:请在下面的评论中添加一些指向此 OAMPL 和 OAM 程序集的文档/语法的链接!!!我能找到的在线文档123并没有真正的帮助(例如,WRITE从“hello world”示例中不起作用..等等,我不知道 OAMPL 支持什么语法,除了在这里找到备忘单)。

于 2013-07-12T03:33:06.753 回答