Computer Science 270
Spim comes in two versions: (plain ol') spim and (graphical) xspim. We will use the xspim version in order to take advantage of the graphical interface. The executable resides in the directory /usr/local/spim, and since we will be using this software quite a bit during the semester it is worthwhile adding that location to your local PATH variable.
(1) Edit .bash_profile or .bashrc to include lines similar to the following:
PATH=$PATH:$HOME/bin:/usr/java/jdk1.3.1_01/bin:/usr/local/spim:. export USERNAME BASH_ENV PATH JAVA_PATH
Force your bash shell to reread the recently modified .bash_profile with the command
Note that the executables, but not the rest of the spim files, are also found in /usr/unsup/bin.
The manual for spim is available as a Postscript file in the directory /usr/local/spim/Documentation. Use Ghostview to open and read it with the command
gv /usr/local/spim/Documentation/spim.ps &
Note that the "scroll bar" on Ghostview is the little rectangular area in the lefthand panel just above the "<< >>" buttons. Slide it up and down with the mouse to scroll the main window. The keyboard's up and down arrows also scroll the window, well, up and down. Page Up and Page Down move to the previous and next page, respectively.
(2) Create a directory named spim in your home directory and copy the file /usr/local/spim/trap.handler into it:
mkdir spim cd spim cp /usr/local/spim/trap.handler .
The "." means "here" or "this directory." Spim refuses to launch if it cannot readily find such a trap.handler.
(3) Test your setup so far by launching spim:
You should see a large white screen filled with several panels, arrays of registers, and mips assembly code.
(4) We need a simple program to run. Let's write the traditional "Hello, World!" program in mips assembly language. Open emacs on the file "hello.s" We will use the dot s convention for our mips assembly source code. Enter the following lines and save your file:
# hello.s # "Hello, World!" program # email@example.com 6 Oct 2001 .globl main main: .data str: .asciiz "Hello, World!" .text li $v0, 4 # system call code for print_str la $a0, str # address of string to print syscall # print the string
Lines beginning with a # are comments. Every program needs a place to start, hence the declaration of the global label main. The next section binds the label str to our "Hello, World!" string. The last section invokes the print_string system call by loading its number 4 into the $v0 register, and its argument str into the $a0 register, and then invoking the system call.
(5) Now we are ready to run our hello program on the spim simulator. Click the load button on xspim and enter the name of the hello program into the dialog box, hello.s, and click the assembly file button. If there was an error in your program you will see an appropriate remark in the lowest spim panel. If not, click the run button. Another dialog box appears asking you to confirm the starting address of the program to run. Spim's suggestion is usually correct, so click OK. You should see a console window open with the words "Hello, World!" displayed in it. Congratulations!
(6) Let's write another program. This one comes from a spim manual written by the author of spim, James Larus. We'll tell everyone that the answer is 3. Use emacs to create a text file with the following code:
# answer program # firstname.lastname@example.org 6 Oct 2001 .globl main main: .data str: .asciiz "The answer is " .text li $v0, 4 # system call code for print_str la $a0, str # address of string to print syscall # print the string li $v0, 1 # system call code for print_int la $a0, 3 # integer to print syscall # print the integer
Start xspim, load the answer program and run it. You should see "The answer is 3" displayed in a console window. Terrific! We're programming in mips.
(7) You may have expected the system to run the new "answer is 3" program, but found that it ran both the previous "Hello World!" program AND the new one. Explore the panels of spim for a moment to discover where your programs were loaded. Hint: It loaded your source code comments as well. Now press the Clear button and hold down your mouse button. A menu will appear offering two options. Choose the one that clears away your source code. Then load the "answer" program again, run it, and verify that "Hello, World!" no longer appears. This is the way to interact with the interpreter to run several programs. The following exercises explore how to handle interactive input and putput.
Exercise: hello_CR.s. Here is a quick exercise to begin working with the system. Go to page 7 of the spim manual to learn how to output a newline to the spim console. Then modify your "Hello, World!" program to issue a newline after its greeting. Run the modified program. Name it hello_CR.s.
Another Exercise: read_int.s. Now write a small program, named read_int.s, that prompts the user to enter an integer, accepts the input from the spim console, and echos it right back again. Hints: See page 9 of the manual to discover that there is a system call named read_int, and then look at Table 1 at the top of page 8 of the manual to discover the code for that system call, what arguments it takes (none, in this case), and where it puts its result. An easy way to move data from one register to another is to use the addi instruction with the number 0 as one of its arguments (manual page 13).