CS201 Lab: Introduction to PCSpim

Important Information


1.      In CL105, we have Windows 10, and installed PCSpim version 9.1.9.

2.      If you wish to install PCSpim 9.1.9 or the other versions 
	in your home computer, you can refer to the following website:
        http://sourceforge.net/projects/spimsimulator/files/

3.      To open the PCSpim, you can double click on the icon on your desk top,
        or open 	C:\Program Files\PCSpim\pcspim.exe
       
4.      Once you get it opened, click on the "Simulator"  and
        then select "Settings" .  When you see the table,
        make sure that "Load exception file" is NOT checked.
	You only need to do this once, not every time.

        Then, close the PCSpim,  and reopen it.
        All the examples in the lab notes will work fine for you.

5.	SPIM is a simulator for the MIPS architecture.
	In the lab, we are going to use PCSpim version 9.1.9.
	For more information about SPIM, please follow the following link:
        http://www.cs.wisc.edu/~larus/spim.html

MIPS Architecture

SPIM is a simulator for the MIPS. MIPS processor contains 32 general purpose registers that are numbered from 0 to 31.
The registers are used in the SPIM instructions, particularly, in arithmetic operations.

Here is a summary of the MIPS register file for your reference.


        Register Name   Number  Usage
        ===============================================================
        Zero            $0      Constant 0
        ---------------------------------------------------------------
        $at             $1      Reserved for assembler
        ---------------------------------------------------------------
        $v0             $2      Used for the expression evaluation
        $v1             $3      and results of a function
        ---------------------------------------------------------------
        $a0             $4      Used to pass arguments to functions
        $a1             $5
        $a2             $6
        $a3             $7
        ---------------------------------------------------------------
        $t0 - $t7       $8-$15  Temporary (not preserved across call)
        ---------------------------------------------------------------
        $s0 - $s7       $16-$23 Saved temporary (preserved across call)
        ---------------------------------------------------------------
        $t8 - $t9       $24-$25 Temporary (not preserved across call)
        ---------------------------------------------------------------
        $k0 - $k1       $26-$27 Reserved for OS kernel
        ---------------------------------------------------------------
        $gp             $28     Pointer to global area
        ---------------------------------------------------------------
        $sp             $29     Stack Pointer
        ---------------------------------------------------------------
        $fp             $30     Frame Pointer
        ---------------------------------------------------------------
        $ra             $31     Return address (used by function call)
        ---------------------------------------------------------------


        NOTE:   The MIPS processor also has 16 floating point registers
                $f0, $f1, ..., $f15 to hold numbers in floating point
                form, such as 3.1415.

Create an Assembly Language Program

To create an assembly language program, you need to use a text editor such as NotePad in Microsoft Windows environment. The file name must have a .s at the end. Let's assume that you have created the following program called hello.s by using NotePad on a PC. The file hello.s contains the source code of the program to print out a character string "hello world". We will use PCSpim to open and execute this program so that you can see the program output.


##
## 	hello.s - prints out "hello world"
##
##      $a0 - points to the string
##

#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################

        .text
        .globl __start
__start:                # execution starts here

        la $a0,str      # put string address into a0
        li $v0,4        # system call to print
        syscall         # out a string

        li $v0,10	# Exit
        syscall         # Bye!


#################################################
#                                               #
#               data segment                    #
#                                               #
#################################################

        .data
str:    .asciiz "hello world\n"

##
## end of file hello.s


Now let's analyze the program and see how it works.

Analyze the Program

The MIPS assembly language program using PCSpim is usually held in a file with .s at the end of the file name.
For example hello.s. This file is processed line by line by the SPIM program. In this file,


        A sharp sign # is used to begin inline comments.
        i.e. Everything from the # to the end of the line is
             ignored by the assembler.

        An identifier is a sequence of alphanumeric characters,
        underbars/underscores(_),and dots(.) that does not begin with a number.

        A label is declared by putting identifiers at the beginning of a line
        followed by a colon.  For example:  str: 

        An operation field contains either a machine instruction or
        an assembler directive.

        One or more operands are required by some machine instructions.
        Assembler directives may also require operands.

        A constant is a value that does not change during program assembly
        or execution. A string contant is delimited by double quotes(").
        For example:  "Hello World\n"

The program hello.s prints out the message hello world.
It sets up the string in the data segment and makes a system call to print out this string in the text segment,
followed by a system call to exit the program.


        .text

                -  a directive which tells the assembler to place
                        what follows in the text segment.

        .globl __start
__start:

                -  These two lines attach the label __start
                        to the first instruction so that the assembler
                        can identify where the execution should begin.
                        All the exercises in the lab will have these two
                        lines unchanged.

	.data

		- a directive which tells the assembler what is in the memory.

Start up PCSpim

You can start up PCSpim by clicking on the icon from the "Start" menu or "All Programs" on a lab PC.
The following screen is what you will see.

 

Open and Run the Program

To open the program, click on the "File" menu from PCSpim screen and find the program that you want to open.
 

Then, you would see the following screen:
 

Now select "Go" from the "Simulator" menu:
 

To run the program, click on "OK".
 

Here is the program output on the "Console".
 

I/O Manipulation - MIPS System Calls (syscall)

SPIM provides a set of operating-system-like services through the system call (syscall) instructions. Basic input and output can be managed and implemented by system calls.

To request a service, a program loads the system call code into register $v0 and arguments into registers $a0, $a1, $a2, and $a3.

Here is a summary of the system calls for your reference.


                        Call Code
   Service              in $v0          Arguments       	Results
   ===========================================================================
   Print Integer        1               $a0=number
                                      (to be printed)

                                        Example Code:
                                        li $a0, 89
                                        li $v0, 1
                                        syscall
   ---------------------------------------------------------------------------
   Print Float          2               $f12=number
                                      (to be printed)

                                        Example Code:
                                        l.s $f12, flabel
                                        li $v0, 2
                                        syscall

                                        .data
                                flabel: .float 3.14
   ---------------------------------------------------------------------------
   Print double         3               $f12=number
                                      (to be printed)

                                        Example Code:
                                        l.d $f12, dlabel
                                        li $v0, 3
                                        syscall

                                        .data
                                dlabel: .double 3.1415926
   ---------------------------------------------------------------------------
   Print String		4		$a0=address
				      (of string in memory)
					
					Example Code:
                                        la $a0, str1
                                        li $v0, 4
                                        syscall

                                        .data
				str1:	.asciiz "Hi There!"
   ---------------------------------------------------------------------------
   Read Integer		5					number in $v0
					Example Code:
					li $v0, 5
        				syscall
					move $t0, $v0  # save the entered number
   ---------------------------------------------------------------------------
   Read Float		6					number in $f0
					Example Code:
					li $v0, 6
        				syscall
   ---------------------------------------------------------------------------
   Read double		7					number in $f0
					Example Code:
					li $v0, 7
        				syscall
   ---------------------------------------------------------------------------
   Read String		8		$a0=address
				 (of input string in memory)
				$a1=length of buffer(n bytes)

					Example Code:
                                        la $a0, str1
					li $a1, 80
                                        li $v0, 8
                                        syscall

                                        .data
				str1:	.space 80
   ---------------------------------------------------------------------------
   Sbrk			9		$a0=n-byte		address in $v0
   (Dynamically				(to allocate)
    allocate n-byte
    of memory)				Example Code:
					li $a0, 80
        				li $v0, 9
        				syscall	# Get memory

					move $a0, $v0
        				li $a1, 80
        				li $v0, 8
        				syscall	# Read String
        				li $v0, 4
        				syscall	# Print String	
   ---------------------------------------------------------------------------
   Exit			10		Example Code:
					li $v0, 10 
        				syscall
   ---------------------------------------------------------------------------

A Few Special Characters


	Character	Escape
	=======================
	Newline		\n
	Tab		\t
	Double Quote	\"

MIPS Reference Card


Lab Assignment


Copyright: Department of Computer Science, University of Regina.