Anda di halaman 1dari 3

SPIM -- THE MIPS SIMULATOR

SPIM is an application that simulates a MIPS processor. A simulator is needed to run a MIPS program, since MIPS processors are
not readily available. SPIM is MIPS spelled backwards. MIPS is an abbreviation for million instructions per second (a performance
measure).
MIPS assembly language was chosen by the authors of the text book, because one of them originally developed MIPS in the 1980's . A
series of MIPS processors have been in use from the original MIPS R2000 to the MIPS 10000(or higher) which is currently used by
Play-Station.
Three versions of SPIM are available. PCSPIM runs on Windows. XSPIM runs on Unix X-Windows. Plain SPIM runs on UNIX from
the command line.
Obtaining and Installing PCSPIM
Pcspim.exe can be obtained at www.cs.wisc.edu/~larus/spim.html. Download the zip file. When unzipping the file, select all files then
extract. A directory named "Install PCspim" will be created. Open the directory and start setup.exe to install pcspim.
Bare Minimum PCPIM Setup
The following "bare" minimum pcspim setup works under all versions of Windows (this will only work if the Microsoft foundation
classes runtime and microsoft C run time are already installed--which I would assume is most always the case). Unzip the following 5
files to a directory: pcspim.exe, pcspim.cnt, Uninst.isu, pcspim.help, trap.handler. A compressed directory with just these files can be
obtained from http://www.cs.pitt.edu/~sanjeev/cs447/spim.zip.
The first time you run PCSPIM you will need to set the path to the trap.handler file. From the Spim menu, select Simulator->Settings.
Change the Trap File path to read trap.handler (by default it reads another path). This information will be remembered in the registry for
future starts.
Running an Assembly Program on PCSPIM
After starting PCspim, open an existing assembly language source file from the tool bar or menu with file->open. Run the source file
from the tool bar or menu with simulator->go.
Debugging an Assembly Program on PCSPIM
Debugging involves stepping through a program and examining the contents of registers and memory. After opening an assembly
source file, select "break" from the tool bar or menu, and enter the memory address of instruction to break at. The addresses of
instructions can be found in the "Text Segment" from the window menu. Now, run the program. When it stops at the first break point,
select "no". From here, use the F10 key to step through the program. Examine the "data segment" and "registers" from the window
menu, to determine the effect of each instruction.
A source file can not be edited directly from SPIM. The code is edited from an editor (notepad or wordpad). After making changes to a
program, save the source from the editor, then re-open the file in SPIM.
Running XSPIM on UNIX at Pitt.
On a Sun Sparcstation
Type startx to start X-windows. Multiple windows can be opened by middle-clicking the window area and selecting new window. You
will need one window for the editor and another for xspim.
Start xspim in one window by typing xspim. Start pico in the other window by typing pico. The assembly source file can be written and
edited with pico. The source file can not be edited directly from SPIM.
To run a MIPS assembly program, click the xspim load button, and enter the file name, then click the run button. Refer to page A-38 of
the text for additional information on debugging.
Writing a MIPS Program
Use any text editor such as notepad or wordpad (or pico in UNIX) to write and edit a source file. Save the source file as a plain text file.
The file name should be given an ".s" extension.

INTRODUCTION TO MIPS ASSEMBLY LANGUAGE


Parts of the Assembly Language Source Code

The source code is separated into a data segment and a text segment. The text segment of the assembly source code contains the
instructions. The text Segment must be identified to the assembler by preceding it with an assembler directive -- .text. All assembler
directives start with a period.
The data segment of the assembly code identifies locations in memory that are storing data. The data segment must be preceded by
the assembler directive -- .data. Locations in memory can be referenced in the code with labels. A label is a name that corresponds to
an address in memory.
A: .word 100
The above line could appear in the data segment of an assembly source file. A is the label. Labels are followed by a colon. The word
directive indicates that the value 100 will be stored as an integer.
example source code:
############################ identify comments with pound sign
.data
# assembler directive -- start data segment
A:
.word 100
# labeled location with value 100
.text

# assembler directive -- start text segment


# label indicating starting point of execution
## instructions go here
#############################################################
main:

Registers
Arithmetic and logical instructions can not directly operate on values in memory. Values must be loaded into the CPU registers in
order to operate on them. The processor contains 32 integer registers and 32 floating point registers. Conventions and restrictions
specify particular uses for certain registers. Each of the registers has a name that indicates the use of the register. For now, you should
use the integer registers $t0, $t1, $t9. Register $zero will also be useful. It is a read-only register that always has the value 0.
The simplest way to put a value into a register is with the load-immediate instruction, as follows:
li $t0, 123
This instruction loads the integer 123 into register $t0.

Memory
The MIPS memory addresses range from 0 to hex 7fffffff. The machine instructions are stored in the text area of memory which begins
at hex 400000. The text area of memory corresponds to the text segment of the assembly language source code.
The static data is stored in the static data area of memory which begins at hex 10000000. The static data area of memory corresponds
to the data segment of the assembly language source code.
Storing Integers to Memory
sw $t0, 0($t1)
Above is a store word instruction. It stores the contents of register $t0 to the memory location with the byte address corresponding to
the value of register $t1 plus the offset, which is zero in this case. The byte address should be a multiple of four, since each word is
four bytes.
Loading Integers to Registers from Memory
lw $t0, 0($t1)
Above is a load word instruction. It loads register $t0 with the word at the memory location with the byte address corresponding to the
value of register $t1 plus the offset..
R-format Instructions
R-format instructions are arithmetic or logical instructions (and some others) which have integer registers as operands.
add $t0, $t1, $t2
This instruction adds the contents of registers $t1, and $t2 and puts the results in register $t0. Other R-type instructions include, sub,
and, or, sll, srl, slt, jr (refer to back cover of text book).

Simple MIPS Programs


Try stepping through these programs while examining registers and memory
#######ADDITION WITH OPERANDS AND RESULT IN REGISTERS
.data
## no data

.text
main:
li $t1, 3
li $t2, 5
add $t0, $t1, $t2 ## $t0 = 3 + 5
li $v0, 10
syscall

##end all programs with these two lines

#######ADDITION WITH OPERANDS AND RESULT IN MEMORY


.data
A:
.word 3
B:
.word 5
C:
.word 0
.text
main:
la $t1, A
lw $t1, 0($t1)

## load address for label A into register $t1


## load word at memory address labeled A

lw $t2, B($zero)

## another way to load a value from memory

add $t0, $t1, $t2 ## $t0 = A + B


sw $t0, C($zero) ## store result to memory location labeled C
li $v0, 10 ## 10 is the system call code to exit
syscall
## syscall looks in register $v0 for a code
## see page A -49 for other uses of syscall