## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

from math import ceil

# representation of a single line of machine code
# defined via each line of the input csv file

# bit width definitions
op_bits = 4
rd_bits = 3
rs1_bits = 3
rs2_bits = 3
imm_bits = 12
all_bits = op_bits + rd_bits + rs1_bits + rs2_bits + imm_bits

min_register_number = 0
max_register_number = pow(2, rd_bits) - 1

# -2^(imm bits) to 2^(imm bits) - 1
min_immediate = -pow(2, imm_bits)
max_immediate = pow(2, imm_bits) - 1

# dictionary for opcode lookup
opcode_dict = {
'NOT': 0,
'AND': 1,
'XOR': 2,
'OR' : 3,
'SUB': 5,
'MOV': 6,
'MSW': 7,
'MPC': 8,
'MVI': 9,
'CMP': 10,
'JMP': 11,
'SLL': 12,
'SRL': 13,
'HLT': 14,
'HCF': 15
}

# https://stackoverflow.com/a/12946226
def bindigits(n, bits):
s = bin(n & int("1"*bits, 2))[2:]
return ("{0:0>%s}" % (bits)).format(s)

class MachineCode:

def __init__(self, op, rd, rs1, rs2, imm, row):
# input variables taken from CSV file
self.op = str(op)
self.rd = str(rd)
self.rs1 = str(rs1)
self.rs2 = str(rs2)
self.imm = str(imm)

# binary variables after checking originals for correctness
self.op_bin = None
self.rd_bin = None
self.rs1_bin = None
self.rs2_bin = None
self.imm_bin = None

# final concatenation variables
self.bin = None
self.hex = None

# debugging
self.row = str(row)

def convert_op(self):
try:...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

\$138.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.