
Cpr E 381 Homework 2
1. Install a version of spim (the MIPS
simulator). The software is available for
Windows and Linux/Unix/Mac OSX
systems. It can be found on the CD
included with your book or can be
downloaded from the web
(http://pages.cs.wisc.edu/~larus/spim.html).
Read the platform specific introduction to
the simulator.
Type the program at right into a text file
named “hw2.s”.
Load the program into spim.
A) Run the program several times trying
different values. Submit results for at
least three distinct values.
B) What is the program doing? Keep your
answers brief.
C) Submit a line commented source code
describing the program.
D) Translate and submit the program into
C code.
You will want to look at Chapter 2 as a
reference for instructions. Appendix A from
the book (may be on the CD) will also be
useful, especially for the “syscall” operations.
.data
str1: .asciiz " Please enter a number >= 1 : "
str2: .asciiz "\n\n The result is:"
.text
.globl main
main:
li $v0, 4
la $a0, str1
syscall
li $v0, 5
syscall
move $t1, $v0
addi $t3, $zero, 1
slti $t2, $t1, 1
beq $t3, $t2, do_nothing
addi $t0, $t0, 0
loop:
sge $t2, $t1, $t3
beq $t2, $zero, end
add $t0, $t0 , $t1
addi $t1, $t1 , -1
j loop
end:
li $v0, 4
la $a0, str2
syscall
li $v0, 1
move $a0, $t0
syscall
do_nothing:
li $v0, 10
syscall