Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SPIM : A MIPS Simulator
Archi & Net Lab
이용석
(yslee@archi.snu.ac.kr)
Contents
Introduction
SPIM Interface
Simulator Usage
MIPS R2000 Assembler Language
Program Example
Introduction (1/2)
What’s SPIM?
A simulator that runs programs for the MIPS
R2000/R3000 RISC computers
What does SPIM do?
Reads and executes MIPS assembly language file
immediately
Works as a debugger
Provides some OS like services
Introduction (2/2)
Where can we get SPIM?
FTP: ftp.cs.wisc.edu
/pub/spim/spimwin.exe
Installing SPIM
Just run spimwin.exe and follow the installation
procedure
Windows Interface
Registers window
shows the values of all
registers in the MIPS CPU and
FPU
Text segment window
shows instructions
Data segment window
shows the data loaded into the
program’s memory and the
data of the program’s stack
Messages window
shows PCSpim messages
(include error messages)
Simulator Usage
Opening source file
Use File menu or toolbar button
Simulation
Go: Run loaded program
Break: Stop the execution of the program
Single / Multiple Step: Stepping for debugging
Breakpoint: Stop program before it executes a particular instruction
Reload: Reload source file after change it with editor program
MIPS Assembly Layout
Program Layout
.text
#code section
.globl main
#starting point: must be global
main:
# user program code
.data
label:
#data section
.data_type list_of_data
#data loc + data type + data
.text
label:
#code section
#function label
#user functions
MIPS Assembler Directives (1/2)
Data Types
.word, .half - 32/16 bit integer
.byte - 8 bit integer (similar to ‘char’ type in C)
.ascii, .asciiz - string (asciiz is null terminated)
Strings are enclosed in double-quotas(”)
Special characters in strings follow the C convention
• newline(\n), tab(\t), quote(\”)
.double, .float - floating point
MIPS Assembler Directives (2/2)
Other Directives
.text - Indicates that following items are stored in
the user text segment
.data - Indicates that following data items are
stored in the data segment
.globl sym - Declare that symbol sym is global
and can be referenced from other files
SPIM System Calls
System Calls (syscall)
OS-like services
Method
Load system call code into register $v0
Load arguments into registers $a0…$a3
After call, return value is in register $v0
Frequently used system calls
Service
Print_int
Print_string
Read_int
Code($v0)
Arg
1
4
5
$a1
$a0
Result
$v0
SPIM Program Example (1/3)
A Simple Program
#sample example 'add two numbers’
.text
.globl main
# text section
# call main by SPIM
main:
#
#
#
#
#
la $t0, value
lw $t1, 0($t0)
lw $t2, 4($t0)
add $t3, $t1, $t2
sw $t3, 8($t0)
.data
value: .word 10, 20, 0
load address ‘value’ into $t0
load word 0(value) into $t1
load word 4(value) into $t2
add two numbers into $t3
store word $t3 into 8($t0)
# data section
# data for addition
SPIM Program Example (2/3)
A Program with System Call
#sample example 'system call'
.text
.globl main
lw $t1, 0($t0)
lw $t2, 4($t0)
add $t3, $t1, $t2
sw $t3, 8($t0)
main:
la $t0, value
li $v0, 4
la $a0, msg1
syscall
li $v0, 1
move $a0, $t3
syscall
li $v0, 5
syscall
sw $v0, 0($t0)
li $v0, 5
syscall
sw $v0, 4($t0)
.data
value: .word 0, 0, 0
msg1: .asciiz "Result = "
SPIM Program Example (3/3)
A Program with Procedure Call
# sample example ‘swap two numbers’
.text
.globl
main
main:
la
$a0, array
addi
$a1, $0, 0
.data
array:
addi
sw
$sp, $sp, -4
$ra, 0($sp)
jal
swap
lw
addi
$ra, 0($sp)
$sp, $sp, 4
jr
$ra
.word 5, 4, 3, 2, 1
#
#
#
#
#
#
#
swap(int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
swap:
add
add
add
lw
lw
sw
sw
jr
$t1,
$t1,
$t1,
$t0,
$t2,
$t2,
$t0,
$ra
$a1, $a1
$t1, $t1
$a0, $t1
0($t1)
4($t1)
0($t1)
4($t1)