Sunteți pe pagina 1din 3

Lab 1 Introduction to MIPS Lab

5 Points
CS 281 Systems Architecture I
Overview
In this lab you will trace through a simple MIPS program using the SPIM simulator. The
SPIM simulator is described in Appendix A and can be obtained from
http://www.cs.wisc.edu/~larus/spim.html.
Objective
To become familiar with the SPIM simulator and MIPS assembly and machine language.
Readings
! Appendix A Section 9
! Tutorials on BBLearn
Outline
In this lab, you will trace through a simple MIPS program and record all of the state
changes (i.e. changes to registers, including the PC, and the stack). You will also list the
instructions that are executed and the corresponding machine code.
Things You Need to Know
! Instructions
" add rd, rs, rt; rd = rs + rt
" sub rd, rs, rt; rd = rs - rt
" lw rt, addr(rs); rt = Mem[addr + rs]
" sw rt, addr(rs); Mem[addr + rs] = rt
" jr rt; PC = rt [jump register]
" jal addr; [jump and link] PC = addr
! Register conventions and the use of the stack
" Registers a0,...,a3 are used to pass arguments to a procedure. Registers v0 and v1
are used to hold the results of a function call. Registers t0,...,t7 are temporary
registers that need not be saved by the called procedures. Registers s0,...,s7 are
stored registers that must be saved by the called procedure.
" The top of the stack is pointed to by the stack pointer (register $sp). By convention
the stack grows down (i.e. a push is performed with a subtraction). The stack is
used to save information and create space for local variables when a procedure is
called. The behavior of procedure calls is compatible with a stack - a procedure is
not finished until all of the procedures it calls are finished, and so on ...

! Assembler syntax: Directives, Pseudo-instructions, labels


" .globl (to make a symbol globally known)
" .text (to indicate that instructions follows)
" .data (to indicate that data follows)
" .asciiz (to create a null-terminated string)
" li (pseudo-instruction to load a constant)
" la (pseudo-instruction to load an address)
" Defining Strings (with .asciiz). Also be aware of ASCII coding of characters.
! System calls to print and input data
! SPIM uses system calls syscall for input and output. (See page A-43 in COD). The
register $v0 is used to indicate what the system call should do. Inputs to the system call
are passed through the register $a0 and outputs are returned through the register $v0.
Note that input is entered and output is printed f rom/to the console window.
" $v0 = 1 is used to print the integer in $a0.
" $v0 = 4 is used to print the string located at the address in $a0.
" $v0 = 5 is used to read an integer from the console. It is returned in $v0.
! Convention for a procedure call
" Arguments to a procedure are passed in registers $a0,..., $a3. If additional
arguments are necessary they are passed on the stack.
" If a procedure modifies registers they must first be saved on the stack so that when
the procedure call exits, the state of the registers are as the were prior to the call
(the exception are registers $t0,...,$t7 which must be saved by the calling procedure
prior to the call).
" A procedure call is made with the jal instruction. This instruction sets the PC to the
address of the procedure and saves the return address (i.e. the instruction that
should be executed after the call finishes) in the return address register $ra.
" A return from a procedure call is made using the jump register jr instruction with the
return address given in $ra. If the procedure has a return value it is passed back to
the calling procedure in $v0.
What To Do
1. Download and install QtSpim
a. Start from http://www.cs.wisc.edu/~larus/spim.html. After studying the information
in this page, go to sourceforge.net to download the installation file for your particular
computer type.
b. If you do not want to install QtSpim on your computer, then download and install
Putty and Xming following the directions in BBLearn. Use Putty and Xming to open
an X Window Session to tux.cs.drexel.edu to login to your tux account and access
Xspim on tux.
2. Download the MIPS program sum.asm
3. Carefully read through this program. There are detailed comments which explain MIPS
instructions, assembly syntax, and conventions that you must learn.
4. Start the SPIM program and load sum.asm using open in the File menu. Run the
program using the go command in the Simulator menu. After the program completes,
you can see the output in the console window. Select Console in the Window menu.

5. Step through the program one instruction at a time using the single step command in
the Simulator menu. You should step until the main program is entered (see the jal
main instruction). After main is entered, you should step through until main is
completed. Write down each instruction that is executed and record any change in a
register or the stack. Also record the machine code for each instruction along with its
address. You can save the contents of the SPIM windows by using Save Log File in the
File menu. After saving the log file, you can cut and paste instructions and their
corresponding machine code. Indicate the format and contents of the fields of the first
add and lw instructions you encounter.
What To Hand In
Please submit the following:
1. A copy of the console window after running the sum.asm program. Use Printscreen in
Windows to copy and then paste the picture of the console window to a word
processor. Alternatively, you can save a log file of the QtSpim session (this is done
using the Save Log File command in the File menu). Once the log file is saved, you can
open it with any editor you want and can cut and paste from there. Note that you can
copy and paste from the QtSpim windows. (1 point)
2. A trace of the execution of sum.asm. Include all of the information asked for in Step 5.
(3 points)
3. A description file that thoroughly details all testing you have done, discusses your
results and mentions any difficulties you may have encountered. Please follow the
specifications for a description file. (1 point)

S-ar putea să vă placă și