What is SPIM?

·        SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors.

·        SPIM lets you test and debug assembly programs written for MIPS.

·        SPIM lets you see the contents of memory and registers when programs are running, i.e., to see what the instructions are doing.

Getting Started with SPIM

·        Go to the Tutorials section of the course web page and follow the link to SPIM.

·        Download the correct version of SPIM.  (Most of you will probably want the Windows version.)

·        Unzip and install the download file.

·        Run SPIM.

The SPIM Interface Display

The SPIM interface contains the following displays:

·        Register Display. 

à        General Registers

à        Double Floating Point Registers

à        Single Floating Point Registers

à        Program Counter

·        Text Segments.

à        Instructions from:

*        your code.

*        the system code that is loaded automatically.

à        Instruction Format:

*        Memory address, in hexadecimal.

*        Numerical encoding of instruction, in hexadecimal.

*        Instruction, in assembly code.

*        Line of code from original file, with line number.  (This may be missing if the instruction was generated by translating a pseudoinstruction.)

·        Data and Stack Segments.

à        Data loaded into program’s memory

à        Data on the program’s stack

·        SPIM Messages.

à        Error and status messages

SPIM Settings

·        Display.  You can display registers in decimal or hexadecimal.

·        Execution.  In general, you will want to:

à        Allow pseudoinstructions.

à        Load the trap file, which loads the standard exception handler and start-up code.

à        Turn other options off.

Loading and Running a Program

·        Open the file containing the assembly code.  This is just a plain ASCII file containing MIPS assembly code, saved with a .s extension.

·        Check if file was loaded successfully.

·        Simulator Go or F5 will run the program.

·        Check for error messages on completion.

Debugging a Program

·        To find errors:

à        If there are syntax errors, these will be caught when you try to load the file.

à        To track down runtime errors:

*        Single Step.  This runs the instructions one at a time, stopping after each instruction so that you can see its effect.

*        Multiple Step.  This runs a set number of instructions at once, stopping in between so you can see the effect. 

*        Breakpoints.  This runs all the code up to the breakpoint.  Then you can continue from there in whatever mode you want.  You can add and remove breakpoints as necessary.  When adding a breakpoint, you can type an address or a label.

·        To fix errors:

à        Edit and save the file.

à        Reload the file.  Memory and registers are automatically cleared, and the simulator is automatically reinitialized.

à        Retest the program in SPIM.

MIPS Assembler Directives

·        SPIM supports a subset of the MIPS assembler directives.

·        Some important ones are:

 

.data <addr>            #          store subsequent items in data segment,

                    starting at optional address.

.text <addr>          #          store subsequent items in text segment,

                    starting at optional address.

.asciiz str          #          store the string str in memory

                    and null-terminate it.

.byte b1,...,bn          #          store the n values in successive bytes

                    of memory.

.half h1,...,hn          #          store the n 16-bit quantities in

                    successive memory halfwords.

.word w1,...,wn          #          store the n 32-bit quantities in

                    successive memory words.

.float f1,...,fn          #          store the n fp single precision numbers

                    in successive memory locations.

.double d1,...dn          #          store the n fp double precision numbers

                    in successive memory locations.

 

·        See appendix section A.10 for additional directives as necessary.

System Calls

·        SPIM provides a small set of services through the system call instruction (syscall).  System call services include:

à        Printing output to the console: 

print_int (1),  print_float (2), print_double (3), print_string (4)

à        Reading a line of input: 

read_int (5), read_float (6), read_double (7), read_string (8)

à        Returning a pointer to a block of memory:  sbrk (9)

à        Stopping a program from running:  exit (10)

·        To request a service, the program:

à        Loads the system call code into register $v0.

à        Loads the arguments into registers $a0-$a3 (or $f12).

à        Makes the system call.

à        System calls place any return values in $v0 (or $f0).

System Calls:  An Example

     .data

str:

     .asciiz “the answer = “

     .text

     li  $v0, 4   # system call for print_str

     la  $a0, str # address of string to print

     syscall      # print the string

 

     li  $v0, 1   # system call for print_int

     li  $a0, 5   # integer to print

     syscall      # print it

What is the Start-Up Code Doing?

lw $a0, 0($sp)  # argc

addiu $a1, $sp, 4  # argv

addiu $a2, $a1, 4  # envp

sll $v0, $a0, 2

addu $a2, $a2, $v0

jal main

li $v0 10

syscall  # syscall 10 (exit)

main:

 

·        It gets argc, the number of arguments, from the stack and puts it in $a0.

·        It puts a pointer to the first argument in $a1.

·        ???

·        It jumps to the main routine (i.e., your code), and sets a link to return here when your code finishes.

·        It runs your code.

·        It returns and makes system call to exit.