You are on page 1of 8

Experiment 3: Introduction to QTSPIM, MIPS Assembly

Programming
Aim of this experiment is to get familiarized with Qtspim environment by executing simple programs
Spim is a simulator that runs MIPS32 assembly language programs. Qtspim is a user interface for spim.
Qtspim also provides a minimal set of OS-like services and simple debugger.
Below is a screenshot of a window showing QTSPIM. The screenshot explains the available
commands and also shows the various tabs available in the application, like Int Registers, Data, Text

Figure 1 Screenshot of qtspim

In the registers section there are two tabs, one for Integer registers and other for floating point
registers. Similarly in memory section there are two tabs Data and Text. The Text tab of the memory
section shows the code currently present in the code memory, which includes simulator generated code
and user code under user text segment and a kernel text segment. The Data tab shows the content of
data memory.

Page 1

Programming with Qtspim: An Example of adding two numbers in


memory and storing the result back in memory
1. The assembly level program should be written using a text editor application like notepad, wordpad or
notepad++. The written file should be saved as .asm file
Below is the assembly code for this experiment

Here .data indicates the data segment. In data segment a location value is created and three words are
stored. .text indicates the beginning of code segment where the main program is written. The program
written here loads two words that are stored at location Value (i.e. Value[0] and Value[1]) and stores the
result in third word location (i.e. Value[2]).
2. The file saved as .asm should be loaded in qtspim by clicking on load button and selecting the file to
be executed.

Page 2

3. The loaded assembly code will appear in the user code section of the text tab
View Before Loading

This code is like a boot loader program which executes before executing the user program.
View After Loading (loaded code is highlighted)

Page 3

Once the program is loaded the data segment also gets loaded with the data stored (in this example
0x10, 0x20 and 0). You can see the data loaded under data tab and the value of all registers under Int
Regs tab. In QTSPIM simulator the data segment always starts with address 10000000h.

4. To run the code press on the run/continue button

5. The changes in the memory content or the register content can be viewed in Data tab or registers tab
respectively.

Page 4

6. In many programs it might be necessary to check the execution step by step. For step by step execution
single step button shown in image below can be used. While running the single step execution you
can constantly view that status of data memory and registers after every step for better understanding of
working of any assembly program. Since most of the programs that you will be working on will be
small use this option more frequently than Run option.

7. If you want to load a new program after execution of another program, then you should reinitialize the
simulator. This can be done by clicking on the Reinitialize Simulator button as shown in the figure.

A few useful assembler directives


.text - indicates that following items are stored in the user text segment, typically instructions
.data - indicates that following data items are stored in the data segment
.globl sym declare that symbol sym is global and can be referenced from other files
.word w1,.,wn store n 32-bit quantities in successive memory words
.half h1,,hn store n 16-bit quantities in successive memory words
.byte b1,,bn store n 8-bit quantities in successive memory bytes
.ascii str store the string in memory but do not null terminate it. The strings are represented in double
quotes str
.asciiz str store the string in memory and null-terminate it
Useful pseudo-instructions These instructions will be explained in the
lab.
la $s0, addr: Load address into register s0
lw $t0, address: Load a word at address into register t0
li $t0, value: Load immediate value in to the register
move $t0, $t1: move contents of register $t1 to $t0
Page 5

Exercise 3.1: Write MIPS assembly program to load two numbers from data segment, add them
and store the result back to the data segment. (code on page 2)
Q3.1.
Copy your assembly code for above exercise here. (In space below if you want to go to next
line use Shift-Enter)
Answer:
Q3.2.
Copy the image of data segment before execution and after execution. Copy the same in
your observation book.
Example: Before execution

After Execution

Answer:

Exercise 3.2: Store N words in data segment (using .data) and write MIPS assembly code to add a
constant value 5 to all the N words. The value of N is also stored in data segment. (Choose N
value to be greater than or equal to 10d)
For example:

.data
N: .word 0x0a
Value: .word 0x32, 0x20, 0x12, 0x45, 0x56, 0x21, 0x67, 0x10, 0x67, 0x90
(Hint for programming: You can use pseudo instructions lw $t0, N; la $s0, Value)
Q3.3.

Copy your assembly code for above exercise here.

Answer:

Page 6

Q3.4.
Copy the image of data segment before execution and after execution for this program.
Copy the inputs and outputs of this program in your observation book.
Answer:

Exercise 3.3: Store N words in data segment (using .data) and write MIPS assembly code to
arrange them in ascending order.
Q3.5.
Copy your assembly code for above exercise here. Show the output of this program to the
Lab instructor.
Answer:

Q3.6.
Copy the image of data segment before execution and after execution for this program.
Copy the inputs and outputs of this program in your observation book.
Answer:

Q3.7.
Write the equivalent MIPS instructions that the QTSPIM gives for the Pseudo
instructions mentioned earlier (li, la, lw, mov).
Answer:

li $t0, 0x80008000h
la $t1, Value

(Where Value is a location in data segment where 0x12345678h is stored)

lw $t2, Value

(Where Value is a location in data segment where 0x12345678h is stored)

move $t3, t4

Page 7

Q3.8. MIPS can be classified into which kind of architecture?


A.
Stack based
B.
Memory base
C.
Load-Store
D.
Register-Memory
Answer:

Q3.9. What is the default starting address of Data Memory and Text Memory?
Answer:

Q3.10. List the concepts you learnt from this experiment.(Conlcusion/observations)


Answer:

Page 8

You might also like