introduction to spim simulator. 2 spim simulator spim is a software simulator that runs programs...

Post on 01-Apr-2015

235 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to SPIM Simulator

2

SPIM Simulator

SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors

SPIM’s name is just MIPS spelled backwards SPIM can read and immediately execute MIPS

assembly language files or MIPS executable files

SPIM contains a debugger and provides a few operating system-like services

3

MIPS Processors

MIPS is a load-store architecture, which means that only load and store instructions access memory

Computation instructions operate only on values in registers

4

MIPS Registers

Name Number Usage$zero 0 constant 0$at 1 reserved for assembler$v0~$v1 2~3 return value of a function$a0~$a3 4~7 arguments$t0~$t7 8~15 temporary (not preserved across call)$s0~$s7 16~23 saved temporary (preserved across call)$t8~$t9 24~25 temporary (not preserved across call)$k0~$k1 26~27 reserved for OS kernel$gp 28 pointer to global area$sp 29 stack pointer$fp 30 frame pointer$ra 31 return address

5

Addressing Modes

Format Address computationregister contents of registerimm immediateimm(register) contents of (immediate + contents of register)label address of label

6

Load, Store and Move Instructions

li rd, imm rd immla rd, label rd labellw rd, imm(rs) rd imm(rs)sw rd, imm(rs) imm(rs) rdmove rd, rs rd rs

7

Arithmetic and Logical Instructions

add rd, rs, rt rd rs + rtsub rd, rs, rt rd rs – rtmul rd, rs, rt rd rs * rtdiv rd, rs, rt rd rs / rtrem rd, rs, rt rd rs % rtneg rd, rs rd - rsand rd, rs, rt rd rs & rtor rd, rs, rt rd rs | rtnot rd, rs rd ! rs

8

Branch and Jump Instructions

beq rs, rt, label branch to label if rs == rtbne rs, rt, label branch to label if rs != rtbgt rs, rt, label branch to label if rs > rtbge rs, rt, label branch to label if rs >= rtblt rs, rt, label branch to label if rs < rtble rs, rt, label branch to label if rs <= rtb label branch to label jal label jump to label,

save the next address in $rajr rs jump to the address in rs

9

Assembler Syntax

Comments in assembler files begin with a sharp sign (#) and continue to the end of the line

Identifiers are a sequence of alphanumeric characters, underbars (_), and dots (.) that do not begin with a number

Opcodes are reserved words that cannot be used as identifiers

10

Assembler Syntax

Labels are declared by putting them at the beginning of a line followed by a colon

Strings are enclosed in doublequotes (“). Special characters in strings: newline \n, tab \t, quote \“

Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal

11

Memory Layout

7ffffff

10000000

400000Reserved

Text segment

Data segment

Stack segment

Static data

Dynamic data

12

Assembler Directives

.globl sym Declare that label sym is global

.text <addr> Subsequent items are put in the user text segment. These items may only be instructions. If the optional addr is present, theitems are stored starting at address addr

.data <addr> Subsequent items are stored in the data segment. If the optional addr is present, theitems are stored starting at address addr

.word w1, …, wn Store the n 32-bit values in successive memory words

.asciiz str Store the string str in memory and null-terminate it

13

Procedure Call Convention

…Argument 6Argument 5

$a3$a2$a1$a0$ra$fp

Saved registersLocal variables

$fp

$sp

Higher address

Lower address

14

Frame Size

The minimum frame size is 24 bytes. It can hold $a0~$a3, $ra, and $fp

Frame size must be double word aligned

15

Caller

Pass arguments. The first four arguments are passed in $a0~$a3. The remaining arguments are passed in frame

Save caller-saved registers $t0~$t9 Execute a jal instruction, which jumps to the cal

lee’s first instruction and saves the return address in $ra

16

Entry of Callee

Allocate the frame by subtracting the frame size from the stack pointer

Save callee-saved registers. $s0~$s7, $fp, and $ra

Establish the frame pointer by adding the frame size minus four to $sp and storing the sum in $fp

17

Exit of Callee

If the callee is a function that returns a value, place the returned value in $v0

Restore all callee-saved registers that were saved upon procedure entry

Pop the stack frame by adding the frame size to $sp

Return by jumping to the address in $ra

18

An Example

int main(){ int m; m = fact(10);}

int fact(int n){ int m; if (n <= 1) return 1; else { m = fact(n – 1); return (n * m); }}

19

An Example

$a3$a2$a1$a0$ra$fpm

$fp

$sp48

12

-4-8

-12-16

-20-24-28

16202428

20

An Example

.text

.globl mainmain:

li $t0, 32sub $sp, $sp, $t0sw $ra, 12($sp)sw $fp, 8($sp)li $t0, 28add $fp, $sp, $t0li $a0, 10jal fact

lw $ra, 12($sp) lw $fp, 8($sp)li $t0, 32add $sp, $sp, $t0jr $ra

.textfact: li $t0, 32

sub $sp, $sp, $t0sw $ra, 12($sp)sw $fp, 8($sp)li $t0, 28add $fp, $sp, $t0

21

An Example

sw $a0, -12($fp)lw $t0, -12($fp)bgt $t0, $zero, L1li $v0, 1b L2

L1: lw $t0, -12($fp)li $t1, 1sub $t0, $t0, $t1move $a0, $t0jal factsw $v0, -24($fp)

lw $t0, -12($fp)lw $t1, -24($fp)mul $t0, $t0, $t1move $v0, $t0

L2: lw $ra, 12($sp) lw $fp, 8($sp)li $t0, 32add $sp, $sp, $t0jr $ra

22

System Calls

SPIM provides a small set of operating system-like services through the system call (syscall) instruction

To request a service, a program loads the system call code into register $v0 and arguments into registers $a0~$a3

System calls that return values put their results in register $v0

23

System Call Code

Service System call code Arguments Resultprint_int $v0=1 $a0=intergerprint_string $v0=4 $a0=stringread_int $v0=5 integer in $v0exit $v0=10

24

An Example

.datastr:

.asciiz “the answer = ”

.textla $a0, strli $v0, 4syscallli $a0, 5li $v0, 1syscall

“the answer = 5”

25

An Example

int main(){ int m;

print_string(“The factorial of 10 is ”); m = fact(10); print_int(m);}

26

An Example

.text

.globl mainmain: li $t0, 32

sub $sp, $sp, $t0sw $ra, 12($sp)sw $fp, 8($sp)li $t0, 28add $fp, $sp, $t0la $a0, LCli $v0, 4syscallli $a0, 10jal fact

sw $v0, -24($fp)lw $a0, -24($fp)li $v0, 1syscalllw $ra, 12($sp) lw $fp, 8($sp)li $t0, 32add $sp, $sp, $t0jr $ra

.data LC: .asciiz “The factorial of

10 is ”

top related