SPIM MIPS Simulator SPIM A MIPS32 Simulator Senior Researcher, Microsoft Research Formerly: Professor, Computer Sciences Department, University of Wisconsin-Madison spim is a self-contained simulator that will run MIPS32 assembly language programs. It reads and executes assembly language programs written for this processor.
Spim also provides a simple debugger and minimal set of operating system services. Spim does not execute binary (compiled) programs. Spim implements almost the entire MIPS32 assembler-extended instruction set. (It omits most floating point comparisons and rounding modes and the memory system page tables.) The MIPS architecture has several variants that differ in various ways (e.g., the MIPS64 architecture supports 64-bit integers and addresses), which means that spim will not run programs compiled for all types of MIPS processors.
MIPS compilers also generate a number of assembler directives that spim cannot process. These directives usually can be safely deleted. Earlier versions of spim (before 7.0) implemented the MIPS-I instruction set used on the MIPS R2000/R3000 computers. This architecture is obsolete (though, has never been surpassed for its simplicity and elegance).
Spim now supports the more modern MIPS32 architecture, which is the MIPS-I instruction set augmented with a large number of occasionally useful instructions. MIPS code from earlier versions of SPIM should run without changes, except code that handles exceptions and interrupts. This part of the architecture changed over time (and was poorly implemented in earlier versions of spim). Code of this sort need to be updated.
Examples of the new code are in exceptions.s and Tests/tt.io.s. Spim comes with complete source code and documentation. It also include a torture test to verify a port to a new machine.
Spim implements both a simple, terminal-style interface and a window interface. On Unix, Linux, and Mac OS X the spim program provides a simple terminal interface and the xspim program provides the windowing interface. On Microsoft Windows, the spim program provides a console interface and PCSpim provides a Windows interface. Downloading SPIM. Platform Program Form File Unix or Linux system Mac OS X spim xspim Source code or Linux spim xspim Binary RPM for Fedora Microsoft Windows (Windows NT, 2000, XP) (spim 7.0 and later versions no longer run on Windows 95/98. Use version 6.5 or earlier.) spim PCSpim Executable Source code.
To run spim or xspim on a Unix, Linux, or Mac OS X system, copy either the compressed tar file or the gzip'ed tar file. Both files contains source code and must be compiled for your system.
Ben Liblit of the Cooperative Bug Isolation Project produced a compiled version of spim for Fedora. It contains the project's low-overhead monitoring code, which can help find bugs in spim. If you are running Linux, this package can: save you the time and trouble of compiling and installing spim, help find bugs, and aid an interesting research project. To run PCspim under Microsoft Windows, download the file, unzip it, and click on setup.exe. Source for the Microsoft Windows version (both PCspim and spim) is available separately in the file. There is no Macintosh version of spim.
However, the xspim will compiles and runs on Mac OS X. Installation Microsoft Windows.
Download the file and save it on your machine. Unzip the file. Click on the setup.exe program.
Unix, Linux, or Mac OS X (Note: the directions changed slightly for version 7.2, to reflect a new directory structure. Please read carefully.) Installation is a bit more complex for a Unix or Linux system, as you need to compile the program for your particular computer and operating system. Download either the file.
Decompress the file, using either the program uncompress for the first file or gzip for the second file:% uncompress spim.tar.Z or% gzip -d spim.tar.gz. Move the file spim.tar to the directory in which you want to build spim and untar it:% tar xf spim.tar It will create a directory named spim-7.2 (or the most recent version number).
The simple terminal interface is contained in the spim-7.2/spim directory and the X-windows interfaces is in the spim-7.2/xspim directory. The other directories are described in the README file. Next, you must set the directories in which spim will be installed by editing the Makefile (the file that contains instructions on building spim).
In general, if you are installing spim and want the windowing version ( xspim), edit the file xspim/Imakefile. If you don't want xspim or are running on a system without X-windows installed, you use the file spim/Makefile. Set these pathnames to the appropriate locations for your system: EXCEPTIONDIR - The full pathname of the directory in which to install the spim exception handler (exceptions.s). BINDIR - The full pathname of the directory in which spim and xspim should be installed.
MANDIR - The full pathname of the directory in which the manual pages for spim and xspim should be installed. In general, the remaining parameters in a Makefile need not be changed. Then, if you are using Imakefile file, change to the spim-7.2/xspim directory and type:% xmkmf% make If you do not have a copy of xmkmf, you can use the Makefile in the xspim directory, but beware that it may not work on your system because the paths to the X windows libraries could be different.
If you do not have X-windows, change to the spim-7.2/spim directory, edit Makefile, and type:% make. To run spim or xspim, the exception handler must be installed in the directory specified by the variable EXCEPTIONDIR in the Makefile.
If the file exception.s is not installed, spim and xspim fail before they start running. You can either install this file by hand or by typing% make install which also installs spim or xspim, and the manual pages in the directories that you set (above).
To test that spim is correctly built, change to the spim-7.2/spim directory and type:% make test and examine the output of the test. (Note: the exception handler must be installed before running the test.) Resources. Elsevier (aka Morgan Kaufmann) has generously provided the spim documentation (Appendix A) from the third edition of Hennessy & Patterson,. This documentation is far more complete and up-to-date than the documentation included in the spim distribution.
The COD documentation includes:. Appendix A: Assemblers, Linkers, and the SPIM Simulator. An overview and reference manual for spim and the MIPS32 instruction set. Getting Started with spim. Materi persamaan garis lurus kelas viii. Overview of the console version of spim (both Unix and Windows).
Getting Started with xspim. Overview of the X-windows version of spim. Getting Starting with PCSpim. Overview of the Microsoft Windows version of spim.
SPIM Command-Line Options. Overview of the command line options of spim (all versions). The documentation included with spim is also online (as a or ). This document is for the original spim (pre-version 7.0), which simulated the MIPS-I architecture rather than MIPS32, and so differs in many minor aspects from the current version of spim.
Appendix A of Hennessy & Patterson, (above), is more up-to-date and correct. The best book on MIPS programming is: Dominic Sweetman, Morgan Kaufmann, San Francisco, CA, 1999, ISBN 1-55860-410-3. Has excellent free documentation on the MIPS32 architecture on their website (registration required):. v2.00 (1249 KB). v2.00 (3258 KB).
v2.00 (1507 KB). An old reference manual for the MIPS architecture (including processors subsequent to the R2000/R3000) is: Gerry Kane and Joe Heinrich, MIPS RISC Architecture, Prentice Hall, Englewood Cliffs, N.J.
07632 ISBN 0. Another good book for MIPS R2000 and R3000 programmers is: Erin Farquhar and Philip Bunce, Morgan Kaufmann, San Francisco, CA, 1994, ISBN 1-55860-297-6. The GNU C compiler can generate code for the MIPS R3000 and can easily be used as a cross compiler. A retargeted port of gcc is available on the.
I have not used this compiler. Downcast Systems offers a very nice text editor for MIPS assembly code called. It has syntax highlight, MIPS-specific help, good integration with SPIM, and a number of other features to make it easier to write assembly code. MIPSter is an inexpensive commercial product, and it offers a limited time trial version.
Changes in Latest Version The current version of spim is 7.3 (August 2006), which is a minor release that cleans up a number of bugs in 7.2:. Fix for flex 2.5.33 (internal change broke spim's scanner). Exception handler can be a semicolon-separated list of files that will be loaded in order. On Unix, SPIMEXCEPTIONHANDLER environment variable is consulted before command line arguments. Memory corruption caused spim to crash when label was defined twice in succession.
PCSpim updates register and memory display after a parse error while reading a file. Eliminated unnecessary null char between strings in list of strings produced by.asciiz. Makefiles (in spim/ and xspim/) now contain clearly defined paths for installing executables, exception handler, and man pages. The man pages are no longer installed by the 'make install' command.
Change psfig to epsfig in spim.tex (does anyone still use this obsolete documentation?). The previous version of spim is 7.2.1 (August 2005), which is a minor release that cleans up a number of bugs in 7.1:. Fixed multiple problems in memory mapped IO in xspim and spim. (Note: to make programming uniform across all three platforms, memory mapped IO now follows'Unix' handling of carriage return (CR) and new line (NL) characters. On input, a CR is translated to a NL, and on output, a CR is printed after a NL.
This was previously true for PCSpim and xspim, but not spim.). Fixed bug in indexed address computation in LW and SW instructions. Eliminated non-POSIX flags IUCLC and IXANY to facilitate port to Mac OS X. Use termios(3) library calls instead of IOCTLs to fix bug introduced in port from termio to termios struct. Test default case for jalr, without default $rd (=31).
Trap handler in test extracted wrong bits from ExcCode field of Cause register. Pervasive restructuring of files to put 3 UIs (spim, xspim, PCSpim) in separate directories and cleanup organization and Makefiles. Use correct help file in install process. Added work-around for bug in flex 2.5.31 that left yytextptr undefined.
Epilogue code restored $at register, then used a pseduo instructions that smashed it. Cleared filename when reinitializing PCSpim, which broke next reload command. The previous version of spim is 7.1 (January 2005), which is a minor release that cleans up a number of bugs in 7.0:. Minor cleanup. Switched Windows installer to Microsoft.msi installer, from InstallShield.
exceptions.s used wrong mask for exception cause field. Clearing the transmitter or receiver interrupt enable bits also clear interrupt pending bits in Cause register. Code to poll for a console input character in X windows blocked and prevented any output until input occured. Type of 'mul' instruction was wrong, which lead to it being printed without destination register.
Print underlying instruction, not breakpoint. Unparing error that unparsed beq as beql.
Pa teacher license lookup. Eliminated dependence on obsolete termio.h in favor of POSIX termios.h since Mac OS X doesn't have termio.h any more. Fix null pointer problem if no entries in registry.
Turn off EXL bit after break instruction, since break is handled by spim, not MIPS, code and the bit was never cleared. Set default path for exceptions.s to C: Program Files PCSpim, the default installation directory.
Test for SQRT.S used double constant, which failed on SPARC (but not x86). The previous version of spim is 7.0 (August 2004), which is a major release that contains a significant number of changes from version 6.5:. Upgraded simulator to MIPS32, Version 1 architecture (except details of FPU and memory). Exceptions and interrupts reimplemented, to make them closer to the actual hardware. Memory mapped IO console reimplemented to make it work properly. Implemented timer in CPU.
Renamed 'trap' to 'exception' to correspond to MIPS terminology. Introduced short forms of command line arguments and dropped need for '-file' argument. Major internal cleanup and reorganization of code to eliminate unused code and simplify implementation. PCSpim prompts about reinitialization when file reloaded. PCSpim lets user set font. Lex source. Word align all segment boundaries and ensure sbrk returns word-aligned addresses.
Added new syscall (EXIT2SYSCALL) that returns value from SPIM. Improved implementation of delayed load instructions. Refactored the test cases, so that endian-independent code is in tt.core.s. Many small bug fixes. Outstanding bugs in 7.0:. The trap handler (exceptions.s) fails when the trapping instruction is in the delay slot of a branch or jump.
The trap handler returns by simplemindedly adding 4 to the EPC (which is the branch's address in this case), so it does not return to the instruction executed after the trapping instruction. Handling of newlines and carriage returns is inconsistent between platforms. When a program prints a newline, PCSpim also prints a carriage return, spim and xspim don't.
Or, is this consistent with the underlying platform? The previous version of spim is 6.5 (January 2003), which contains the following changes from version 6.4:. If started program is started with only one argument, assume it is a file name. Added Alt-F4 shortcut to PCSpim.
Fixed trap.handler to return to instruction at EPC on interrupt. Fixed (real!) hardware exception on integer overflow in division.trapfile did not properly parse argument in xspim. Fixed printing of error on last line without a carriage return. Print proper registers when printing hex FP numbers.
Properly zero newly allocated memory. Added nops (addu $0, $0, 0) to trap handler, so that it works properly in bare mode!. PCSPIM.HLP file was corrupted. The previous version of spim is 6.4 (January 2002), which contains the following changes from version 6.3:.
PCSpim no longer shifts the focus of the register and data segments when values change. Fixed bug in the expansion of USH pseudo-op. Add system calls for open, close, read, and write. Fixed the Configure file to work with Mac OSX.
Made the REM pseudo-op require 3 arguments. Fixed PCSpim dumping code to dump contents of console window. Treat LUI's immediate value as unsigned, as it is not sign extended. More error checking for malformed programs. Make symbol 'main' as global. Avoid clearing last few bytes of data segments when they expand.
Check that high-order 4 bits in jump instruction's PC match bits in target PC. The previous version of spim is 6.3 (January 2001), which contains the following changes from version 6.2:. PCSpim interface simplified to 4 fixed panes, similar to xspim, which permit cut and paste. PCSpim console stays on the screen after assembly program terminates.
Comments in assembly source displayed in text segment. NOP is standardized to sll $0 $0 0 (= 0x00000000). Fixed several bugs and missed optimization in computing immediate values in lw/sw instruction sequences. High 2 bits in negative branch offsets were incorrect. Display floating point registers with up to 18 digits of precision. New spim command (printallregs) prints all registers. Minor changes to compile spim under cygwin for Windows.
Validate the range of immediate values in MIPS (but not pseudo) instructions. Fixed display of stack with non-word aligned $sp. New flags:.delayed-branches Simulate MIPS delayed control transfers for branches, jumps, and calls.delayed-loads Simulate MIPS non-interlocked loads. Copyright spim is copyrighted by James R. Larus and distributed under the following conditions:. You may make copies of spim for your own use and modify those copies. All copies of spim must retain my name and copyright notice.
You may not sell spim or distribute spim in conjunction with a commercial product or service without the expressed written consent of James Larus. THIS SOFTWARE IS PROVIDED ``AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The DOS and Windows ports were done by David A. Carley and are copyrighted by Morgan Kaufmann Publishers. SPIMSAL Note spimsal is an old version of spim that ran on and. Spimsal implements an extended version of the MIPS instruction set and is based on an old version of spim. It also does NOT run under Windows 95 or Windows NT.
Getting PCSpim and a program to run Downloading PCSpim PCSpim is a MIPS R2000/R3000 Simulator, available from To run PCspim on a PC under Microsoft Windows (you don’t need to do this if you are in the lab): download the file, unzip it, and run the setup program Reference documentation with application and Appendix A from the text Running PCSpim Once installed Start - Programs - PCSPIM The first time you run PCSPIM you may have the warning box shown below appear (new versions will say “exception file” instead of “trap file). You’ll then see a screen like this: At the bottom, under “Trap file:” or “Exception file: ”, change the drive from “C” to “D”. Leave “Allow pseudocode instructions” option checked. You can return to settings at any time from the PCSpim window by selecting Simulator-Settings Four windows appear in the PCSpim frame: Registers, Text Segment, Data Segment, and Messages. Register window: the values of all registers in the MIPS CPU and FPU.
Text Segment window: instructions both from your program and the system code that is loaded automatically when PCSpim is running. Data Segment: the data loaded into your program’s memory and the data of the program’s stack.
Messages window: for PCSpim messages. This is where error messages appear. The console also opens. Your input and output to the program will be through the console. Opening, Loading and Running a File Download the file (right-click and use “Save Target As” to save it somewhere).
To open the first file: File - Open Open “simple- spim.asm” (from wherever you saved it above). In the Message window, you should see a message “simple- spim.asm has been successfully loaded” First, try running the program: Simulator - Run - OR- (or you can use the button) You will be asked for the starting address of the program. Use the address of the first instruction in the Text Segment window. ( this is most likely set already) You will be prompted for input on the console window.
Your output will be shown on the console window. Next, try single-stepping through the program one step at a time. Simulation - Reload (don’t use “Reinitialize”). Then, F -10 to step through one statement at a time You’ll notice that SPIM first executes a few instructions at address 0x00400000 that don’t come from the input file – these set up the environment for the program. After a few instructions, SPIM will execute “ jal main” – this is where your program starts. Step through the program, trying to understand how it works. Watch how the registers change.
Note that the console will pop up whenever you print something, but you won’t be able to enter any input until the program executes the syscall for “read integer.” Stopping Program Execution To stop the execution at any time, you can use Simulation - Break You will be prompted whether to continue or end execution. (Click on Yes to end) When you make changes to the file you are running Simulation - Reload to have the updated file loaded into PCSpim Quitting PCSpim File - Exit Videos of the loading of a program are available from.
XSPIM / PCSPIM Introduction & Examples XSPIM / PCSPIM Introduction SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors. SPIM’s name is just MIPS spelled backwards. The simulator is available in both X-Windows (XSPIM) and PC Windows (PCSPIM) versions along with it's source code. SPIM may be downloaded free of charge from.
A complete description of the simulator, the MIPS assembly language and general information about assemblers is contained in Appendix A of 'Computer Organization & Design: The Hardware / Software Interface',Second Edition, by David Patterson and John Hennessey. The X-Windows version of the simulator, XSPIM, is installed on the KSU Computer Science Departmental system. The simulator may be accessed from your student account:. Start up X-Windows. Log onto neptune. Open a X-terminal window. Run /local/opt/spim/bin/xspim& Alias the command, alias xspim /local/opt/spim/bin/xspim or add it to your path in order to save typing.
The figure below shows the xspim user interface. Figure taken from Appendix A of 'Computer Organization & Design: The Hardware / Software Interface',Second Edition, by David Patterson and John Hennessey The load button is used to load a text file containing a MIPS assembly language program into the simulator. The program may then be run or stepped through. Complete instructions can be found in Appendix A. Note: For our class assignments we will using XSIPM in various modes. These modes will be explained with each assignment Following is a list of command options which may be set for XSPIM: Note: For the PCSPIM, the Windows version of spim, you will need to select the appropriate options from the Simulator/Settings menu. Use machine in 'bare metal' mode.
In this mode the assembler uses no pseudo-instructions and does not load error-trapping code. XSPIM must be started with options -nopseudo and -notrap. The symbol start must be defined when -notrap option is used ########################################################### # This program should be run in bare metal mode with # no errror trapping. # It demonstrates how to use arithmetic operations: # # ori Rdest, Rsrc1, Imm // or immediate # add Rdest, Rsrc1, Rsrc2 // Rdest = Rsrc1 + Rsrc2 # ###########################################################.text.globl main main: start: ori $t0, $zero, 8 # put 8 in register $t0 ori $t1, $zero, 16 # put 16 in register $t1 add $t2, $t0, $t1 # $t2 = $t0 + $t1 # end of program 2. Input and Output We will be implementing input and output using the syscall instruction provided by SPIM. The services provided are summarized in the table below.
To implement a system call the arguments are placed in the appropriate register(s) listed below, the system call code is placed in register $v0, and the syscall instruction is issued. The result returned is found in one of the registers listed below, depending upon the service performed. # Start with data declarations # This program should be run in default mode.data str1:.asciiz 'Please enter first operand, A ' str2:.asciiz 'Please enter second operand, B ' str3:.asciiz 'A - B = ' newline:.asciiz ' n' #This will cause the screen cursor to move to a newline.align 2.globl main # leave this here for the moment.text # This is an I/O sequence.
Pcspim Download
Sample SPIM Program Sample SPIM Program # Sample spim program # # Written by Pat Troy, 11/8/2002.data prompt:.asciiz 'Enter in an integer: ' str1:.asciiz 'the answer is: ' newline:.asciiz ' n' bye:.asciiz 'Goodbye! N'.globl main.text main: # initialize li $s0, 10 # prompt for input li $v0, 4 la $a0, prompt syscall # read in the value li $v0, 5 syscall move $s0, $v0 loop: # print str1 li $v0, 4 la $a0, str1 syscall # print loop value li $v0, 1 move $a0, $s0 syscall # print newline li $v0, 4 la $a0, newline syscall # decrement loop value and branch if not negative sub $s0, $s0, 1 bgez $s0, loop # print goodbye message li $v0, 4 la $a0, bye syscall.