Download Introduction to SPIM

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSCI-365
Computer Organization
Lecture
Note: Some slides and/or pictures in the following are adapted from:
Computer Organization and Design, Patterson & Hennessy, ©2005
Some slides and/or pictures in the following are adapted from:
slides ©2008 UCB
The SPIM Simulator

SPIM is a simulator that let you run and debug MIPS assembler
programs.

The simulator allows you to look at the content of registers and
memory, and to single step through the simulation.

Install PCSpim
http://www.cs.wisc.edu/~larus/spim.html

Documentation
 book: appendix A9 (on CD)
 www.cs.wisc.edu/~larus/SPIM/spim_documentation.pdf

Webinterface: http://cgi.aggregate.org/cgi-bin/cgispim.cgi
Steps to Starting a Program (translation)
C program: foo.c
Compiler
Assembly program: foo.s
Assembler
Object(mach lang module): foo.o
Linker
Executable(mach lang pgm): a.out
Loader
Memory
lib.o
Assembly Language (cont.)

Pseudo-instructions: extending the instruction set for
convenience.

Examples:
 move $2, $4
Translates to:
add $2, $4, $0
# $2 = $4, (copy $4 to $2)

li $8, 40
addi $8, $0, 40
# $8 = 40, (load 40 into $8)

sd $4, 0($29)
sw $4, 0 ($29)
sw $5, 4($29)
# mem[$29] = $4; Mem[$29+4] = $5

la $4, 0x1000056c
lui $4, 0x1000
ori $4, $4, 0x056c
# Load address $4 = <address>
MIPS
Pseudoinstructions
Copy
Arithmetic
Shift
Logic
Memory access
Control transfer
Pseudoinstruction
Usage
Move
Load address
Load immediate
Absolute value
Negate
Multiply (into register)
Divide (into register)
Remainder
Set greater than
Set less or equal
Set greater or equal
Rotate left
Rotate right
NOT
Load doubleword
Store doubleword
Branch less than
Branch greater than
Branch less or equal
Branch greater or equal
move
la
li
abs
neg
mul
div
rem
sgt
sle
sge
rol
ror
not
ld
sd
blt
bgt
ble
bge
regd,regs
regd,address
regd,anyimm
regd,regs
regd,regs
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
regd,reg1,reg2
reg
regd,address
regd,address
reg1,reg2,L
reg1,reg2,L
reg1,reg2,L
reg1,reg2,L
The MIPS
Instruction Set
Copy
Arithmetic
Logic
Memory access
Control transfer
Instruction
Usage
Load upper immediate
Add
Subtract
Set less than
Add immediate
Set less than immediate
AND
OR
XOR
NOR
AND immediate
OR immediate
XOR immediate
Load word
Store word
Jump
Jump register
Branch less than 0
Branch equal
Branch not equal
Jump and link
System call
lui
rt,imm
add
rd,rs,rt
sub
rd,rs,rt
slt
rd,rs,rt
addi rt,rs,imm
slti rd,rs,imm
and
rd,rs,rt
or
rd,rs,rt
xor
rd,rs,rt
nor
rd,rs,rt
andi rt,rs,imm
ori
rt,rs,imm
xori rt,rs,imm
lw
rt,imm(rs)
sw
rt,imm(rs)
j
L
jr
rs
bltz rs,L
beq
rs,rt,L
bne
rs,rt,L
jal
L
syscall
op fn
15
0
0
0
8
10
0
0
0
0
12
13
14
35
43
2
0
1
4
5
3
0
32
34
42
36
37
38
39
8
12
$0
$1
$2
$3
$4
$5
$6
$7
$8
$9
$10
$11
$12
$13
$14
$15
$16
$17
$18
$19
$20
$21
$22
$23
$24
$25
$26
$27
$28
$29
$30
$31
0
$zero
$at Reserved for assembler use
$v0
Procedure results
$v1
$a0
Procedure
$a1
Saved
arguments
$a2
$a3
$t0
$t1
$t2
Temporary
$t3
values
$t4
$t5
$t6
$t7
$s0
$s1
Saved
$s2
across
$s3
Operands
procedure
$s4
calls
$s5
$s6
$s7
More
$t8
temporaries
$t9
$k0
Reserved for OS (kernel)
$k1
$gp Global pointer
$sp Stack pointer
Saved
$fp Frame pointer
$ra Return address
A 4-b yte word
sits in consecutive
memory addresses
according to the
big-endian order
(most significant
byte has the
lowest address)
Byte numbering:
3
2
3
2
1
0
1
Recalling
Register
Conventions
0
When loading
a byte into a
register, it goes
in the low end Byte
Word
Doublew ord
A doubleword
sits in consecutive
registers or
memory locations
according to the
big-endian order
(most significant
word comes first)
Registers and
data sizes in
MIPS.
SPIM/MIPS assembly directives
.data
start data segment
.ascii "str"
store the string "str" in memory without '\0'
.asciiz "str"
idem, with '\0'
.byte 3,4,16
store 3 byte values
.double 3.14, 2.72
store 2 doubles
.float 3.14, 2.72
store 2 floats
.word 3,4,16
store 3 32-bit quantities
.space 100
reserve 100 bytes
.text
start text segment
© PG/HC Programming 5JJ70 pg 8
SPIM syscall examples
Service
Trap
code
print_int
$v0 = 1
$a0 = integer to print
prints $a0 to standard output
print_float
$v0 = 2
$f12 = float to print
prints $f12 to standard output
print_double
$v0 = 3
$f12 = double to print
prints $f12 to standard output
print_string
$v0 = 4
$a0 = address of first character
prints a character string to standard output
read_int
$v0 = 5
integer read from standard input placed in $v0
read_float
$v0 = 6
float read from standard input placed in $f0
read_double
$v0 = 7
double read from standard input placed in $f0
read_string
$v0 = 8
$a0 = address to place string, $a1 = max string length
reads standard input into address in $a0
sbrk
$v0 = 9
$a0 = number of bytes required
$v0= address of allocated memory
Allocates memory from the heap
exit
$v0 = 10
print_char
$v0 = 11
read_char
$v0 = 12
file_open
$v0 = 13
$a0 = full path (zero terminated string with no line feed),
$a1 = flags, $a2 = UNIX octal file mode (0644 for rw-r--r--)
$v0 = file descriptor
file_read
$v0 = 14
$a0 = file descriptor, $a1 = buffer address,
$a2 = amount to read in bytes
$v0 = amount of data in buffer from file
(-1 = error, 0 = end of file)
file_write
$v0 = 15
$a0 = file descriptor, $a1 = buffer address,
$a2 = amount to write in bytes
$v0 = amount of data in buffer to file
(-1 = error, 0 = end of file)
file_close
$v0 = 16
$a0 = file descriptor
Input
Output
$a0 = character (low 8 bits)
$v0 = character (no line feed) echoed
© PG/HC Programming 5JJ70 pg 9
Let’s try
.text
.globl main
main:
ori
$t0, $0, 0x2
ori
$t1, $0, 0x3
addu $t2, $t0, $t1
# $8  OR(0, 0x2)
# $9  OR(0, 0x3)
# $10  ADD($t0, $t1)
Hex address
Memory
Map in
MIPS
00000000
Reserved
1 M words
Program
Text segment
63 M words
00400000
10000000
Addressable
with 16-bit
signed offset
Static data
10008000
1000ffff
Data segment
Dynamic data
$gp
$28
$29
$30
448 M words
$sp
$fp
Stack
Stack segment
7ffffffc
80000000
Second half of address
space reserved for
memory-mapped I/O
Overview of the memory address space in MIPS.
n:
m:
r:
.data
.word 0x2
.word 0x3
.space 4
.text
.globl main
main:
la
lw
la
lw
addu
la
sw
$t5, n
$t0, 0($t5)
$t5, m
$t1, 0($t5)
$t2, $t0, $t1
$t5, r
$t2, 0($t5)
# load address of n to $t5
# load n to $t0
# load address of m to $t5
# load m to $t1
# $10  ADD($8, $9)
# load address of r to $t5
# store $10 to r
n:
m:
r:
.data
.word 0x2
.word 0x3
.space 4
.text
.globl main
main:
lw
lw
addu
sw
$t0, n
# load n to $t0
$t1, m
# load m to $t1
$t2, $t0, $t1 # $10  ADD($8, $9)
$t2, r # store $10 to r
System calls – print_str
str:
.data
.asciiz “Hello World”
.text
.globl main
main:
li $v0, 4
la $a0, str
syscall
# code for print_str
# argument
# executes print_str
System calls – read _int
.data
num: .space 4
.text
.globl main
main:
li $v0, 5
syscall
la $t0, num
sw $v0, 0($t0)
# code for read_int
# executes read_int
# return value is stored in $v0
# load address of num to $t0
# store the number in num
SPIM example 1: add two numbers
#
#
#
$t2
$v0
$a0
- used to hold the sum of the $t0 and $t1.
- syscall number, and syscall return value.
Assembler directive
- syscall input parameter.
starts with a dot
.text
# Code area starts here
li
$v0, 5
syscall
move
$t0, $v0
# read number into $v0
# make the syscall read_int
# move the number read into $t0
li
$v0, 5
syscall
move
$t1, $v0
# read second number into $v0
# make the syscall read_int
# move the number read into $t1
main:
add
$t2, $t0, $t1
move
$a0, $t2
li
$v0, 1
syscall
li
$v0, 10
syscall
# end of main
Special SPIM
instruction: system call
# move the number to print into $a0
# load syscall print_int into $v0
#
# syscall code 10 is for exit
#
© PG/HC Programming 5JJ70 pg 16