我在编写一个OAM程序时遇到问题,该程序生成一系列奇数,并以1开头交替显示。
我已经编写了一个程序,但它不起作用,我不知道为什么。因为这是一种罕见的语言,如果有人可以帮助和解释我做错了什么会很好。
X,NOOP
Y,NOOP
LDA stdin
loop, SET 1
LDA Y
STA 0
LDA X
DEC
STA X #X = X - 1
LDA X #ACC = X
答案 0 :(得分:0)
请注意其他读者想知道这是怎么回事:
一个名为One Address Machine
的基于web OAMulator
is here编译器/汇编器/模拟器。
您要求OAM program that generates a sequence of odd numbers with alternating signs starting with 1
(未指定最大值)
我将解释您需要以下输出:1, -3, 5, -7, 9, -11, 13, -15 etc...
。
我认为您的LDA stdin
是要求最大值,但您似乎不会将其与STA
一起存储。在初始化变量BR
和x
后,您也没有开始将“y
”分支到“程序块”。我也没有真正看到你的代码如何替换标志......
我首先写了OAMPL
代码:
n = 1 #Init number n as 1
PRINT "enter max number:" #Ask for max number
READ i #Read that number from input, storing it as i
LOOP (/ i 2) #Calculate number of iterations, passing that to LOOP
IF t #If t (toggle)
t = 0 #Toggle falsy
PRINT (- 0 n) #Print 0-counter
ELSE
t = 1 #Toggle truthy
PRINT n #Print counter
ENDIF
n = (+ n 2) #Increase counter by 2
END
EXIT #Exit program
将编译为以下OAM Assembly
:
# Emitted by the OAMPL compiler
1. BR 5 # branch to program block
# Variable storage allocation block
2. n, NOOP # variable n
3. i, NOOP # variable i
4. t, NOOP # variable t
# Begin OAM program block
# OAMPL: n = 1
5. SET 1
6. STA n
# OAMPL: PRINT "enter max number:"
7. SET "enter max number:"
8. STA stdout
# OAMPL: READ i
9. LDA stdin
10. STA i
# OAMPL: LOOP (/ i 2)
11. SET 2
12. STA 14
13. BR 15
14. NOOP # intermediate value
15. LDA i
16. DIV 14
17. BR L18
18. NOOP # loop counter
# OAMPL: IF t
19. LDA t
20. BRZ I20
# OAMPL: t = 0
21. SET 0
22. STA t
# OAMPL: PRINT (- 0 n)
23. LDA n
24. STA 26
25. BR 27
26. NOOP # intermediate value
27. SET 0
28. SUB 26
29. STA stdout
# OAMPL: ELSE
30. BR I30
31. I20, NOOP
# OAMPL: t = 1
32. SET 1
33. STA t
# OAMPL: PRINT n
34. LDA n
35. STA stdout
# OAMPL: ENDIF
36. I30, NOOP
# OAMPL: n = (+ n 2)
37. SET 2
38. STA 40
39. BR 41
40. NOOP # intermediate value
41. LDA n
42. ADD 40
43. STA n
# OAMPL: END
44. LDA 18
45. DEC
46. L18, STA 18
47. BRP 19
# OAMPL: EXIT
48. HLT
注意:您也可以省略评论(# comment
)和排行(行号)编号。
如果我在50
字段中输入input
(以回答最大值问题)并点击execute
(编译后!),我会得到以下output
结果:
enter max number:
1
-3
5
-7
9
-11
13
-15
17
-19
21
-23
25
-27
29
希望这有帮助!