In this project you will complete two utility functions that will be useful for processing the input for a disassembler (in phase 2). You will also complete a test driver to test your two functions. (A test driver is a main function written to "drive&

computer science


Project: Disassembler Utility Functions (Phase 1, 1/3 of final project)


In this project you will complete two utility functions that will be useful for processing the input for a disassembler (in phase 2). You will also complete a test driver to test your two functions. (A test driver is a main function written to "drive" tests of other functions.)


Get started:

The goal of the project is to gain more familiarity with C by writing some useful functions for a future programming project.


Download the code in zip file.


Files description:


Test driver, with main function


Utility function 1, used to verify the input format


Utility function 2, convert input bin to dec


indicates how to compile both this program and the future disassembler program


A simple input test file


A header file


processes command-line arguments passed to the main function; used in test driver


A header file for the following two print functions


Prints debugging messages, used in test driver


Prints error messages, used in verifyMIPSInstruction


Will be used in phase 2


Will be used in phase 2


The Makefile contains information for the make command, telling it how to compile this program (and the future disassembler program). Check the contents of Makefile to understand its structure. To compile this program, type make disUtil at a command-line prompt.


The make command will create a compiled executable called disUtil. You can run your program by typing its name at the command line. It takes two optional parameters: a filename and an integer 1 if you want to turn on debugging. If you run the program with the minimalist starter test file provided above, you'll see that for now it just prints the contents of the test file with a line number inserted at the beginning of each line.

./disUtil testfileStarter.txt

./disUtil testfileStarter.txt 1


Compare the test file with your output and then modify the test file and run the program again to verify its behavior. Test the program with and without the second parameter, 1.



Project description

The input file for the disassembler we will write shortly will contain strings representing MIPS instructions, one per line. Actual MIPS instructions would be stored in 32-bit integers; instead, our file will contain lines of 32 characters ('0' or '1'), where each line represents the 32 bits in a machine language MIPS instruction.


The purpose of the functions you are completing in this project is to read in the strings representing MIPS instructions, make sure they are valid, and decode subsets of the instructions in decimal format.



The main function in the test driver (disUtilDriver.c) currently reads lines in from a file until reaching the end of the file. If the line ends with a newline, the program strips the newline from the string by replacing it with a null byte. The code that is missing should call verifyMIPSInstruction to verify that the format contains a MIPS instruction in the proper format or print an error if it does not. If the instruction is valid, main should call binToDec several times, with various parameters, to test it effectively and print the results. (Until you have completed the missing code in binToDec, a single call will be a sufficient test.)

The verifyMIPSInstruction function should verify that the string provided to it is 32 characters long and that the characters in the string are all 0's or 1's (character '0' or character '1'). If the instruction is valid, verifyMIPSInstruction should return 1. Otherwise, it should print an error message indicating the error and the line on which it occurred before returning 0.


Test your modifications before going further by adding appropriate data to the existing test file or by creating new data files. Be sure to include tests cases that exercise every path through your code as well as all appropriate boundary conditions (e.g., empty lines, too-long lines, invalid data in the first / last positions, etc.).




The binToDec function should interpret a substring of characters in an array as a binary number, convert it to an integer in decimal format, and return the integer. The first parameter is the array of characters; the second and third parameters are the beginning index and ending index of the substring. For example, assume A is a character array that contains the following characters.

       1 0 1 1 0 1 0 0 1

The call binToDec(A, 2, 5) should convert the string of binary digits '1' '1' '0' '1' (the substring A[2] - A[5], inclusive) to the decimal integer 13 and return it. One efficient solution involves building the decimal number from the least significant digit to the most significant digit by adding in (or not) the appropriate power of two. Rather than calculating the power of two for each digit, you can store the power of two in a variable and multiply it by two when you go to the next digit. (What should the power-of-two variable be initialized to for the least significant digit?)

To test binToDec thoroughly, you should call it in a number of different ways whenever the instruction format is valid. Think particularly about what kinds of boundary conditions you should test for.





The Makefile I have provided specifies a set of compiler options that will help you catch many errors at compile time. These options generate warnings about questionable constructions that often indicate programmer confusion or actual logic errors. You may have to make adjustments to the Makefile, though, if the specific options or option names for your compiler are somewhat different.



One way to generate valid binary inputs is to use this online tool:

You can type a valid MIPS instruction and it will generate the binary and hex for you! (This is an assembler!) Copy the binary to your test file when the phase 2 is done, you should be able to reproduce the instruction.

Our work is to implement a disassembler function that is also available on this website(right side). Input a Hex format number and it will be reconstructed back to a MIPS instruction.


Submission requirement:

Your submission should contain:

  • All the source code for your program (including header file(s) and makefiles).
  • One or more sample input files containing all your test cases.
  • The output produced by your program for those input files.


Note: It is a good idea to run make clean in the directory before submitting; this will remove the machine-specific executable and intermediate "object code" files, since your code will have to be re-compiled by TA anyway.


Grading rubrics

1.      Compiles and runs: 10%

2.      Documentations: 10%

3.      Your own test cases: 20%. Your test cases should include valid and invalid instructions.

4.      verifyMIPSInstruction function: 30%, binToDec function: 30%. Your code will be tested with a test input which contains multiple lines valid and invalid binary instruction. For invalid lines, the verifyMIPSInstruction is able to print out an error message, and for valid lines, the binToDec should be able to parse the binary and print out the decimal value of subsection of the binary.

Related Questions in computer science category