Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import sys # DO NOT EDIT THIS
  2.  
  3. '''
  4. This function should return the binary representation of an integer.
  5. Note that the digits in the string you return must be in the corect
  6. order, e.g. convert(6) must return '110'
  7. '''
  8. def convert(num):
  9. a = ''
  10. while num > 0:
  11. a = str(num % 2) + a
  12. num = num // 2
  13. return a
  14. pass
  15.  
  16.  
  17. '''
  18. This function should check if the 'target' string is less than 'length'
  19. characters long.
  20. If this is the case, 'pad_char' should be prepended to 'target' until the
  21. string is exactly 'length' characters long.
  22. If this is not the case, 'target' should be returned without modification.
  23. For example, pad('111', 3, '0') should return '111', and pad('rdvark', 8, 'a')
  24. should return 'aardvark'.
  25. '''
  26. def pad(target, length, pad_char='0'):
  27. while len(target) < length:
  28. target = pad_char + target
  29. return target
  30. pass
  31.  
  32.  
  33. '''
  34. This function should split an assembly language instruction into its opcode
  35. and operands, and return a tuple (opcode, [operand1, operand2, ...])
  36. For example, split_instruction('ADDI r1, r9, 17') should return
  37. ('ADDI', ['r1', 'r9', '17']). Note that all operands are still
  38. in string form, i.e. that '17' is not converted to an integer.
  39. '''
  40. def split_instruction(assembly_instr):
  41. list(assembly_instr)
  42. assembly_instr = assembly_instr.replace(',','')
  43. assembly_instr = assembly_instr.split()
  44. return assembly_instr[0],assembly_instr[1:]
  45. pass
  46.  
  47.  
  48. '''
  49. This function should convert an assembly language instruction to a binary
  50. string as described in the instructions for this lab, and return this string.
  51. Hint: You will want to use your convert(), pad(), and split_instruction()
  52. functions here
  53. '''
  54. def assemble(assembly_instr):
  55. # This is the table of opcodes for each of the assembly instructions
  56. # Do not modify this!
  57. opcodes = {'ADDU': 33, 'ADD': 32, 'ADDIU': 9, 'ADDI': 8, 'AND': 36,
  58. 'SW': 43, 'LW': 35, 'LH': 33, 'SLL': 0, 'SUBU': 35, 'SH': 41,
  59. 'MULTU': 25, 'MFLO': 18, 'NOP': 0, 'SRA': 3, 'LUI': 15,
  60. 'ANDI': 12, 'BEQ': 4, 'JR': 8, 'OR': 37}
  61.  
  62. # Use the split_instructions function to break up assembly_instr
  63. b,assembly_instr = split_instruction(assembly_instr)
  64. # Find the [opcode] in the above dict, convert() it to binary, and then pad() it.
  65. a = opcodes[b]
  66. a = convert(a)
  67. a = pad(str(a),6)
  68. # Loop through your operands and remove the r's. After that, convert() to binary and pad() with zeroes.
  69. # Remember you can print variables and lists out if you get confused!
  70. c = a
  71. for i in assembly_instr:
  72. if type(i) != int:
  73. d = i.replace('r','')
  74. else:
  75. d = i
  76. d = int(d)
  77. d = convert(d)
  78. e = pad(str(d),5)
  79. print(e)
  80. c = c + e
  81. return c
  82.  
  83.  
  84.  
  85.  
  86.  
  87. # Put together your results - a padded, binary "operation" followed by the padded, binary "operands".
  88. # Refer to unit tests and instructions to get an exact idea of what you need to return.
  89.  
  90. if __name__ == '__main__':
  91. # file_name is the name of the Super Mario 64 assembly input file, passed in via the first command line argument
  92. if len(sys.argv) < 2:
  93. raise Exception('you should have used one command line argument, i.e, python3 main.py argument')
  94.  
  95. e = []
  96. with open(sys.argv[1],'r') as word:
  97. d = word.readlines()
  98. i = 0
  99. for line in d:
  100. d[i] = assemble(line)
  101. d[i] += '\n'
  102. i += 1
  103. for f in range(len(d)):
  104. e.append(''.join(d[f]))
  105. with open('my_out.bin', 'w+') as g:
  106. for h in range(len(e)):
  107. g.write(e[h])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement