PROGRAMMING IN SPIM AND C
Steps in writing and running a program
- List all of the program's inputs from, and outputs to, main
memory.
- Write down (in plain English or pseudocode) what the program
must do with the inputs and how it is supposed to obtain the
outputs.
- Break a complicated procedure into simple tasks
and write a separate description for each task,
including a list of inputs from, and outputs to,
the main procedure.
- Use a text editing program (NOT a word processor) to create
your program. Windows text editing programs include Notepad
(bundled with the OS) and
GWD Text Editor
(shareware,
recognizes and color-codes HTML tags and keywords in C and
C++). UNIX text editors include vi and emacs. Macintosh
text editors include SimpleText (bundled with the OS) and
BBEdit Lite
(freeware).
- Start up your desktop computer (for SPIM), or
log in to the UTD UNIX machine apache (for C or C++),
or boot your favorite flavor of UNIX on your desktop
machine (also for C or C++).
- For SPIM, begin with the program's data segment.
This part of the program starts with a .data directive.
Define all of the variables and arrays that you will need,
using the SPIM instruction
summary as a guide. In C or C++, declare all of your
variables and arrays.
- For SPIM, the next part of the program is the text
segment, which begins with a .text directive. The
first executable instruction MUST be preceded (depending
on your version of SPIM) EITHER with the
label "__start:", which tells the loader what address
to hand to the processor when your program is loaded into
memory, OR with the directive .globl __start, then
a new line, then the label main: with nothing after the
colon (:).
In C or C++, this part of the program contains
executable statements.
- Type each statement or instruction on a separate
line, and push "return" at the end of each line.
- Turn off word wrapping if you can. Soft-wrapped
lines cause assembler errors that can be very
hard to trace, because you can't see the characters
that cause the errors.
- Save your file with a name such as program.s (for SPIM),
program.c (for C), or program.cpp (for C++).
- To print your program, choose Print from the File menu
of your text editor (Windows or MacOS), or use lp on
your UNIX machine.
- When you are ready to test a SPIM program, launch SPIM.
- In Windows or MacOS, pull down the File menu in SPIM and choose Load...
- Browse until you find the file that you created with
your text editor. Click on that file's name to load
it into SPIM/SAL.
- Pull down the Execute menu to run the program or go
through it step by step. Pull down other windows to
see what is happening to the contents of the registers
and memory as you step through your program. Standard
output will appear in the Console window.
- To save your output, take a
snapshot
of the Console window.
- When you are ready to test a C or C++ program, exit from vi or
emacs and invoke the C compiler with the command "cc program.c",
or invoke the C++ compiler with the command "cpp program.cpp".
Read the manual pages for cc or cpp to find out how to use
compiler options to control the compilation and execution of
your program.
- If the compiler finds a syntax error, it will exit with
an error message. Look for the statement that caused the
first error message, fix the problem, and recompile.
- When your program compiles without any errors, the
compiler will create a file named a.out in the directory
in which you ran the compiler. This is the executable
form of your program. Run it with the command "./a.out".
Example programs
The SPIM and C programs listed below are intended to be instructive
and/or useful as you improve your understanding of data representations
and assembler programming. Please refer to the
SPIM instruction summary. For I/O
you will need the table of system calls at the very end of the instruction summary.
If your Web browser is Internet Explorer (or some other browser that is not
cross-platform-aware), and if you are using an "IBM-compatible" PC, then you may
not be able to use text files downloaded from a UNIX server, because Internet
Explorer does not correctly convert end-of-lines
from UNIX format to DOS format.
If you right-click on declare.s and choose
"save target as" in Internet Explorer, it will be saved as a Notepad
file. Opening the file with Notepad may show a file that has
lots of little black rectangles. These are non-printing
ASCII linefeed characters (0x0a), which Notepad doesn't recognize as UNIX
end-of-lines.
If this is happening to you, then please right-click on the links titled
"DOS format" below, and save the targets in your SPIM program directory.
Normally Netscape will convert end-of-lines in an OS-aware way.
declare.c, a simple C program that interprets
binary data in various ways. (DOS format)
Typical output: On a
SPARC processor (Unix)
(DOS format),
on a PowerPC 601 processor (MacOS)
(DOS format),
or on a Motorola 68040 processor (MacOS)
(DOS format)
declare.s, a simple SPIM program that declares
a few types of data and prints the values in various ways. The way to use this
program is to single-step through it, watching the values stored in the
data segment (DOS format).
memory.s, which shows how to do
loads and stores in SPIM (DOS format)
io.s, which shows how to do
simple input/output in SPIM (DOS format)
init1d.s, a SPIM program that
loops through a 1-dimensional array and stores the same floating-point
constant in each location (DOS format)
init1d-proc.s, a SPIM program that
loops through a 1-dimensional array and stores the same floating-point
constant in each location, using a procedure call for the loop code
(DOS format)
init2d.s, a SPIM program that
loops through a 2-dimensional array using index computation,
and stores the same floating-point constant in each location
(DOS format)
init2dp.s, a SPIM program that
loops through a 2-dimensional array using pointers,
and stores the same floating-point constant in each location
(DOS format)
count.s, a SPIM program that
counts the number of characters in a given null-terminated string,
using the terminating null byte to signal the end of the string
(equivalent to a C "while" loop)
(DOS format)
John
Waldon's example programs are available either from his Web site, or
in a local directory
SPIM instruction summaries
SPIM instructions (text file)
James Larus's article on SPIM
Detailed summary of SPIM instructions,
including corrections to errors in the textbook
Programming in C
The following links may help you review or learn C:
Programming
in C, by David Marshall
Introduction
to C Programming, by Marshall Brain
C Programming,
v2.7, by Brian Brown