Title: SPIM
1SPIM
- Prof. Jerry Breecher
- Appendix A
2MIPS Simulation
- SPIM is a simulator.
- Reads a MIPS assembly language program.
- Simulates each instruction.
- Displays values of registers and memory.
- Supports breakpoints and single stepping.
- Provides simple I/O for interacting with user.
3SPIM Versions
- SPIM is the command line version.
- XSPIM is x-windows version (Unix workstations).
(This is the version you will be using in the
lab.). - There is also a windows version. You can use
this at home and it can be downloaded from - http//www.cs.wisc.edu/larus/spim.html.
- Note that evaluation of your projects will take
place using Unix You can prepare your code at
home OR in the lab, but it must be able to run on
LINUX.
4Resources On the Web
- Theres a very good SPIM tutorial at
- http//chortle.ccsu.edu/AssemblyTutorial/Chapter-0
9/ass09_1.html - In fact, theres a tutorial for a good chunk of
the ISA portion of this course at - http//chortle.ccsu.edu/AssemblyTutorial/tutorialC
ontents.html - Here are a couple of other good references you
can look at - Patterson_Hennessy_AppendixA.pdf
- And
- http//babbage.clarku.edu/jbreecher/comp_org/labs
/Introduction_To_SPIM.pdf
5SPIM Program
- MIPS assembly language.
- Must include a label main this will be called
by the SPIM startup code (allows you to have
command line arguments). - Can include named memory locations, constants and
string literals in a data segment.
6General Layout
- Data definitions start with .Data directive.
- Code definition starts with .Text directive.
- Text is the traditional name for the memory
that holds a program. - Usually have a bunch of subroutine definitions
and a main.
7Simple Example
- .data data memory
- foo .word 0 32 bit variable
- .text program memory
- .align 2 word alignment
- .globl main main is global
- main
- lw a0,foo
8Data Definitions
- You can define variables/constants with
- .word defines 32 bit quantities.
- .byte defines 8 bit quantities.
- .asciiz zero-delimited ascii strings.
- .space allocate some bytes.
9Data Examples
- .data
- prompt .asciiz Hello World\n
- msg .asciiz The answer is
- x .space 4
- y .word 4
- str .space 100
10MIPS Software Conventions For Registers
11Simple I/O
- SPIM provides some simple I/O using the syscall
instruction. The specific I/O done depends on
some registers. - You set v0 to indicate the operation.
- Parameters in a0, a1.
12I/O Functions
System call is used to communicate with the
system and do simple I/O. Load system call code
into Register v0 Load arguments (if any) into
registers a0, a1 or f12 (for floating
point). do syscall Results returned in registers
v0 or f0.
13Example Reading an int
- li v0,5 Indicate we want function 5
- syscall
- Upon return from the syscall, v0 has the
integer typed by - a human in the SPIM console
- Now print that same integer
- move a0,v0 Get the number to be printed
into register - li v0,1 Indicate were doing a
write-integer - syscall
14Printing A String
- .data
- msg .asciiz SPIM IS FUN
- .text
- .globl
- main li v0,4
- la a0,msg
- syscall
- jr ra
pseudoinstruction load immediate
pseudoinstruction load address
15SPIM Subroutines
- The stack is set up for you just use sp.
- You can view the stack in the data window.
- main is called as a subroutine (have it return
using jr ra). - For now, dont worry about details. But the next
few pages do some excellent example of how stacks
all work.
16Why Are Stacks So Great?
- Some machines provide a memory stack as part of
the architecture (e.g., VAX) - Sometimes stacks are implemented via software
convention (e.g., MIPS)
17Why Are Stacks So Great?
18MIPS Function Calling Conventions
SP fact addiu sp, sp, -32 sw ra,
20(sp) . . . sw s0, 4(sp) ...
lw ra, 20(sp) addiu sp, sp, 32 jr ra
19MIPS Function Calling Conventions
main() printf("The factorial of 10 is
d\n", fact(10)) int fact (int n) if (n
lt 1) return(1) return (n fact (n-1))
20MIPS Function Calling Conventions
.text .global main main subu
sp, sp, 32 stack frame size is 32
bytes sw ra,20(sp) save
return address li a0,10
load argument (10) in a0 jal
fact call fact la
a0 LC load string address in
a0 move a1,v0 load fact
result in a1 jal printf
call printf lw ra,20(sp)
restore sp addu sp, sp,32
pop the stack jr ra
exit() .data LC .asciiz "The
factorial of 10 is d\n"
21MIPS Function Calling Conventions
.text fact subu sp,sp,8 stack frame
is 8 bytes sw ra,8(sp) save return
address sw a0,4(sp) save argument(n) subu
a0,a0,1 compute n-1 bgtz a0, L2 if
n-1gt0 (ie ngt1) go to L2 li v0, 1 j L1
return(1) L2 new argument (n-1) is
already in a0 jal fact call fact lw
a0,4(sp) load n mul v0,v0,a0
fact(n-1)n L1 lw ra,8(sp) restore
ra addu sp,sp,8 pop the stack jr ra
return, result in v0
22MIPS Function Calling Conventions
23MIPS Function Calling Conventions
24MIPS Function Calling Conventions
25MIPS Function Calling Conventions
26MIPS Function Calling Conventions
27MIPS Function Calling Conventions
28MIPS Function Calling Conventions
29MIPS Function Calling Conventions
30MIPS Function Calling Conventions
31MIPS Function Calling Conventions
32MIPS Function Calling Conventions
33MIPS Function Calling Conventions
34MIPS Function Calling Conventions
35MIPS Function Calling Conventions
36MIPS Function Calling Conventions
37Sample SPIM Programs (on the web)
- multiply.s multiplication subroutine based on
repeated addition and a test program that calls
it. - http//babbage.clarku.edu/jbreecher/comp_org/labs
/multiply.s - fact.s computes factorials using the multiply
subroutine. - http//babbage.clarku.edu/jbreecher/comp_org/labs
/fact.s - sort.s the sorting program from the text.
- http//babbage.clarku.edu/jbreecher/comp_org/labs
/sort.s - strcpy.s the strcpy subroutine and test code.
- http//babbage.clarku.edu/jbreecher/comp_org/labs
/strcpy.s