Computer Science Department
Johns Hopkins University
Computer Systems Fundamentals
Fall 2005

Working with SPIM

Jorge Vasconcelos

1. Introduction

Programming projects assigned during this course must be done with the MIPS R2000 Assembly language and tested on the simulator SPIM. This software uses a graphic user interface which works like most common GUI's. The CS Lab (225 NEB) has installed two versions of this software: XSPIM for Unix and PCSPIM for Windows NT. (If you want to work at home you may download the last version from

SPIM simulates the behavior of a MIPS-based syste: processor, main memory, and basic data input/output devices. The simulated system requires an Assembly program that is read from a text file and then executed.

The SPIM environment has six major sections:

2. Conductiong a session

  • In Unix

    Log into the undergraduate network and type xspim at the shell prompt. The spim environment will be launched and you will see a window with these parts: registers section, control buttons, code segment, data segment, and messages window (the console will appear when the simulation is running).

    To load a program use the load button and then write its full name. (It is very convenient to launch XSPIM from the very same directory where the program is located.)

    To run the loaded program click on the run button and select ok in the appearing window (

    To track your program (steb-by-step) click the step button and then click it each time you want to execute an instruction. (You will see the registers and code segment change.)

    If you are modifying your assembly program while using the simulator, then clear the memory and the registers (with the button clear) and click reload to assure that the newest version is utilized.

    To finish your session, just click the quit button.

  • In Windows

    Log into the Windows network and then click on the option PCSPIM that appears in the programs group.

    The PCSPIM window will be launched; you will only see the messages (session) window, and all other sections must be enabled through the option Windows (at the main menu)and resized if needed. To load a program use the option load from the menu file and then select the program in the appearing window.

    To run the program select the option run from the menu Simulator and then click on the button do not pay any attention to the value in the starting address field, except if it only have zeros). If you are using system calls to print data or request data, then the console window will appear, but it tends to dissapear immediately after it was shown, thus, you must open it with the menu Window.

    To track your program, steb-by-step, select the option step at the menu Simulator and then click it each time you want to execute an instruction. (You will see the registers and code segment change.)

    To finish your session, just select exit at the menu File.

    Sometimes there is a minor configuration problem on the PCSpim installed in our Windows network. If you are working on the NT machines, please verify that the Trap File has been properly loaded:

    Click on Simulator->Settings and near to the box named Load trap file should appear the name "Trap.handler". Otherwise, check the box and browse for that file; it is usually located at "C:\Program Files\PCSpim\Trap.handler".

    3. A trial program

    Write the following program using any editor for plain text (like pico, vi, x-windows text editor, Windows notepad, etc.). You may want to add the extesion .asm to the filename, to facilitate its identification (this is convenient but not required.)

    This program performs the addition of the first 100 natural numbers and displays the result. It exhibits all the basic MIPS features to create complex Assembly programs.

    # Comments:
    #          This program performs the addition of the first 100 
    #          natural numbers and displays the result.
    # Variables:
    #          $s1 - iterations counter
    #          $s4 - accumulator for sum
    #          $a0 - number of system service 
    message:  .asciiz "1+2+...+100 = "
    .globl main
          la $4, message       # $a0 <- start of welcome message 
          li $v0, 4            # $v0 <- service #4 
          syscall              # call to system service
          nop                  # not operation
          move $20, $zero      # $s4 <- 0, initialize accumulator 
          move $17, $0         # $s1 <- 0, initialize iterations counter
                               # Next two instructions mean "While $s1 < 100 Do"
    loop: slti $18, $17, 101  # $s1 < 100 => $s2 <- 1          
          beq $18, $0, end_lop # $s2 = 0 => go to end_loop
          add $20, $20, $17    # $s4 <- $s4 + $s1, add number
          add $17, $17, 1      # $s1 <- $s1 + 1, update counter of iterations
          j loop               # go to loop
    end_lop: move $4, $20      # $a0 <- $s4, load result of sum
          li $v0,1             # $v0 <- service #1 (data is already in $a0)
          syscall              # call to system service
    _exit:                     # main program exit
          li $v0,10            # $v0 <- service #10 
          syscall              # call to system service
    After loading and running the program you must obtain 5050 as result.

    Important information

    You will be required to submit your source code to the TA via email and he/she will test your programs under a Unix environment. Please send them as attachments of your message, in plain text format, and with the number of project as subject. Do not pack or compress your files in any form.

    Also, notice that transferring text files between Unix and Windows may modify the non-visible character at the end of each line, which might cause problems during the program execution. Therefore, it is very convenient testing your projects on the Unix network before submission to the TA.

    All the information you need to create MIPS programs may be found in the book Computer Organization and Design 3rd. ed., by Patterson, et al. CD-Appendix A, (it includes an Assembly language reference, system calls, assembler directives and exceptions). Additionally, all chapter 2 is devoted to analyze in depth the MIPS Assembly languge.

    NEW MIPS Quick Reference

    Copyright © 2000, 2005 Jorge Vasconcelos-Santillan. Johns Hopkins University, Department of Computer Science.

    Return to the CSF Homepage