Computer Organization: Program 1

Disassembler

Due: Tuesday, October 29 at 3pm


Goals: to understand how MIPS instructions are encoded and to gain practice using bit manipulations and switch statements in C.


What you need to do:

  1. read this document in its entirety
  2. implement the disassembler
  3. thoroughly test your program
  4. create a readme.txt file
  5. place a copy of your source code on the science server and indicate (reply to the email officially announcing this assignment) the full path of the folder in which it resides (e.g., /home/msiff/computer-organization/disassembler/).

Details

A disassembler breaks machine-code instructions into their constituent pieces (opcode, etc.) and uses that information to produce assembly language. This is possible because there is (essentially) a one-to-one correspondence between assembly language and machine code.

In general, disassembly facilitates debugging of machine programs, since it can print out readable assembly code to go along with the machine code. It also makes it possible to build “user friendly” simulators.

For this assignment you should implement (at least) two functions, showBits and disassemble. Their descriptions are in this C code. You need only fill in the two functions bodies (marked by /* ... */), though you are strongly encouraged to break the disassembly process down into smaller pieces and to write and use auxiliary functions to make the code easier to develop and to read. You may modify other parts of the program if you wish. (The supplied code should compile and run as is. It outputs the hexadecimal representation of the base-ten integer input. You just need to add functionality to showBits and disassemble.)

The program is designed to take advantage of input/output redirection capabilities of UNIX-style operating systems. For example, assuming that a file called test.mc contains the following:

  0
  62750752
  16416
  585696333
  283377659
  134217729
  201326602
  65011720
  2411266071

Then you can run your program as:

  ./disassemble < test.mc

and it should produce output like:

    0000 0000 0000 0000 0000 0000 0000 0000   00000000  sll  $zero, $zero, 0
    0000 0011 1011 1101 1000 0000 0010 0000   03BD8020  add  $s0, $sp, $sp
    0000 0000 0000 0000 0100 0000 0010 0000   00004020  add  $t0, $zero, $zero
    0010 0010 1110 1001 0000 0010 1111 1101   22E902FD  addi $t1, $s7, 765
    0001 0000 1110 0011 1111 1111 1111 1011   10E3FFFB  beq  $a3, $v1, -20
    0000 1000 0000 0000 0000 0000 0000 0001   08000001  j    4
    0000 1100 0000 0000 0000 0000 0000 1010   0C00000A  jal  40
    0000 0011 1110 0000 0000 0000 0000 1000   03E00008  jr   $ra
    1000 1111 1011 1001 0000 0000 0001 0111   8FB90017  lw   $t9, 23($sp)

The first 8 columns are the 32-bit binary representation of each instruction. The next column is the hexadecimal representation of the instruction. The last part is the assembly language translation of the machine code, with the difference that numbers rather than labels are produced for branch and jump target addresses.

MIPS subset

You need only implement a small subset of MIPS, namely:

  add, addi, sub, slt, slti
  sll, srl
  lw, sw
  beq, bne
  j, jal, jr

Registers

The registers you need represent are:

  names   numbers
  ------- ---------
  zero    0
  v0,v1   2,3
  a0-a3   4-7
  t0-t7   8-15
  s0-s7   16-23
  t8,t9   24,25
  sp      29
  ra      31

Compilation

Just use the standard C compiler as in:

  gcc -o disassemble -Wall -ansi disassemble.c 

Execution and testing

You can run the generated executable by typing:

  ./disassemble

from within the directory containing the executable. For testing, it will be helpful to take advantage of standard input redirection as in:

  ./disassemble < test.mc

which is the equivalent of running disassemble and then typing in the code in the file test.mc.

Example

machine code from above and the output it should generate.

Your code…

… should be very readable. It should be well-formatted (indented consistently and appropriately), cleanly presented (do not hesitate to use spaces and comments to separate different parts of the code), and well documented. Include a comment atop the program with your name. Make sure you test your code thoroughly (not just on the supplied examples; you should craft some of your own).

Place your final submission on the science server. Indicate (reply to the official email announcing this assignment) the full path of the folder where your solution resides as in:

/home/msiff/computer-organization/disassembler/

Please use a folder dedicated for this assignment. It should have at least two files: your .c source code and your readme.txt. (It can also have others, for example the compiled solution and test files.) Your readme.txt should clearly, but concisely describe how much of the assignment you completed and any known bugs or limitations that remain.

Include your name in your source code and readme.


Extra credit

Augment your disassembler so that it generates labels and symbolic addresses for branches and jumps. Only work on the extra credit if you have a fully working version of the main assignment and you clear it with me first.