MIPS Simulator for Assembly Language homework (MARS)


There is a MIPS Assembly language simulator with free downloads and the online documentation of the MARS simulator. The simulator is built on the SPIM assembler for MIPS, and there is generous, high-quality documentation of the MIPS assembly language with the free, online SPIM documentation for the MIPS assembler, and the SPIM Quick Reference (or an even shorter quick reference here)

The MARS software is distributed as a Java .jar file. It requires Java J2SE 1.5 or later. Depending on your configuration, you may be able to directly open it from the download menu. Java J2SE 1.5 or later. Depending on your configuration, you may be able to directly open it from the download menu.

If you have trouble, or if you prefer to run from the command line on your own computer, the Java SDK is is also available for free download from the same download page. The instructions for running it from Windows or DOS should work equally well on Linux. The CCIS machines should already have the necessary Java SDK installed.

Resources

  • MIPS Green Sheet
  • MIPS CPU layout
  • Useful Assembler Directives
    .align n
    Align the next datum on a 2^n byte boundary. For example, .align 2 aligns the next value on a word boundary. .align 0 turns off automatic alignment of .half, .word, .float, and .double directives until the next .data or .kdata directive.
    .asciiz str
    Store the string in memory and null-terminate it.
    .byte b1, ..., bn
    Store the n values in successive bytes of memory.
    .word w1, ..., wn
    Store the n 32-bit quantities in successive memory words.
    .data
    The following data items should be stored in the data segment. If the optional argument addr is present, the items are stored beginning at address addr.
    .space n
    Allocate n bytes of space in the current segment (which must be the data segment in SPIM).
    .text
    The next items are put in the user text segment. In SPIM, these items may only be instructions or words (see the .word directive below). If the optional argument addr is present, the items are stored beginning at address addr.
  • System Calls
    service system call code arguments result
    print_int 1 $a0 = integer
    print_float 2 $f12 = float
    print_double 3 $f12 = double
    print_string 4 $a0 = string
    read_int 5 integer (in $v0)
    read_float 6 float (in $f0)
    read_double 7 double (in $f0)
    read_string 8 $a0 = buffer, $a1 = length
    sbrk 9 $a0 = amount address (in $v0)
    exit 10

Gotchas

There are several important things to watch out for.
  1. When you hit the "Assemble" menu item, any error messages about failure to assemble are in the bottom window pane, tab: "Mars Messages". Input/Output is in the bottom window pane, tab: "Run I/O"
  2. If you paste assembly code into the edit window pane, you must save that code to a file before Mars will let you assemble it.
  3. If you have selected a box with your mouse (e.g. "Value" box in the data window pane, or "Register" box), then Mars will not update the value in that box. Mars assumes you prefer to write your own value into that box, and not to allow the assembly program to use that box.
  4. If your program stops at an error, read the "Mars Messages" for the cause of the error, and then hit the "Backstep" menu item to see what caused that error. Continue hitting "Backstep" or "Singlestep" in order to identify the error.
  5. Your main routine must call the "exit" system call to terminate. It may not simply call return ("jr $ra"). Note that page B-44 of Appendix B of the text (fourth edition) has a table of "system services" (system calls). These allow you to do "exit" and also I/O.
One of the nicer features of this software is a limited backstep capability (opposite of single-step) for debugging. In addition, the help menu includes a short summary of the MIPS assembly instructions. In general, I like this IDE for assembly even better than some of the IDEs that I have seen for C/C++/Java. (The one feature that I found a little unintuitive is that if you want to look at the stack (for example) instead of data, you must go to the Data Segment window pane, and use the drop-down menu at the bottom of that pane to choose "current $sp" instead of ".data".)

Please note the three sample assembly programs, along with an accompanying tutorial on the same web page. I'd appreciate if you could be on the lookout for any unusual issues, and report them promptly (along with possible workarounds), so the rest of the class can benefit. Thanks very much for your help on this.