CS201 Lab: Introduction to PCSpim


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

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.

 

Create an Assmbly Lauguage Program

Let's assume that you have created a program called hello.s by using NotePad on a PC.


##
## 	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


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".
 

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 do not begin with a number.

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

	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.

SPIM System Call (syscall)

SPIM provides a set of operating-system-like services through the system call (syscall) instruction.
To request a service, a program loads the system call code into register $v0 and arguments into registers $a0, $a1, $a2, or $a3.
Here is a summary of the system calls for your reference.

Function
Code
($v0)

Function

Other Important Registers

Sample Code

1

Print an Integer

INPUT
$a0 : the integer to be printed
        li $a0, 12345
        li $v0, 1
        syscall

2

Print a Float

INPUT
$f12 : the Floating point number to be printed.
        .data
fvar:   .float 1.125
        .text        
        l.s $f12, fvar
        li $v0, 2
        syscall

3

Print a Double

INPUT
$f12 : the Double to be printed.
        .data
fvar:   .double 1.2345678901234567
        .text        
        l.d $f12, fvar
        li $v0, 3
        syscall

4

Print a String

INPUT
$f12 : the address of the string to be printed.
        .data
str1:   .asciiz "I'm a string!"
        .text
        la $a0, str1
        li $v0, 4
        syscall

5

Read an Integer

OUTPUT
$v0 : the integer that was read
        li $v0, 5
        syscall

6

Read a Float

OUTPUT
$f0 : the Floating point number that was read
        li $v0, 6
        syscall

7

Read a double

OUTPUT
$f0 : the double that was read
        li $v0, 7
        syscall

8

Read a String

INPUT
$a0 : Address of input buffer in memory
$a1 : size of buffer (in bytes)

OUTPUT
Input buffer has user's input loaded into it.
        .data
str1:   .space 255
        .text        
        la $a0, str1
        li $a1, 255
        li $v0, 8
        syscall
        li $v0, 4
	syscall

9

Dynamically allocate n bytes of memory.

INPUT
$a0 : n The number of bytes to allocate.
OUTPUT
$v0 : A pointer to the bytes allocated.
        
	li $a0, 255
        li $v0, 9
        syscall 	# Get memory

	move $a0, $v0
        li $a1, 255
        li $v0, 8
        syscall		# Read String
        li $v0, 4
        syscall		# Print string
	

10

Exit.

 
        li $v0, 10 
        syscall

A Few Special Characters

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


Copyright: Department of Computer Science, University of Regina.