Introduction to SPIM Simulator SPIM Simulator l l

  • Slides: 26
Download presentation
Introduction to SPIM Simulator

Introduction to SPIM Simulator

SPIM Simulator l l 2 SPIM is a software simulator that runs programs written

SPIM Simulator l l 2 SPIM is a software simulator that runs programs written for MIPS R 2000/R 3000 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

MIPS Processors l l 3 MIPS is a load-store architecture, which means that only

MIPS Processors l l 3 MIPS is a load-store architecture, which means that only load and store instructions access memory Computation instructions operate only on values in registers

MIPS Registers 4 Name $zero $at $v 0~$v 1 $a 0~$a 3 $t 0~$t

MIPS Registers 4 Name $zero $at $v 0~$v 1 $a 0~$a 3 $t 0~$t 7 $s 0~$s 7 $t 8~$t 9 $k 0~$k 1 $gp $sp $fp $ra Number 0 1 2~3 4~7 8~15 16~23 24~25 26~27 28 29 30 31 Usage constant 0 reserved for assembler return value of a function arguments temporary (not preserved across call) saved temporary (preserved across call) temporary (not preserved across call) reserved for OS kernel pointer to global area stack pointer frame pointer return address

Addressing Modes Format register imm(register) label 5 Address computation contents of register immediate contents

Addressing Modes Format register imm(register) label 5 Address computation contents of register immediate contents of (immediate + contents of register) address of label

Load, Store and Move Instructions li la lw sw move 6 rd, imm rd,

Load, Store and Move Instructions li la lw sw move 6 rd, imm rd, label rd, imm(rs) rd, rs rd imm rd label rd imm(rs) rd rd rs

Arithmetic and Logical Instructions 7 add sub mul div rem neg and or not

Arithmetic and Logical Instructions 7 add sub mul div rem neg and or not rd, rs, rt rd, rs, rt rd, rs rd rs + rt rd rs – rt rd rs * rt rd rs / rt rd rs % rt rd - rs rd rs & rt rd rs | rt rd ! rs

Branch and Jump Instructions 8 beq bne bgt bge blt ble b jal rs,

Branch and Jump Instructions 8 beq bne bgt bge blt ble b jal rs, rt, label rs, rt, label jr rs branch to label if rs == rt branch to label if rs != rt branch to label if rs >= rt branch to label if rs <= rt branch to label jump to label, save the next address in $ra jump to the address in rs

Assembler Syntax l l l 9 Comments in assembler files begin with a sharp

Assembler Syntax l l l 9 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

Assembler Syntax l l l 10 Labels are declared by putting them at the

Assembler Syntax l l l 10 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 0 x, they are interpreted as hexadecimal

Memory Layout 7 ffffff 10000000 400000 11 Stack segment Dynamic data Static data Data

Memory Layout 7 ffffff 10000000 400000 11 Stack segment Dynamic data Static data Data segment Text segment Reserved

Assembler Directives 12 . globl sym Declare that label sym is global. text <addr>

Assembler Directives 12 . 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, the items are stored starting at address addr. data <addr> Subsequent items are stored in the data segment. If the optional addr is present, the items are stored starting at address addr. word w 1, …, wn Store the n 32 -bit values in successive memory words. asciiz str Store the string str in memory and null-terminate it

Procedure Call Convention $fp 13 $sp … Argument 6 Argument 5 $a 3 $a

Procedure Call Convention $fp 13 $sp … Argument 6 Argument 5 $a 3 $a 2 $a 1 $a 0 $ra $fp Saved registers Local variables Higher address Lower address

Frame Size l l 14 The minimum frame size is 24 bytes. It can

Frame Size l l 14 The minimum frame size is 24 bytes. It can hold $a 0~$a 3, $ra, and $fp Frame size must be double word aligned

Caller l l l 15 Pass arguments. The first four arguments are passed in

Caller l l l 15 Pass arguments. The first four arguments are passed in $a 0~$a 3. The remaining arguments are passed in frame Save caller-saved registers $t 0~$t 9 Execute a jal instruction, which jumps to the callee’s first instruction and saves the return address in $ra

Entry of Callee l l l 16 Allocate the frame by subtracting the frame

Entry of Callee l l l 16 Allocate the frame by subtracting the frame size from the stack pointer Save callee-saved registers. $s 0~$s 7, $fp, and $ra Establish the frame pointer by adding the frame size minus four to $sp and storing the sum in $fp

Exit of Callee l l 17 If the callee is a function that returns

Exit of Callee l l 17 If the callee is a function that returns a value, place the returned value in $v 0 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

An Example int main() { int m; m = fact(10); } 18 int fact(int

An Example int main() { int m; m = fact(10); } 18 int fact(int n) { int m; if (n <= 1) return 1; else { m = fact(n – 1); return (n * m); } }

An Example 28 24 20 16 12 8 4 $sp 19 $a 3 $a

An Example 28 24 20 16 12 8 4 $sp 19 $a 3 $a 2 $a 1 $a 0 $ra $fp m $fp -4 -8 -12 -16 -20 -24 -28

An Example. text. globl main: li sub sw sw li add li jal 20

An Example. text. globl main: li sub sw sw li add li jal 20 $t 0, 32 $sp, $t 0 $ra, 12($sp) $fp, 8($sp) $t 0, 28 $fp, $sp, $t 0 $a 0, 10 fact: lw $ra, 12($sp) lw li add jr $fp, 8($sp) $t 0, 32 $sp, $t 0 $ra . text li sub sw sw li add $t 0, 32 $sp, $t 0 $ra, 12($sp) $fp, 8($sp) $t 0, 28 $fp, $sp, $t 0

An Example L 1: 21 sw lw bgt li b lw $a 0, -12($fp)

An Example L 1: 21 sw lw bgt li b lw $a 0, -12($fp) $t 0, $zero, L 1 $v 0, 1 L 2 $t 0, -12($fp) li $t 1, 1 sub move jal sw $t 0, $t 1 $a 0, $t 0 fact $v 0, -24($fp) L 2: lw lw mul move lw lw li add jr $t 0, -12($fp) $t 1, -24($fp) $t 0, $t 1 $v 0, $t 0 $ra, 12($sp) $fp, 8($sp) $t 0, 32 $sp, $t 0 $ra

System Calls l l l 22 SPIM provides a small set of operating systemlike

System Calls l l l 22 SPIM provides a small set of operating systemlike services through the system call (syscall) instruction To request a service, a program loads the system call code into register $v 0 and arguments into registers $a 0~$a 3 System calls that return values put their results in register $v 0

System Call Code Service System call code Arguments Result print_int $v 0=1 $a 0=interger

System Call Code Service System call code Arguments Result print_int $v 0=1 $a 0=interger print_string $v 0=4 $a 0=string read_int $v 0=5 integer in $v 0 exit $v 0=10 23

An Example. data str: 24 “the answer = 5” . asciiz “the answer =

An Example. data str: 24 “the answer = 5” . asciiz “the answer = ”. text la $a 0, str li $v 0, 4 syscall li $a 0, 5 li $v 0, 1 syscall

An Example int main() { int m; print_string(“The factorial of 10 is ”); m

An Example int main() { int m; print_string(“The factorial of 10 is ”); m = fact(10); print_int(m); } 25

An Example. text. globl main: li $t 0, 32 sub $sp, $t 0 sw

An Example. text. globl main: li $t 0, 32 sub $sp, $t 0 sw $ra, 12($sp) sw $fp, 8($sp) li $t 0, 28 add $fp, $sp, $t 0 la $a 0, LC li $v 0, 4 syscall li $a 0, 10 26 jal fact sw $v 0, -24($fp) lw $a 0, -24($fp) li $v 0, 1 syscall lw $ra, 12($sp) lw $fp, 8($sp) li $t 0, 32 add $sp, $t 0 jr $ra LC: . data. asciiz “The factorial of 10 is ”