Chapter 1
Chapter 1
Engineering Faculty
Department of Computer Engineering
ECOM 3010: Computer Architecture Discussion
Lecture # 1
SPIM
Spim is a self-contained simulator that runs MIPS32 programs. It reads and executes assembly
language programs written for this processor. Spim also provides a simple debugger and
minimal set of operating system services. Spim does not execute binary (compiled) programs.
Spim implements both a terminal and windows interfaces. On Microsoft Windows, Linux, and
Mac OS X, the spim program offers a simple terminal interface and the QtSpim program
provides the windowing interface.
QtSpim
The newest version of Spim is called QtSpim, and unlike all of the other version, it runs on
Microsoft Windows, Mac OS X, and Linuxthe same source code and the same user interface
on all three platforms! QtSpim is the version of Spim that currently being actively maintained. It
has a modern user interface, extensive help, and is consistent across all three platforms.
When you open QtSpim, A window will open as shown in Figure 1. The window is divided into
different sections:
1. The Register tabs display the content of all registers.
2. Buttons across the top are used to load and run a simulation
3. The Text tab displays the MIPS instructions loaded into memory to be executed. (From
left-to-right, the memory address of an instruction, the contents of the address in hex,
the actual MIPS instructions where register numbers are used, the MIPS assembly that
you wrote, and any comments you made in your code are displayed.)
4. The Data tab displays memory addresses and their values in the data and stack
segments of the memory.
5. The Information Console lists the actions performed by the simulator.
Buttons
Register
tabs
Messages
Figure 1: QtSpim
MIPS
The MIPS architecture is a Reduced Instruction Set Computer (RISC). This means that there are
a smaller number of instructions, using a uniform instruction encoding format. Each
instruction/operation does one thing (memory access, computation, conditional, etc.). The idea
is to make the lesser number of instructions execute faster. In general RISC architectures, and
specifically the MIPS architecture, are designed for high-speed implementations.
Commenting:
Before we start to write the executable statements of program, however, we'll need to write a
comment that describes what the program is supposed to do. In the MIPS assembly language,
any text between a pound sign (#) and the subsequent newline is considered to be a comment.
# add.asm-- A program that computes the sum of 1 and 2,
# leaving the result in register $t0.
# Registers used:
# t0 - used to hold the result.
# end of add.asm
Syscall
The way to tell SPIM that it should stop executing your program, and also to do a number of
other useful things, is with a special instruction called a syscall. The syscall instruction suspends
the execution of your program and transfers control to the operating system. The operating
system then looks at the contents of register $v0 to determine what it is that your program is
asking it to do.
Service
Code
Arguments
Result
print_int
$a0
none
print_float
$f12
none
print_double
print_string
3
4
$f12
$a0
none
none
read_int
read_float
5
6
none
none
$v0
$f0
read_double
none
$f0
read_string
sbrk
8
9
none
$v0
exit
10
none
none
In this case, what we want is for the operating system to do whatever is necessary to exit our
program. Looking in table 1, we see that this is done by placing a 10 (the number for the exit
syscall) into $v0 before executing the syscall instruction.
We can use the li instruction again in order to do this:
# add.asm-- A program that computes the sum of 1 and 2,
# leaving the result in register $t0.
# Registers used:
# t0 - used to hold the result.
# t1 - used to hold the constant 1.
main:
li $t1, 1
add $t0, $t1, 2
li $v0, 10
syscall
# end of add.asm
Using QtSpim
1- Click on the load button
Standard startup
code
The code start
here
Debugging
Suppose your program does not do what you expect. What can you do? QtSpim has two
features that help debug your program.
The first, and perhaps the most useful, is single stepping, which allows you to run your program
an instruction at a time. The single stepping icon
can be found in the toolbar. Every time
you do single stepping, QtSpim will execute one instruction and update its display, so that you
can see what the instruction changed in the registers or memory.
What do you do if your program runs for a long time before the bug arises? You could single
step until you get to the bug, but that can take a long time. A better alternative is to use a
breakpoint, which tells QtSpim to stop your program immediately before it executes a
particular instruction. When QtSpim is about to execute the instruction where there is a
breakpoint, it asks for continue, single stepping or abort.
Single-stepping and setting breakpoints will probably help you find a bug in your program
quickly. How do you fix it? Go back to the editor that you used to create your program and
change it. Click on the Riinitialize simulator tab in the toolbar and load the sourcefile again.
10
li $v0, 10
syscall
# end of add2.asm.
11
"Hello World\n"
.text
main:
la $a0, hello_msg
li $v0, 4
syscall
li $v0, 10
syscall
# end hello.asm
Homework
Write a MIPS assembly language program that computes the difference between two numbers
specified by the user at runtime, and displays the result on the screen.
The program should display the message "Enter First Integer >>" to let the user enter the first
integer and store the first integer in $t1, then display the message "Enter Second Integer >> "
and store the second integer in $t2. After computing the result display "The Result is = " then
display the result.
You must submit the program printed with screenshot of Console after run the program.
Best Wishes