Question

Should be written in Python: A disassembler takes a machine code file (binary) and converts it...

Should be written in Python:

A disassembler takes a machine code file (binary) and converts it back into assembly code. Disassemblers are a key tool in the software reverse engineering process because it allows you to go from the raw binary all the way back to the original source code, if desired. For this assignment you will be given 8 32-bit binary strings and the type of the instruction (I-type, R-type, or J-type). You’ll write a basic disassembler in Python that splits the 32-bit string into its corresponding fields, converts them from binary into their string representation, then reorders the fields, resulting in the original assembly code instruction. Here’s an example:

Here are the 8 machine code instructions to disassemble (it’s fine to hard-code them into your program):

1. instr1 = 00000001101011100101100000100100 (R-type)

2. instr2 = 10001101010010010000000000001000 (I-type)

3. instr3 = 00001000000000010010001101000101 (J-type)

4. instr4 = 00000010101010010101100000100010 (R-type)

5. instr5 = 00000011111000000000000000001000 (R-type)

6. instr6 = 00110101111100001011111011101111 (I-type)

7. instr7 = 10101110100011010000000000100000 (I-type)

8. instr8 = 00000010110011010101000000100000 (R-type)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi!

This is an interesting question. You will need to add more opcodes in Dictionary for it to work with other.

Code:

Nm instructions = || #List to store all functions instructions.append(00000001101011100101100000100100 R), instructions.app

Code that you can copy: (Take care of Indentation)

file.py

instructions = [] #List to store all functions

instructions.append("00000001101011100101100000100100 R")

instructions.append("10001101010010010000000000001000 I")

instructions.append("00001000000000010010001101000101 J")

instructions.append("00000010101010010101100000100010 R")

instructions.append("00000011111000000000000000001000 R")

instructions.append("00110101111100001011111011101111 I")

instructions.append("10101110100011010000000000100000 I")

instructions.append("00000010110011010101000000100000 R")

#Dictionary to store opcodes

opcodeDict = {'100011':'LW','000010':'J','001101':'ORI','101011':'SW','100100':'AND','100010':'SUB','100000':'ADD','001000':'JR'}

for i in instructions:

    instr,typeI = i.split() #type of instruction part is sepearted from the strings

    if(typeI=='R'):

        opcode = instr[0:6] #store first 6 digits to find opcode

        rs = instr[6:11]

        rt = instr[11:16]

        rd = instr[16:21]

        shamt = instr[21:26]

        func = instr[26:]

        if(opcodeDict[func]!='JR'):

            print("{}\tR{}\tR{}\tR{}\n".format(opcodeDict[func],int(rs,2),int(rt,2),int(rd,2)))

        else:

            print("{}\tR{}\n".format(opcodeDict[func],int(rs,2)))

    if(typeI=='I'):

        opcode = instr[0:6] #store first 6 digits to find opcode

        rs = instr[6:11]

        rt = instr[11:16]

        im = instr[16:]

        if(opcodeDict[opcode][-1]=='I'):

            print("{}\t{}\t{}\t#{}\n".format(opcodeDict[opcode],int(rs,2),int(rt,2),int(im,2)))

        else:

            print("{}\t{}\t{}(R{})\n".format(opcodeDict[opcode],int(rs,2),int(im,2),int(rt,2)))

    if(typeI=='J'):

        opcode = instr[0:6] #store first 6 digits to find opcode

        address = instr[6:]

        print("{}\t{}\n".format(opcodeDict[opcode],hex(int(address,2))))

Output:

AND R14 R11 R13 10 LW 8(R9) 0x12345 SUB R21 R9 R11 JR R31 16 #48879 ORI SW ADD 15 20 R22 32(R13) R13 R10

if you liked the answer, do give feedback.

Comment for help.

Add a comment
Know the answer?
Add Answer to:
Should be written in Python: A disassembler takes a machine code file (binary) and converts it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • If I am given a MIPS assembly instruction and asked to write the corresponding machine code...

    If I am given a MIPS assembly instruction and asked to write the corresponding machine code bit string representation how do I know if it is R Format, I Format, or J Format?

  • Help needed related python ! Thanx 6. What makes the exclusive-or the most interesting of the...

    Help needed related python ! Thanx 6. What makes the exclusive-or the most interesting of the logic operations in Boolean algebra? (Hint: try them and see.) 7. The How Computers Work Part III video from Week 1 talks about assembly level abstraction. Why is it better than machine code? Why is the assembly level abstraction still not abstract enough? Give the 2300 year old algorithm by Euclid for finding the greatest common divisor which is: 8. 9. Get two positive...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we...

    Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal? Given a binary string of an arbitrarily length k, bk-1....bi .box the decimal number can be computed by the formula 20 .bo +21.b, + ... + 2k-1. bx-1- In mathematics, we use the summation notation to write the above formula: k- 2.b; i=0) In a program,...

  • Please write code using Python. Rubric Assignment Solution Total: 100 pts Program successfully retrieves and validates...

    Please write code using Python. Rubric Assignment Solution Total: 100 pts Program successfully retrieves and validates user input 15 pts Morse Code Function Created 10 pts Function Uses Parameters to get input message 15 pts Function successfully converts passed message to Morse Code and returns the Morse Code equivalent of message 45 pts Program calls Morse Code function and outputs the result of the function (i.e. the actual Morse Code) 15 pts Main Function Requirement Part 2 MUST have a...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • using this code to complete. it python 3.#Display Program Purpose print("The program will show how much...

    using this code to complete. it python 3.#Display Program Purpose print("The program will show how much money you will make working a job that gives a inputted percentage increase each year") #Declaration and Initialization of Variables userName = "" #Variable to hold user's name currentYear = 0 #Variable to hold current year input birthYear = 0 #Variable to hold birth year input startingSalary = 0 #Variable to hold starting salary input retirementAge = 0 #Variable to hold retirement age input...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • # In this file, fill in the ... parts with lines of code. Do not # create new functions. from ran...

    # In this file, fill in the ... parts with lines of code. Do not # create new functions. from random import seed, randrange P=[" ♟♜♝♞♛♚"]; L,R,BL,TL=["▌▐▄▀"] BonR=WonR=WonB=DonR=DonB=RonB=GonR=GonB=RonG='\033[1;m\033[' WonR+='7;31;47m' # For drawing a white piece on a red background WonB+='7;30;47m' # For drawing a white piece on a black background DonR+='2;37;41m' # For drawing a dark piece on a red background DonB+='2;37;40m' # For drawing a dark piece on a black background GonR+='2;33;41m' # For drawing gold on a red...

  • My Python file will not work below and I am not sure why, please help me...

    My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT