Submit this assignment by email by midnight of March 21. Be sure
to use "CS-343 Assignment 6" as the subject, and include your name
in the message body. Send your program as an attached text file and
answer the questions in the body of your email message.
Don't forget that you can get help for this and other assignments
at the Discussion Forum for the
course.
The Assignment
Write a MIPS assembly language program that adds several
numbers together using a loop. See below for help. Verify that
it is correct by stepping through the program one instruction at a
time.
Here are two lines from the Text Segment of the sample
program:
What are the hexadecimal numbers in square brackets at the
left end of each line?
What is the next hexadecimal number (0x3c011001, etc) on
each line?
Explain the difference between the assembly language code
to the left and to the right of the semicolon?
Explain how these two lines of code implement the first
lw instruction in sample.s.
Explain how the branch instruction in your program was
translated to machine language.
Help
Installing (PC/X)SPIM
There are three versions of the SPIM simulator on the CD that
comes with the book. (And there are updated versions on the SPIM
author's website if you are interested.) The simplest version is
called spim, but it works only from the command line, which
makes it less useful than the two graphical versions. The graphical
version for Windows is called pcspim and the graphical
version for Unix and Macintosh is called xspim.
To install the PC/Windows version, use WinZip to unzip the
pcspim.zip file that's in the Content/Software/Spim directory of the
CD. Then run setup.exe, and the program will be installed and ready
to go.
For Linux/Unix/Macintosh, you have to build and install the
program yourself. The file to use is spim.tar.gz from the same
directory as pcspim.zip. Unzip it (tar xvzf spim.tar.gz)
and you will get a directory named something like Spim-7.0. Change
to that directory and edit Imakefile to tell where you want
everything installed. Then run ./Configure, then
xmkmf, then make, make xspim, and
make install. These instructions are spelled out more
carefully in the README file. Ask on the course forum if you need
help getting set up.
Writing an Assembly Language Program
You have to use a text editor (not a word processor) to prepare
your program. Windows has Notepad, but if you are a CS student you
undoubtedly have a programmer's text editor that you prefer by now,
like vim or nedit or maybe a commercial one.
Create a text file with a .s extension. Here is a sample
program you can use as a model:
Comments are introduced by the '#' character.
Labels (symbols that represent memory addresses) appear at the
beginning of a line, and end with a ':'. Like a C program, your
code starts executing at main, so use that as the label of
your first instruction.
Memory is divided into three regions: text, where the
instructions go, data, where the data goes, and
kernel where the operating system resides. The SPIM
simulator comes with a small operating system kernel in a file
called exception.s. If the simulator is installed
correctly, exception.s is loaded into kernel memory
automatically whenever you run it. The text area starts at address
0x400000 and the data area starts at 0x10000000.
The instructions that start with dots, (.text,
.globl, .data, .asciiz, .word,
.space, and .end) are "assembler directives."
Use .text and .data to begin the text and
data memory segments of your program. There is no .org
directive despite what I said in class, and it's not needed
because of the standard beginning locations for the text and data
segments. (You can put addresses on .text and
.data, but there is no reason to bother.)
The main label has to be declared global by using the
.globl directive. When you run the simulator, you will
see that the kernel executes a few instructions and then calls
your program with a "jal main" instruction; your main has
to be global for this kernel instruction to get initialized
properly.
There is a syscall instruction that can be used to
invoke some simple kernel functions provided by
exception.s. The sample program prints a string of ascii
characters ("The sum is ") and an integer, and then "exits." If
you just run the program, a console window will show up, the
message will be displayed, and the simulator will stop
executing.
You should set up your array of numbers to be summed using
.word directives; only the first one needs a label. Use
at least half a dozen numbers, and be sure to include both
positive and negative values in the set of values.
The MIPS architecture normally does "delayed branches" and
"delayed loads." These features are related to the pipelined
design of the processor, which we will look at when we get to
chapter 6. But for now, they will make your program act
"strangely" and should be disabled. They are disabled by default
in xspim, but with pcspim you might have to turn them off using
the Simulator->Settings menu item.
Note that the assembler is "intelligent" and lets you do lw/sw
using any memory address. When necessary, it automatically
generates the necessary lui and ori instructions
that we went over in class. Also, you can use li as a
"load immediate" instruction: the assembler generates
addi and/or lui instructions for you as
necessary. And you can use la a a "load address"
instruction. If the sample program said, la $t0,
aNumber, the assembler would have generated the
lui/ori instructions needed to load the address
represented by aNumber into register $t0.
Finally, note that the syntax I used for register numbers in
class was wrong: it's not "$r5" to refer to register 5 for
example, it's just "$5." And all the registers have symbolic
names according to their conventional uses in MIPS assembly
language programs. For example $at is another name for
$1, the "assembler temporary" register mentioned in class. The
sample program uses registers $t0 and $t1, which
are "temps," corresponding to real registers $8 and $9. (There
are six more temps after that if you want to use them.) The
syscall instructions use $v0 ($2) to tell the
kernel which function to perform (print a string, print an
integer, or exit in the sample program), and use $a0 ($4) to pass
an argument, such as the address of a string or the value of an
integer to print. So if you use syscall instructions,
you have to be sure not to use $2 and/or $4 to hold anything
important.