Professional Documents
Culture Documents
5Z032
Processor Design
SPIM, a MIPS simulator
Henk Corporaal
1
SPIM
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
Alternative to SPIM
MARS: MIPS Assembler and Runtime Simulator
http://courses.missouristate.edu/KenVollmar/MARS/
SPIM in action
MIPS registers
Program
memory
Data memory
Messages
$t2
$v0
$a0
.text
li
$v0, 5
syscall
move
$t0, $v0
li
$v0, 5
syscall
move
$t1, $v0
main:
add
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 4
endwhile:
li
$t1, 0
blez $t0, endwhile
li
$v0, 4
la
$a0, prmpt2
syscall
li
$v0, 5
syscall
add $t1, $t1, $v0
sub $t0, $t0, 1
j
while
li
$v0,
la
$a0,
syscall
move $a0,
li
$v0,
syscall
li
$v0,
syscall
4
sumtext
$t1
1
10
# Syscall to exit
PG/HC Programming 5JJ70 pg 5
.ascii "str"
.asciiz "str"
.byte 3,4,16
store 2 doubles
store 2 floats
.word 3,4,16
.space 100
.text
Trap
code
print_int
$v0 = 1
print_float
$v0 = 2
print_double
$v0 = 3
print_string
$v0 = 4
read_int
$v0 = 5
read_float
$v0 = 6
read_double
$v0 = 7
read_string
$v0 = 8
sbrk
$v0 = 9
exit
$v0 = 10
print_char
$v0 = 11
read_char
$v0 = 12
file_open
$v0 = 13
file_read
$v0 = 14
file_write
$v0 = 15
file_close
$v0 = 16
Input
Output
C code:
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// recursive call
void main(void) {
int a, b;
printf("Enter a and b: ");
scanf("%d%d", &a, &b);
printf("gcd(%d, %d) = %d\n", a, b, gcd(a, b));
}
PG/HC Programming 5JJ70 pg 8
.text
addi $sp, $sp, -4
sw
$ra, 0($sp)
beqz $a1, exit_gcd
div $a0, $a1
mfhi $t1
move $a0,$a1
move $a1,$t1
jal gcd
#
#
#
#
#
#
#
#
move
lw
addi
jr
#
#
#
#
$v0=$a0
restore the return address
adjust stack pointer
return to caller
exit_gcd:
$v0, $a0
$ra, 0($sp)
$sp, $sp, 4
$ra
Q: Why do we not need to save the arguments $a0 and $a1 on the stack?
Note we could have optimized above code using tail-recursion.
PG/HC Programming 5JJ70 pg 9
Control
Hi
Lo
32-bit
32-bit
mflo
mfhi
mtlo
mthi
$t1
$t1
$t1
$t1
PG/HC Programming 5JJ70 pg 10
main:
again:
.data
.asciiz
.asciiz
.asciiz
.asciiz
.asciiz
.text
addi $sp, $sp, -4
sw
$ra, 0($sp)
la
$a0, str1
li
$v0, 4
syscall
li
$v0, 5
syscall
move $s0, $v0
bltz $s0, again
#
#
#
#
...
move
move
jal
move
$a0, $s0
$a1, $s1
gcd
$t0, $v0
#
#
#
#
$a0=$s0
$a1=$s1
go to gcd
$t0=$v0
...
la
$a0, str5
li
$v0, 4
syscall
#
# print of str5
#
lw
$ra, 0($sp)
addi $sp, $sp, 4
jr
$ra
SPIM in action
MIPS registers
Program
memory
Data memory
Messages