Professional Documents
Culture Documents
LAB I: SPIM
!
Abelardo Lpez Lagunas, Ph.D.
School of Engineering and Architecture
Computer Science department
ITESM Campus Toluca
abelardo.lopez@itesm.mx
Version 1.0
!1
Outline
SPIM Installation
Basic documentation
Tool usage
Simple programs
Matrix multiplication
Version 1.0
!2
Tools: SPIM
Version 1.0
!3
SPIM
Version 1.0
!4
Version 1.0
!5
Equivalent
name
Description
zero
the value of 0
$at
2-3
$v0-$v1
4-7
$a0-$a3
8-15
$t0-$t7
16-23
$s0-$s7
24-25
$t8-$t9
Version 1.0
!6
Equivalent
name
Description
26-27
$k0-$k1
28
$gp
29
$sp
30
$s8-$fp
31
$ra
(return address)
So to keep thing simple yo can safely use zero, $t0 - $t9 and $s0 - $s7
Version 1.0
!7
Program structure
Using an editor you can create a plain text file with the
following structure:
Data declarations: Declares variable names used in the program.
Use the assembler directive .data to star this section.
# Example:
!
var1: .word 3
# Single integer variable with initial value of 3
array1: .byte 'a','b' # 2-element character array with elements a and b
array2: .space 40
# allocate 40 consecutive bytes. Not initialised
Program code: Contains the actual code. The starting point of the
code should start with a label, such as main. Use the assembler
directive .text to star this section.
Version 1.0
!8
into RD.
lb RD RWM_source copies one byte from the RWM address into
the low order byte of RD. The high order bytes are signextended.
li RD value copies the immediate value into RD.
la RD variable copies the address of the variable into RD.
store
sw RS RWM copies four bytes from RS into the address RWM.
sb RS RWM copies one byte from the lower byte f RS into the
lower byte of the address RWM. Note that the high order bytes
of the RWM location are sign-extended.
Version 1.0
!9
Memory examples
! ! .data!
var1:! .word!!23!!#!declares!storage!for!var1!and!sets!its!value!to!23!
!
.text!
__start:! ! ! ! #!Program!entry!point!
! ! lw!$t0,!var1!#!Load!the!contents!of!RWM!location!23!into!$t0!
!
!
li!$t1,!5!!
#!Loads!constant!5!into!$t1!
sw!$t1,!var1!#!Stores!a!5!into!RWM!memory!location!var1
!
!
23
23
23
var1
var1
$t0
$t1
$t1
1
Version 1.0
3
Abelardo Lpez Lagunas
5
$var1
2
!10
lw RD, (RS) loads the word from the RWM address in RS into RD.
sw RS, (RD) stores the word from RD into the RWM address of RD.
lw RD, k(RS) loads the word from the RWM address in RS plus the
base value k into RD.
sw RS, k(RD) stores the word from RD plus the base value k into the
RWM address of RD.
Version 1.0
This addressing mode is very useful for array access as offsets from a
base address and for accessing elements in the stack at an offset
form a stack or frame pointers.
!11
Memory examples
! ! ! .data!
array1:!!.space!!12!#!allocates!12!bytes!if!storage!for!3!array!elements!
! ! !
__start:!
! ! !
! ! !
! ! !
! ! !
! ! !
! ! !
! ! !
.text!
la!!!!!!$t0,!array1!#!Load!base!address!of!array!
li!!!!!!$t1,!5! ! ! #!Set!$t1!to!five!
sw!$t1,!($t0)!! ! ! #!array1[0]!=!5!
li!$t1,!13! ! ! ! #!Set!$t1!to!13!
sw!$t1,!4($t0)! ! ! #!array1[1]!=!13!
li!$t1,!O7! ! ! ! #!Set!$t1!to!O7!
sw!$t1,!8($t0)! ! ! #!array1[2]!=!O7!
done!
The above example is not how you store elements into an array
programmatically. Instead you set the base of the array and iterate
over the total number of elements.
Version 1.0
!12
Version 1.0
!13
Branches, Jumps
Version 1.0
!14
Subroutine calls
Version 1.0
!15
Code in
$v0
Arguments
Result
print_int
print_float
print_double 3
print_string
read_int
read_float
read_double
read_string
sbrk
exit
10
Version 1.0
!16
The print_string service expects the address to start a nullterminated character string.
A null terminated string ends with character \n
The directive .asciiz creates a null-terminated character string.
The read_int, read_float and read_double services read an
entire line of input up to and including the newline character.
The read_string service has the same semantics as the UNIX
library routine fgets.
It reads up to n-1 characters into a buffer and terminates the string with a
null character.
If fewer than n-1 characters are in the current line, it reads up to and
including the newline and terminates the string with a null character.
Version 1.0
!17
#!Load!the!print_int!service!
move!$a0,!$t2!!
#!Move!integer!to!be!printed!from!$t2!into!$a2!
#!Move!is!NOT!an!actual!MIPS!instruction!but!a!MACRO!
syscall!
#!syscall!is!NOT!an!actual!instruction!but!a!SPIM!directive
Read an integer
#!Read!an!integer!value!and!store!it!into!a!location!in!RWM!store_int!
!
li! $v0,!5!!!!!
#!Load!the!read_int!service!
syscall!
#!syscall!is!NOT!an!actual!instruction!but!a!SPIM!directive!
sw!$v0,!store_int! #!Stores!the!result!integer!in!memory!address!store_int
Version 1.0
!18
.data!
str!!
.asciiz!
String!example\n! #!Declare!the!string!to!be!printed!
.text! !
#!Code!starts!here!
main:! !
li! $v0,!4!!
#!Code!for!print_string!service!
la!$a0,!str! !
#!Load!the!base!address!of!the!string!
syscall!
#!Print!the!string!
li!$v0,!10!
#!Code!for!ending!the!program!
syscall!
#!End!the!program
!
!
Version 1.0
!19
Version 1.0
!20