You are on page 1of 10

TE2031 Computer Architecture

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

Abelardo Lpez Lagunas

!1

Outline

SPIM Installation
Basic documentation
Tool usage
Simple programs
Matrix multiplication

Version 1.0

Abelardo Lpez Lagunas

!2

Tools: SPIM

SPIM is a Mips-32 instruction set simulator which we


will use for topic two and maybe four of the class.

Download a windows/OSX/Linux version from:


http://sourceforge.net/projects/spimsimulator/files/

You can find the MIPS-32 ISA documentation at:


http://www.imgtec.com/mips/mips32-architecture.asp
!
The quick reference guide is free but you will need to register to
download the ISA documents.

Version 1.0

Abelardo Lpez Lagunas

!3

SPIM

SPIM - MIPS32 simulator

Version 1.0

Abelardo Lpez Lagunas

!4

MIPS: Assembly Language format

Syntax of Instructions: <op> <dest> <src1> <src2>


op name of operation
dest destination of the operation
src1 1st operand, a.k.a. source1
src2 2nd operand, a.k.a. source2
Syntax is rigid:
1 operator, 3 operands
This is to keep hardware simple because all instructions are
regular and easy to decode.
It is also good when you build an assembler (program that
converts the ASCII human readable version of your code into a
machine readable representation)

Version 1.0

Abelardo Lpez Lagunas

!5

Registers: Numbers vs Equivalent Names

The MIPS assembly handles registers in two ways:


Using the register number $0 through $31
Using the following equivalent names:
Register
Number

Equivalent
name

Description

zero

the value of 0

$at

Assembler Temporary is reserved by the assembler, do not use.

2-3

$v0-$v1

(values) from expression evaluation and function results

4-7

$a0-$a3

(arguments) First four parameters for subroutine. Not preserved


across procedure calls

8-15

$t0-$t7

(temporaries) Caller saved if needed. Subroutines can use w/out


saving. Not preserved across procedure calls.

16-23

$s0-$s7

(saved values) - Callee saved. A subroutine using one of these


must save original and restore it before exiting. Preserved across
procedure calls

24-25

$t8-$t9

(temporaries) Caller saved if needed. Subroutines can use w/out


saving. These are in addition to $t0 - $t7 above. Not preserved
across procedure calls.

Version 1.0

Abelardo Lpez Lagunas

!6

Registers: Numbers vs Equivalent Names


Register
Number

Equivalent
name

Description

26-27

$k0-$k1

reserved for use by the interrupt/trap handler.

28

$gp

global pointer. Points to the middle of the 64K block of memory in


the static data segment.

29

$sp

stack pointer. Points to last location on the stack.

30

$s8-$fp

saved value / frame pointer Preserved across procedure calls.

31

$ra

(return address)

So to keep thing simple yo can safely use zero, $t0 - $t9 and $s0 - $s7

Version 1.0

Abelardo Lpez Lagunas

!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.

The format for data declarations is:


name: storage type value(s)

# 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.

To end the program use the exit system call.

Comments start with a # and go through the end of the


line.

Version 1.0

Abelardo Lpez Lagunas

!8

Direct Addressing mode

Memory can only be accessed through load and store


instructions.
load
lw RD RWM_source copies four bytes from the RWM address

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

Abelardo Lpez Lagunas

!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

Indirect & Base addressing modes

MIPS also has indirect and offset with a base (aka.


indexed) addressing modes:
indirect addressing

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.

offset with a base addressing:

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.

Note that negative offset values k are possible.

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.

Abelardo Lpez Lagunas

!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

Abelardo Lpez Lagunas

!12

TE2031 Computer Architecture


LAB II: SPIM
!
Abelardo Lpez Lagunas, Ph.D.
School of Engineering and Architecture
Computer Science department
ITESM Campus Toluca
abelardo.lopez@itesm.mx

Version 1.0

Abelardo Lpez Lagunas

!13

Branches, Jumps

Branches are used to conditionally jump based on the result


of comparing the values between two registers.
!

beq $t0,$t1,target # branch to target if $t0 = $t1


blt $t0,$t1,target # branch to target if $t0 < $t1
ble $t0,$t1,target # branch to target if $t0 <= $t1
bgt $t0,$t1,target # branch to target if $t0 > $t1
bge $t0,$t1,target # branch to target if $t0 >= $t1
bne $t0,$t1,target # branch to target if $t0 <> $t1

All the above targets are labels in the program


Jumps are used to unconditionally jump to a target.
!

j target # unconditional jump to program label target


jr $t3
# jump to address contained in $t3.

Version 1.0

Abelardo Lpez Lagunas

!14

Subroutine calls

The format is similar to a jump, but we need to store


the return address in a register (usually $ra).
# "jump and link"
jal sub_label
!

Before the jump, the contents of the program counter


is copied into register $ra and then the program
counter gets loaded with the address sub_label.
When we reach the end of the subroutine we issue the
instruction:
$ra
# "jump register
jr
That way we return to the instruction after the
subroutine call.

Version 1.0

Abelardo Lpez Lagunas

!15

SPIM System calls

The SPIM simulator provides some Operating Systemlike support services.


Service

Code in
$v0

Arguments

Result

print_int

$a0 = integer to be printed

Prints an integer into the output


console

print_float

$f12 = float to be printed

Prints a a float into the output


console

print_double 3

$f12 = double to be printed

Prints a a double into the output


console

print_string

$a0 = address of string in memory

Prints a a string into the output


console starting at address $a0

read_int

integer value is returned in $v0

read_float

float value is returned in $v0

read_double

double value is returned in $v0

read_string

$a0 = memory address of string


input buffer
$a1 = length of string buffer (n)

sbrk

$a0 = amount. Returns a block of


memory. Dynamic memory usage.

exit

10

Version 1.0

address of retuned block pointed by


$v0

Abelardo Lpez Lagunas

!16

SPIM System calls

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.

The sbrk service returns the address to a block of memory


containing n additional bytes. This would be used for dynamic
memory allocation.

The exit service stops a program from running.

Version 1.0

Abelardo Lpez Lagunas

!17

SPIM System calls


Print
#!Print!an!integer!stored!in!register!$t2!
li! $v0,!1!!!!!

#!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

Abelardo Lpez Lagunas

!18

SPIM System calls


Print a string
!

.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

Abelardo Lpez Lagunas

!19

MIPS Assembly Language


Class examples

Version 1.0

Abelardo Lpez Lagunas

!20

You might also like