嗯,我从未听说过这个名为 One Address Machine 的教学资源,它是基于网络的 OAMulator(1、2、3),它支持 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)。