Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys # DO NOT EDIT THIS
- '''
- This function should return the binary representation of an integer.
- Note that the digits in the string you return must be in the corect
- order, e.g. convert(6) must return '110'
- '''
- def convert(num):
- a = ''
- while num > 0:
- a = str(num % 2) + a
- num = num // 2
- return a
- pass
- '''
- This function should check if the 'target' string is less than 'length'
- characters long.
- If this is the case, 'pad_char' should be prepended to 'target' until the
- string is exactly 'length' characters long.
- If this is not the case, 'target' should be returned without modification.
- For example, pad('111', 3, '0') should return '111', and pad('rdvark', 8, 'a')
- should return 'aardvark'.
- '''
- def pad(target, length, pad_char='0'):
- while len(target) < length:
- target = pad_char + target
- return target
- pass
- '''
- This function should split an assembly language instruction into its opcode
- and operands, and return a tuple (opcode, [operand1, operand2, ...])
- For example, split_instruction('ADDI r1, r9, 17') should return
- ('ADDI', ['r1', 'r9', '17']). Note that all operands are still
- in string form, i.e. that '17' is not converted to an integer.
- '''
- def split_instruction(assembly_instr):
- list(assembly_instr)
- assembly_instr = assembly_instr.replace(',','')
- assembly_instr = assembly_instr.split()
- return assembly_instr[0],assembly_instr[1:]
- pass
- '''
- This function should convert an assembly language instruction to a binary
- string as described in the instructions for this lab, and return this string.
- Hint: You will want to use your convert(), pad(), and split_instruction()
- functions here
- '''
- def assemble(assembly_instr):
- # This is the table of opcodes for each of the assembly instructions
- # Do not modify this!
- opcodes = {'ADDU': 33, 'ADD': 32, 'ADDIU': 9, 'ADDI': 8, 'AND': 36,
- 'SW': 43, 'LW': 35, 'LH': 33, 'SLL': 0, 'SUBU': 35, 'SH': 41,
- 'MULTU': 25, 'MFLO': 18, 'NOP': 0, 'SRA': 3, 'LUI': 15,
- 'ANDI': 12, 'BEQ': 4, 'JR': 8, 'OR': 37}
- # Use the split_instructions function to break up assembly_instr
- b,assembly_instr = split_instruction(assembly_instr)
- # Find the [opcode] in the above dict, convert() it to binary, and then pad() it.
- a = opcodes[b]
- a = convert(a)
- a = pad(str(a),6)
- # Loop through your operands and remove the r's. After that, convert() to binary and pad() with zeroes.
- # Remember you can print variables and lists out if you get confused!
- c = a
- for i in assembly_instr:
- if type(i) != int:
- d = i.replace('r','')
- else:
- d = i
- d = int(d)
- d = convert(d)
- e = pad(str(d),5)
- print(e)
- c = c + e
- return c
- # Put together your results - a padded, binary "operation" followed by the padded, binary "operands".
- # Refer to unit tests and instructions to get an exact idea of what you need to return.
- if __name__ == '__main__':
- # file_name is the name of the Super Mario 64 assembly input file, passed in via the first command line argument
- if len(sys.argv) < 2:
- raise Exception('you should have used one command line argument, i.e, python3 main.py argument')
- e = []
- with open(sys.argv[1],'r') as word:
- d = word.readlines()
- i = 0
- for line in d:
- d[i] = assemble(line)
- d[i] += '\n'
- i += 1
- for f in range(len(d)):
- e.append(''.join(d[f]))
- with open('my_out.bin', 'w+') as g:
- for h in range(len(e)):
- g.write(e[h])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement