SPIM and MIPS programming

The objective of this lab is to introduce you to the basics of MIPS assembly programming using xspim, a MIPS simulator.

  1. Download and read the short (4 page) introduction to xspim available at
  2. Using your favourite editor open the file hello.s. (It is written with tabs set to 4 characters.) Read it carefully and try to understand the basic structure of a MIPS assembly program.

    All that hello.s does is to print "hello world!" and then exit. The program uses MIPS assembly pseudo-instruction la $a0, msg, which loads register $a0 with the memory address where msg is held. Printing (and exiting) is done by the special instruction syscall. For now, think of syscall as a special procedure call which is already provided by xpsim. You will get a more complete picture when exceptions and interrupts are discussed later in this course.

    The exact task that syscall performs depends on the value stored into register $v0. To find out what other syscall options are made available by spim, see page A-44 of Appendix A of the Patterson and Hennessy book. We will be using some of these for input/output.

  3. Start xspim by typing "xspim" on a terminal window of a DICE machine. A MS Windows version, called pcspim, is available from the spim web page and looks very similar to xspim.

    Load hello.s into spim.

    Step through the program using the "step" button, observing the changes to the registers and the currently executed instruction. A few instructions that are not part of your program are executed before "main" is called. You don't have to understand how this part works yet. The first instruction of your program that gets executed is the one marked with the label "main".

    Use the hello.s program to familiarise yourselves with the buttons and menus available in xspim.

  4. (Note: The following uses instructions that will be introduced in the second lecture on MIPS instructions, scheduled for 4/10/06. You can go ahead and read the notes already provided and/or the descriptions of the instructions in the Appendix A of the textbook, but if you have problems, you can stop at this point and continue after the second lecture on MIPS instructions).

    Open hexOut.s in an editor and try to understand what it does. The first two syscalls prompt the user for a number and get the number from the keyboard. Then a message is printed. The remaining of the program, extracts each hexadecimal digit from the provided number, converts it into the corresponding ASCII character and prints it (using yet another syscall). This is done eight times, as there are 8 hex digits in a 32-bit word.

    Use xspim to load and step through the program to find out exactly what each instruction is doing. Set a breakpoint (explained in the spim tutorial) at the instruction just before the label "loop", so that you can let the program run through quickly the part where it asks for input and focus on what happens in the loop.

  5. You can now practise writing your own assembly programs. Here are some suggestions:
    • Based on hexOut.s write a program, binOut.s that asks for a number and prints it in binary format, digit by digit.
    • Modify hexOut.s so that when a negative number is entered, it is printed as '-', followed by the two's complement of the number (in hex again).
    • Modify hexOut.s so that leading zeros are not printed.

Home : Teaching : Courses : Inf2c : Labs 

Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail:
Please contact our webadmin with any comments or corrections. Logging and Cookies
Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh