2

I am having trouble writing an OAM program that generates a sequence of odd numbers with alternating signs starting with 1.

I have written a program but it does not work and I am not sure why. Because this is a rare language if anyone can help and explain what I am doing wrong that would be great.

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
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • This question is very unclear. How long is the sequence supposed to be? What are `X` and `Y` supposed to be used for (I'm guessing `X` is a loop counter and `Y` is the current sequence element, but that's not explained anywhere)? Generating a sequence implies the use of a loop, so why is there no branch instruction in your code? What's the point of a `SET` immediately followed by an `LDA`? What's the point of `STA X` immediately followed by `LDA X`? – Michael Mar 30 '13 at 11:09

1 Answers1

0

Note for other readers wondering what the heck this is all about:
an webbased One Address Machine compiler/assembler/emulator called OAMulator is here.

You ask for an OAM program that generates a sequence of odd numbers with alternating signs starting with 1 (without specifying the max value).
I'll interpret that you want the following output: 1, -3, 5, -7, 9, -11, 13, -15 etc....

I assume your LDA stdin is meant to ask for the max value, but you don't seem to store it with STA. Also you didn't start with branching (BR) to the 'program block' after initializing the your variables x and y. I also don't really see how your code is going to alternate sign...

I cheated by first writing the OAMPL code:

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

that will compile to the following 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

Note: you can also leave out the comments (# comment) and leading (line-) numbering.

If I enter 50 in the input-field (to answer the max value question) and hit execute (after compiling!), I get the following output result:

enter max number:
1
-3
5
-7
9
-11
13
-15
17
-19
21
-23
25
-27
29

Hope this helps!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80