QtSpim Basics
QtSpim Basics
QtSpim is a self-contained simulator that will run a MIPS32 assembly program and
display the processor's registers and memory. QtSpim reads and executes programs
written in assembly language for a MIPS computer. QtSpim does not execute binary
(compiled) programs. To simplify programming, QtSpim provides a simple
debugger and small set of operating system services.
In this lab we are going to present a brief overview of QtSpim and implement our
program here.
1
QtSpim's main window has three parts:
The narrow pane on the left can display integer or floating-point registers.
Select the set of registers by clicking the tab at the top of the pane.
The wide pane on the right can display the text segment, which contains
instructions, and the data segments. Choose between text and data by clicking
the tab at the top of the pane.
The small pane on the bottom is where QtSpim writes its messages.
All of the panes are dockable, which means that you can grab a pane by its top bar
and drag it out of QtSpim's main window, to put on some other part of your screen.
QtSpim also opens another window called Console that displays output from your
program.
Loading a Program
Your program should be stored in a file. Assembly code files usually have the
extension ".s", as in file1.s. To load a file, go to the File menu and select Load File.
The screen will change as the file is loaded, to show the instructions and data in
your program.
Another very useful command on the File men is Reinitialize and Load File. It first
clears all changes made by a program, including deleting all of its instructions, and
then reloads the last file. This command works well when debugging a program, as
2
you can change your program and quickly test it in a fresh computer without closing
and restarting QtSpim.
Running a Program
To start a program running after you have loaded it, go to the Simulator menu and
click Run/Continue. Your program will run until it finishes or until an error occurs.
Either way, you will see the changes that your program made to the MIPS registers
and memory, and the output your program writes will appear in the Console window.
If your program does not work correctly, there are several things you can do. The
easiest is to single step between instructions, which lets you see the changes each
instructions makes, one at a time. This command is also on the Simulator menu and
is named Single Step.
Sometimes, however, you need to run your program for a while before something
goes wrong, and single stepping would be too slow. QtSpim lets you set a breakpoint
at a specific instruction, which stops QtSpim before the instruction executes. So, if
you think your problem is in a specific function in your program, set a breakpoint at
the first instruction in the function, and QtSpim will stop every time the function is
invoked. You set a breakpoint by right-clicking on the instruction where you want
to stop, and selecting Set Breakpoint. When you are done with the breakpoint, you
can remove it by selecting Clear Breakpoint instead.
If you want to stop your program while it is running, go to the Simulator menu and
click Pause. This command stops your program, let you look around, and continue
execution if you want. If you do not want to continue running, click Stop instead.
When QtSpim stops, either because of an error in your program, a breakpoint, after
clicking Pause, or after single stepping, you can continue the program running by
clicking on Run/Continue (or you can continue single stepping by clicking Single
Step). If you click Stop, instead of Pause, then clicking Run/Continue will restart
your program from the beginning, instead of continuing from where it stopped. (This
is roughly the same way that a music player operates; you can pause and restart a
song, but if you stop the music, you need to start playing at the beginning.)
3
Display Options
The three other menus -- Registers, Text Segment, and Data Segment -- control
QtSpim's displays. For example, the Register menu controls the way QtSpim
displays the contents of registers, either in binary, base 8 (octal), base 10 (decimal),
or base 16 (hexadecimal). It is often quite convenient to flip between these
representations to understand your data.
These menus also let you turn off the display of various parts of the machine, which
can help reduce clutter on the screen and let you concentrate on the parts of the
program or data that really matter.
You can change the contents of either a register or memory location by right-clicking
on it and selecting Change Register Contents or Change Memory Contents,
respectively.
Settings
The Simulator menu contains the Settings command, which brings up a dialog like
this:
4
The dialog has two tabs. The first, shown above, changes the visual aspects of
QtSpim, such as the fonts. The second looks like this:
5
It changes the way that QtSpim operates:
The button marked Simple Machine enables the most common options (Accept
Pseudo Instructions) that are what most people use. The button marked Bare
Machine turns on the instructions corresponding to a real MIPS processor (Bare
Machine, Delayed Branches, and Delayed Loads).
3. Experimental Work
Part 1:
In this part, you will use the SPIM in pseudo-code allowing mode.
Clear the bare machine setting, and check only the allow-pseudocode option
in the settings dialog-box (key-sequence alt-S,L).
Write the following text to a file named "exp2.asm"
.data
.text
.globl main
main:
li $8,0x3210
li $9,0x76543210
sge $11,$8,$9
mul $12,$11,$10
infloop:
bge $11,$0,infloop
syscall
6
Load the file to SPIM, and watch the corresponding machine codes of each
line. Use the log file to fill in the following binary-machine-code table to
understand the fields of each instruction in a better manner.
Note that the given text is not a program, it is not traceable. It contains a sample of
some commonly used MIPS pseudo-instructions.
7
# A demonstration of some simple MIPS instructions
.globl main
.text
main:
syscall # Exit
.data
value: .word 12
Z: .word 0
8
9. Reporting
Before the Lab-time is over, fill in the following report page as soon as you complete
the laboratory work, and submit it to your assistant. Your report is important for your
grading.
9
Part 1: The observed binary machine codes of the instructions are:
10
Part 2: The observed binary machine codes of the instructions are:
Instruction opc rs rt rd sa fn
li $t2, 25
lw $t3, value
add $t4, $t2, $t3
sub $t5, $t2, $t3
sw $t5, Z
li $v0, 10
Grading:
Lab Performance:
Asst. Observations:
11