Transcript Spim

Compilers
Modern Compiler Design
Supplementary Note 2
SPIM Overview
NCYU C. H. Wang
1
SPIM Simulator
Spim is a self-contained simulator that will run
MIPS32 assembly language programs.
 Spim reads and immediately executes assembly
language code for this processor.
 Spim provides a simple debugger and minimal set
of operating system services.
 SPIM’s name is just MIPS spelled backwards
 SPIM can read and immediately execute MIPS
assembly language files or MIPS executable files
 Web site: http://www.cs.wisc.edu/~larus/spim.html

2
MIPS Processors
 MIPS
is a load-store architecture, which
means that only load and store instructions
access memory
 Computation instructions operate only on
values in registers
3
MIPS Registers (1/2)
4
MIPS Registers (2/2)
5
Addressing Modes
6
Assembler Syntax (1)
Comments in assembler les begin with a sharpsign (#). Everything from the sharp-sign to the end
of the line is ignored.
 Identifiers are a sequence of alphanumeric
characters, underbars (_), and dots (.) that do not
begin with a number.
 Opcodes for instructions are reserved words that
are not valid identifiers.
 Labels are declared by putting them at the
beginning of a line followed by a colon.

7
Assembler Syntax (2)

Example
8
Assembler Syntax (3)

.data <addr>
 The following data items should be stored in the data
segment. If the optional argument addr is present, the
items are stored beginning at address addr.

.text <addr>
 The next items are put in the user text segment. In
SPIM, these items may only be instructions or words
(see the .word directive below). If the optional argument
addr is present, the items are stored beginning at
address addr.
9
Assembler Syntax (4)

.word w1, ..., wn
 Store the n 32-bit quantities in successive memory words.

.double d1, ..., dn
 Store the n floating point double precision numbers in successive
memory locations.

.float f1, ..., fn
 Store the n floating point single precision numbers in successive
memory locations.

.globl sym
 Declare that symbol sym is global and can be referenced from other
files.
10
Assembly Instructions (1)
 Load,
li
Store and Data Movement
rd, imm
la
rd, label
lw
rd, imm(rs)
sw
rd, imm(rs)
move rd, rs
rd  imm
rd  label
rd  imm(rs)
imm(rs)  rd
rd  rs
11
Assembly Instructions (2)
 Arithmetic
add
sub
mul
div
rem
neg
& Logical Instructions
rd, rs, rt
rd, rs, rt
rd, rs, rt
rd, rs, rt
rd, rs, rt
rd, rs
rd  rs + rt
rd  rs – rt
rd  rs * rt
rd  rs / rt
rd  rs % rt
rd  - rs
12
Assembly Instructions (3)
 Branch
beq
bne
bgt
bge
blt
ble
b
& Jump Instructions
rs, rt, label
rs, rt, label
rs, rt, label
rs, rt, label
rs, rt, label
rs, rt, label
label
branch to label if rs == rt
branch to label if rs != rt
branch to label if rs > rt
branch to label if rs >= rt
branch to label if rs < rt
branch to label if rs <= rt
branch to label
13
Layout of Memory
14
System Services (1)
15
System Services (2)
SPIM provides a small set of operating-system-like
services through the system call (syscall)
instruction.
 To request a service, a program loads the system
call code (see Table 1) into register $v0 and the
arguments into registers $a0: : :$a3 (or $f12 for
oating point values).
 System calls that return values put their result in
register $v0 (or $f0 for oating point results).

16