CDA 3101

Spring 2002


XSPIM Tutorial


 

1. Loading and Running a MIPS program


A. In your favorite editor , enter the following program. save it as sum.s
The first time that you look at the MIPS code , it looks too complex and difficult to understand, but if you read the explanation about the code you will realize that the code follows the convention of any other well structured Language. After you save the program, go to the following sections to learn the basic instructions of the XSPIM simulator.
An explanation of the code. IMPORTANT!

#------------------------------------------------------------
# sum.s
# Asks for a number i. Then it calculates the sum of all numbers
# from 1 to i
# Register Usage:
# $t0 sum (the sum)
# $t1 i (loop index)
# $t2 testing if i>=1
# $t3 1
# Algorithm in c:
#
# int i,sum;
# scanf ("Please enter a number>=1",&i);
# if (i>=1)
# {
# sum = 0;
# while (i>=1)
# {
# sum = sum + i;
# i = i - 1;
# }
# printf ("The result is: %d", sum);
# }
#-----------------------------------------------------------

            .data
str1:     .asciiz " Please enter a number >= 1 : "
str2:     .asciiz "\n\n The result is:"
            .text
            .globl main

main:
            li $v0, 4                             # Here, we load the value for our macro print_str
            la $a0, str1                        # We pass the argument with the string
            syscall                               # Print the string

            li $v0, 5                             # value for macro read_int
            syscall                               # Get integer
            move $t1, $v0                   # $t1 = $v0

            addi $t3, $zero, 1             # $t3=1
            slti $t2, $t1, 1                   # if $t1<1 then $t2=1 else $t2=0
            beq $t3, $t2, do_nothing  # if $t2==1 do nothing


            addi $t0, $t0, 0               # sum = 0
loop:
            sge $t2, $t1, $t3             # if $t1>=1 then $t2=1 else $t2=0
            beq $t2, $zero, end        # if $t2!=1 exit

            add $t0, $t0 , $t1           # sum = sum + i
            addi $t1, $t1 , -1            # i = i - 1
            j loop

end:
            li $v0, 4                        # Here, we load the value for our macro print_str
            la $a0, str2                   # We pass the argument with the string
            syscall                          # Print the string

            li $v0, 1                        # value for the macro print_int
            move $a0, $t0              # move $t0 to the argument
            syscall

do_nothing:

           li $v0, 10                         #system call code for exit
           syscall                             #executes exit




B. Example of XSPIM window




Error





C. Load the XSPIM.

In the UNIX shell type the command

xspim &

After that, you will see to appear the previous window.Then in XSPIM, click the "load" button. In the popup dialog box, enter the filename sum.s and click the "assembly file" button. If everything in your program is correct the program should be loaded without problems.

D. Run the program.
Click the "run" button. In the popup dialog box, click "ok". The console window will open and the output of the program will be displayed. You can hide/redisplay the console window with the "terminal" button.
Normally, the code is loaded at the address
0x00400000 , but some times we want to start in another address direction the popup dialog box allows us to change the value for the start position.


2. Changing parameters in XSPIM

A. Changing Registers or Addresses.
Some times, we want to be able to set up the the values the registers individually. Then in the  XSPIM window, click in the button set value and in the popup we have two dialog boxes. So, if we want to change some of the values for a register, we need to input the selected register in the dialog windows " Address or Register Name". After that, we can set the value for that address or register in the dialog box for values.
Normally, we introduce that value in hexadecimal notation because of its compact representation and its canonical mapping with the binary numbers.

B. Clearing Values.
We have a button in XSPIM that allows to clear all the values in the register.



3. Some Tips for Running Programs

A. Setting Breakpoints.
It is possible to add breakpoint to our program. So, we can run our programs in segments that allow us to see the change in the registers or in the memory data.
We can set this break point using the button called "
breakpt". After  we click in the button, we can set the break point in the position that we need. For that, we need to set the address for this break point. For example:

                        1)     Observe that the instructions jumps from four bytes to four                                             bytes.
0x00400000 lw $4, 0($29)
                                             <= We want to place the                                                        break point here
0x00400004 addiu $6, $5, 4
2)        Now, the final version is

            0x00400000 lw $4, 0($29)
            0x00400004 break $1           <=Notice the change in
            0x00400008 addiu $6, $5, 4
       address for the next                                                                             instruction
B. Step by step running.
We can run the program instruction by instruction using the " step" button. We can go one instruction each time or multiple instructions each time. For that, we only need to change the size of each step in the popup dialog box.