Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import os, sys;
  2.  
  3. # ეს დიქშენარები არის გადაკოპირებული, იმიტომ რომ იგივე რომ დამეწერა მაინც იგივე კოდი გამოვიდოდა
  4. comp = {
  5. "0": "0101010",
  6. "1": "0111111",
  7. "-1": "0111010",
  8. "D": "0001100",
  9. "A": "0110000",
  10. "!D": "0001101",
  11. "!A": "0110001",
  12. "-D": "0001111",
  13. "-A": "0110011",
  14. "D+1": "0011111",
  15. "A+1": "0110111",
  16. "D-1": "0001110",
  17. "A-1": "0110010",
  18. "D+A": "0000010",
  19. "D-A": "0010011",
  20. "A-D": "0000111",
  21. "D&A": "0000000",
  22. "D|A": "0010101",
  23. "M": "1110000",
  24. "!M": "1110001",
  25. "-M": "1110011",
  26. "M+1": "1110111",
  27. "M-1": "1110010",
  28. "D+M": "1000010",
  29. "D-M": "1010011",
  30. "M-D": "1000111",
  31. "D&M": "1000000",
  32. "D|M": "1010101"
  33. }
  34.  
  35.  
  36. dest = {
  37. "null": "000",
  38. "M": "001",
  39. "D": "010",
  40. "A": "100",
  41. "MD": "011",
  42. "AM": "101",
  43. "AD": "110",
  44. "AMD": "111"
  45. }
  46.  
  47.  
  48. jump = {
  49. "null": "000",
  50. "JGT": "001",
  51. "JEQ": "010",
  52. "JGE": "011",
  53. "JLT": "100",
  54. "JNE": "101",
  55. "JLE": "110",
  56. "JMP": "111"
  57. }
  58.  
  59.  
  60. variable = {
  61. "SP": 0,
  62. "LCL": 1,
  63. "ARG": 2,
  64. "THIS": 3,
  65. "THAT": 4,
  66. "SCREEN": 16384,
  67. "KBD": 24576,
  68. "cursor": 16
  69. }
  70.  
  71.  
  72. for i in range(0, 16):
  73. st = 'R' + str(i)
  74. variable[st] = i
  75.  
  76.  
  77. # აქამდე გადაწერილია შემეზარა ამგენის დაწერა
  78.  
  79.  
  80. def getCommand(line):
  81. if (line.find('/') != -1):
  82. line = line[:line.find('/')]
  83.  
  84. lineList = line.split(' ')
  85. line = ''.join(str(x) for x in lineList)
  86. lineList = line.split('\n')
  87. line = ''.join(str(x) for x in lineList)
  88.  
  89. return line
  90.  
  91. def Abinary(line):
  92. if line[0].isalpha():
  93. if line in variable:
  94. r = variable.get(line)
  95. else:
  96. variable[line] = variable.get('cursor')
  97. variable['cursor'] = variable['cursor'] + 1
  98. r = variable[line]
  99.  
  100. else:
  101. r = int(line)
  102.  
  103. return str(bin(r)[2:].zfill(16))
  104.  
  105. def Cbinary(line):
  106.  
  107. if not "=" in line:
  108. line = "null=" + line
  109.  
  110. if not ";" in line:
  111. line = line + ";null"
  112.  
  113. binary = ''
  114. splitt = line.split('=')
  115. binary = dest.get(splitt[0])
  116.  
  117. splitt = splitt[1].split(';')
  118. binary = comp.get(splitt[0], 'damerxa') + binary
  119. binary += jump.get(splitt[1], 'damerxa')
  120.  
  121. return '111' + binary
  122.  
  123. def binary(line):
  124.  
  125. if line[0] == '@':
  126. return Abinary(line[1:])
  127.  
  128. return Cbinary(line)
  129.  
  130.  
  131. def firstRead():
  132. infile = open(root + ".asm")
  133.  
  134. i = 0
  135.  
  136. for line in infile:
  137. line = getCommand(line)
  138. if line == "":
  139. continue
  140.  
  141. if line[0] == '(':
  142. line = line[1:-1]
  143. variable[line] = i
  144. continue
  145.  
  146. i += 1
  147.  
  148. infile.close()
  149.  
  150. def readLines():
  151. infile = open(root + ".asm")
  152. outfile = open(root + ".hack", "w")
  153.  
  154. i = 0
  155.  
  156. for line in infile:
  157. line = getCommand(line)
  158. if line == "":
  159. continue
  160.  
  161. if line[0] == '(':
  162. continue
  163.  
  164. i = i + 1
  165. ans = binary(line)
  166. print(ans)
  167. outfile.write(ans + '\n')
  168.  
  169. outfile.close()
  170. infile.close()
  171.  
  172.  
  173. root = sys.argv[1]
  174.  
  175. firstRead()
  176. readLines()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement