0

I am trying to convert my python program into assembly language yet I am unsure how. Here is the python program:

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 
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
user2548833
  • 383
  • 1
  • 4
  • 7

1 Answers1

1

Hmm, I never heard of this teaching resource called One Address Machine and it's web-based OAMulator (1, 2, 3) that supports the OAMPL (OAM Programming Language) and (that compiles) to OAM Assembly.

So I cheated..
First, I 'parsed' your python-code (I don't know python either) and translated it to 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

Note: I didn't translate your float because it should be an int (although each score could be a float). After some testing I found that an input of 2.5 is 'read' as a float anyway. I also discarded the count variable (since the LOOP instruction worked without it) and average variable (since I didn't need it)...

Clicking compile renders (this is probably your answer):

# 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

Given the input (one per line):

3
71.4
33
21.6

Note: answers '3' to the question that asks for the number of test scores and subsequently answers '71.4', '33' and '21.6' to the question that asks for the individual test scores. (this is no interactive input, that threw me off for 15 minutes... this thing might seriously benefit from incorporating META II)

If I execute the above OAM assembly (after compiling!!) and feeding it the above input, then the output renders:

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

... holy c.. the answer is 42, it's the answer to everything...
(That's cool.. without usable documentation, guessing a programming language... this is somehow the coolest question I've answered)

Hope this helps!

PS: Please, add some links to the documentation/syntax of this OAMPL and OAM Assembly in the comments below!!! The online doc's (1, 2, 3) I could find didn't really help (for example, WRITE from the 'hello world' example didn't work.. etc. and I have no idea what syntax OAMPL supports, other than the cheat-sheet found here).

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • I WILL! is there not a way of printing the input to the screen as well? – user2548833 Jul 14 '13 at 00:33
  • *The capital **PS** part was meant for anyone reading it and has an OAMPL, OAM assembly and OAM instruction set reference: I'd *love* to have this data, otherwise it would be impossible for us on SO to help future students.*    I (and surely others) would be thankful if you'd share that here!      To print the input to the screen as well you'd simply do: `PRINT varname` (so for example: `PRINT numberofscores`). – GitaarLAB Jul 14 '13 at 02:30
  • Since you don't accept this answer, might I ask what you don't like about it? Also, please don't forget to link us to the documentation you promised! – GitaarLAB Jul 23 '13 at 14:25