Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SPIM Simulator. 2 SPIM Simulator SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors SPIM ’ s name.

Similar presentations


Presentation on theme: "Introduction to SPIM Simulator. 2 SPIM Simulator SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors SPIM ’ s name."— Presentation transcript:

1 Introduction to SPIM Simulator

2 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 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 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~$s716~23 saved temporary (preserved across call) $t8~$t924~25 temporary (not preserved across call) $k0~$k126~27 reserved for OS kernel $gp 28 pointer to global area $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address

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

6 6 Load, Store and Move Instructions lird, immrd  imm lard, labelrd  label lwrd, imm(rs) rd  imm(rs) swrd, imm(rs) imm(rs)  rd moverd, rsrd  rs

7 7 Arithmetic and Logical Instructions addrd, rs, rtrd  rs + rt subrd, rs, rtrd  rs – rt mulrd, rs, rtrd  rs * rt divrd, rs, rtrd  rs / rt remrd, rs, rtrd  rs % rt negrd, rsrd  - rs andrd, rs, rtrd  rs & rt orrd, rs, rtrd  rs | rt notrd, rsrd  ! rs

8 8 Branch and Jump Instructions beqrs, rt, labelbranch to label if rs == rt bners, rt, labelbranch to label if rs != rt bgtrs, rt, labelbranch to label if rs > rt bgers, rt, labelbranch to label if rs >= rt bltrs, rt, labelbranch to label if rs < rt blers, rt, labelbranch to label if rs <= rt blabelbranch to label jallabeljump to label, save the next address in $ra jrrsjump to the address in rs

9 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 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 11 Memory Layout 7ffffff 10000000 400000 Reserved Text segment Data segment Stack segment Static data Dynamic data

12 12 Assembler Directives.globl symDeclare that label sym is global.text 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 Subsequent items are stored in the data segment. If the optional addr is present, the items are stored starting at address addr.word w1, …, wnStore the n 32-bit values in successive memory words.asciiz strStore the string str in memory and null-terminate it

13 13 Procedure Call Convention … Argument 6 Argument 5 $a3 $a2 $a1 $a0 $ra $fp Saved registers Local variables $fp $sp Higher address Lower address

14 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 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 callee ’ s first instruction and saves the return address in $ra

16 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 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 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 19 An Example $a3 $a2 $a1 $a0 $ra $fp m $fp $sp 4 8 12 -4 -8 -12 -16 -20 -24 -28 16 20 24 28

20 20 An Example.text.globl main main: li$t0, 32 sub$sp, $sp, $t0 sw$ra, 12($sp) sw$fp, 8($sp) li$t0, 28 add$fp, $sp, $t0 li$a0, 10 jalfact lw $ra, 12($sp) lw$fp, 8($sp) li$t0, 32 add$sp, $sp, $t0 jr$ra.text fact:li$t0, 32 sub$sp, $sp, $t0 sw$ra, 12($sp) sw$fp, 8($sp) li$t0, 28 add$fp, $sp, $t0

21 21 An Example sw$a0, -12($fp) lw$t0, -12($fp) bgt$t0, $zero, L1 li$v0, 1 bL2 L1:lw$t0, -12($fp) li$t1, 1 sub$t0, $t0, $t1 move$a0, $t0 jalfact sw$v0, -24($fp) lw$t0, -12($fp) lw$t1, -24($fp) mul$t0, $t0, $t1 move$v0, $t0 L2:lw $ra, 12($sp) lw$fp, 8($sp) li$t0, 32 add$sp, $sp, $t0 jr$ra

22 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 23 System Call Code Service System call code Arguments Result print_int $v0=1 $a0=interger print_string $v0=4 $a0=string read_int $v0=5 integer in $v0 exit $v0=10

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

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

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


Download ppt "Introduction to SPIM Simulator. 2 SPIM Simulator SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors SPIM ’ s name."

Similar presentations


Ads by Google