Lab 01

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab 1 : An Introduction to SPIM, the MIPS

architecture simulator

Name:
Kevin Bradshaw

Sign the following statement:


On my honor, as an Aggie, I have neither given nor received unauthorized aid
on this academic work
Kevin Bradshaw

Objective

The objective of this lab is to give you hands on experience with QtSpim; a MIPS simulator.

Pre-requisite

Before starting with this lab, you are required to read up on QtSpim. Information on
QtSpim can be found on the course webpage or in the CD-ROM that comes with your
textbook.

Computer Architecture and Design, Lab 1

Setup

For those of you unfamiliar with Linux (or Unix) and the command line interface (CLI),
you should review the basics on the following website:
http://www.ee.surrey.ac.uk/Teaching/Unix/index.html
Before you begin, you must setup the correct path environment variable to run QtSpim.
Perform the following steps to setup a terminal with the correct environment:
1. Open up a Terminal window (Right click on desktop and select Open Terminal).
2. Determine what type of shell you are running (see the tutorial for details of what
this means). Type the following:
echo $SHELL
(a) If the answer contains bash, then type the following to setup the path correctly:
source /softwares/courses/350/setup.350.bash
(b) If the answer contains tcsh, then type the following to setup VCS:
source /softwares/courses/350/setup.350.tcsh
Note: You may add this to your shell by modifying your login scripts, ask your
TA for details.
(c) Start QtSpim by typing QtSpim on the command line of the terminal.
You should have a working QtSpim simulator window pop open at this point.
If the window does not open properly but instead the words Segmentation fault
appear, follow these steps to fix the error:
(a) Start the qtconfig tool by typing:
qtconfig
(b) Under Appearance/Gui Style/Select GUI Style select the option Motif.
(c) Close the qtconfig window. It should ask you if you want to save the changes,
select yes.
This should fix the Segmentation fault error permanently for your users environment.

Computer Architecture and Design, Lab 1

Loading and executing a simple MIPS assembly program

Consider the program described below.


1
2
3
4
5
6
7

. text
. globl main
main :
addi $t1 , $0 , 10
addi $t2 , $0 , 11
add $t3 , $t1 , $ t 2
jr $ra

# text section
# c a l l main by SPIM
# l o a d immediate v a l u e ( 1 0 ) i n t o $ t 1
# l o a d immediate v a l u e ( 1 1 ) i n t o $ t 2
# add two numbers i n t o $ t 3
# r e t u r n from main ; r e t u r n a d d r e s s s t o r e d i n $ra

The MIPS assembly language in SPIM has a few simple rules:


Text that follows after a sharp sign (#) is a comment in SPIM.
A line that starts with a period (.) is an assembler directive. Assembler directives are
a way of instructing the assembler how to translate a program but do not produce
machine instructions. For example at line 6 in the code below, .globl main defines
main as a global variable.
Text followed by a colon (:) defines a label. It is similar to defining variables in any
high-level language, for example int var in C-language.
Type the program in using your favorite text editor and save it as Lab1a.s. Make sure
you do not enter the line numbers.
If unsure what text editor to use, emacs and vim are the most popular and powerful.
gedit is usable and similar to notepad. You may open the editor from the command line
by typing:
gedit Lab1a.s
Note: SPIM accepts files with either .asm or .s extensions. We will be using .s notation. You may refer to page B-47 in the Appendix of the Text Book for more information
on Assembler Syntax and Directives.
1. Load the program into QtSpim(refer to the QtSpim help files on the website if unsure
how to do this);
2. Execute the program step by step;
3. Examine the content of registers $t1,$t2, and $t3;
4. Copy the hexadecimal representation of the instruction on line 6 ( add $t3, $t1, $t2);
012a5820

5. Modify the program such that in the end the register $t3 is equal to 4*$t1+2*$t2
(use multiple add instructions);

Computer Architecture and Design, Lab 1

6. Run and validate the modified program step by step and examine the content of the
registers $t1,$t2, and $t3. Demonstrate your progress to the TA; .

Loading and executing another sample MIPS assembly program

Consider the program described below. The following program takes two values, X and Z,
and computes 2 values.
. text
. globl main
main :
# r e p l a c e X w i t h any number you l i k e
addi $t0 , $0 , X
# make s u r e you r e p l a c e Z w i t h t h e f i r s t d i g i t o f your UIN
s r l $t1 , $t0 , Z
#c o m p u t a t i o n 1 , r e s u l t i s i n $ t 1
s l l $t2 , $t0 , Z
#c o m p u t a t i o n 2 , r e s u l t i s i n $ t 2
# c h e c k t h e c o n t e n t o f $ t 1 and $ t 2
jr $ra
# r e t u r n from main ; r e t u r n a d d r e s s s t o r e d i n $ra

1
2
3
4
5
6
7
8
9
10

1. Type the program above into a file called Lab1b.s.


2. Enter an integer of your choice in X and Z.
Value of X= 5
Value of Z= 1
Content of $t1= 2
Content of $t2= a

;
;
;
.

3. Can you figure out what computation the program is doing? Write the mathematical
representation of the computations in terms of X and Z.
$t1 := X
$t2 := Z

Loading and executing a sample MIPS assembly program with


Input/Output operations

We would like to execute this program multiple times with different values for X. Execution
will be much more convenient if the user is prompted for the value of X and the program
prints out the result. To achieve this, we will use system calls to interact with the user.
System calls are a form of function call that runs routines defined by the Operating System.
In SPIM, the register $v0 defines which function will be executed, and $a0 is its argument.

Computer Architecture and Design, Lab 1

For example, function 4 will print text to the console, using $a0 as the address of the string
to print. You can refer to page B-43 in the appendix of the Text Book for a list of System
Calls that SPIM supports.
Enter the program below into a file called Lab1c.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

. data
msg1 :
. a s c i i z P l e a s e e n t e r an i n t e g e r number :
msg2 :
. a s c i i z \ t F i r s t r e s u l t :
msg3 :
. a s c i i z \ tSecond r e s u l t :
. text
. globl main
# I n s i d e main t h e r e a r e some c a l l s ( s y s c a l l ) which w i l l change t h e
# v a l u e i n r e g i s t e r $ra which i n i t i a l l y c o n t a i n s t h e r e t u r n
# a d d r e s s from main . This n e e d s t o be s a v e d .
main :
addu $s0 , $ra , $0
# s a v e $31 i n $16
l i $v0 , 4
# system c a l l f o r p r i n t s t r
l a $a0 , msg1
# address of s t r i n g to print
syscall
# now g e t an i n t e g e r from t h e u s e r
l i $v0 , 5
# system c a l l f o r r e a d i n t
syscall
# t h e i n t e g e r p l a c e d i n $v0

18
19
20
21
22
23

# do some c o m p u t a t i o n h e r e w i t h t h e i n t e g e r
addu $t0 , $v0 , $0
# move t h e number i n $v0 t o $ t 0
# make s u r e you r e p l a c e Z w i t h t h e f i r s t d i g i t o f your UIN
s l l $t1 , $t0 , Z
#c o m p u t a t i o n 1 , r e s u l t i s i n $ t 1
s r l $t2 , $t0 , Z
#c o m p u t a t i o n 2 , r e s u l t i s i n $ t 2

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# print the f i r s t r e s u l t
l i $v0 , 4
# system c a l l f o r p r i n t s t r
l a $a0 , msg2
# address of s t r i n g to print
syscall
l i $v0 , 1
# system c a l l f o r p r i n t i n t
addu $a0 , $t1 , $0
# move number t o p r i n t i n $a0
syscall
# p r i n t the second r e s u l t
l i $v0 , 4
# system c a l l f o r p r i n t s t r
l a $a0 , msg3
# address of s t r i n g to print
syscall
l i $v0 , 1
# system c a l l f o r p r i n t i n t
addu $a0 , $t2 , $0
# move number t o p r i n t i n $a0
syscall
# r e s t o r e now t h e r e t u r n a d d r e s s i n $ra and r e t u r n from main
addu $ra , $0 , $ s 0
# r e t u r n a d d r e s s b a c k i n $31
jr $ra
# r e t u r n from main

Make sure you put the first digit of your UIN, instead of Z in lines 22 and 23

Computer Architecture and Design, Lab 1

1. Enter an integer of your choice and the program should print two results for you.
Keep repeating the process for 5 different integers, and fill the table below:
Note: You can re-run the program without reloading it by selecting Simulator/Clear
Registers prior to re-running. Clear Registers resets all the register values to their
defaults (including PC).
Value of Z = 1
Integer

Result 1

Result 2

10

14

16

32

45

90

22

2. Next, you learn how to use the simulator to debug your program. First, select
Simulator/Display Symbols and fill out the following table. This is the list of
where parts of your program are located in memory.
Symbol

Address

g __eoth

0x00400024

g __start

0x00400000

g main

0x00400024

3. Step through your program using the Step menu button (step one instruction at
a time). With each step, you will see lines in the Text window become highlighted.
The highlighted data contains the Address, Machine Instruction, and the line of code
currently executing. Step through the first 5 lines of your code and fill in the following
table:
Address (PC)

Native Instruction

Source code

[00400024]

addu $16, $31, $0

addu $s0, $ra, $0

[00400028]

ori $2, $0, 4

li $v0, 4

[0040002c]

lui $4, 4097 [msg1]

la $a0, msg1

[00400030]

syscall

syscall

[00400034]

ori $2, $0, 5

li $v0, 5

Note: You will have to step past some preamble code after reinitializing the
registers to get to the beginning of your code.
4. Re-initialze your program, and set a breakpoint at line 26 (li $vo, 4 ). Find the appropriate line with this instruction in the Text window and note its memory address.
Right click on this line and select Set Breakpoint. Run your program. When it hits

Computer Architecture and Design, Lab 1

the breakpoint, examine registers $8, $9, and $10. Try entering different numbers
into the program to fill in the following table:
Number you entered

$8

$9

$10

12

12

12

24

16

16

32

23

23

46

11

5. Execute and validate the assembly program created in prelab that swaps the contents
of two variables stored in registers $4 and $5.
Note: QtSpim allows the user to save the status of the registers, the memory and
the instructions in file. Either select Save Log File from the File menu or simply click
the save icon (which looks like a floppy drive). In both cases a dialog will prompt
you for a file name in which to store the log.

Deliverables
Submit completed copy of this lab manual.
Include the following in a compressed file (.zip format) to your TA:
The source code for all .s files.
All log files.

You might also like