Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SPIM Simulator

Similar presentations

Presentation on theme: "Introduction to SPIM Simulator"— Presentation transcript:

1 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 reserved for assembler $v0~$v1 2~3 return value of a function $a0~$a3 4~7 arguments $t0~$t7 8~ temporary (not preserved across call) $s0~$s7 16~ saved temporary (preserved across call) $t8~$t9 24~ temporary (not preserved across call) $k0~$k1 26~ reserved for OS kernel $gp pointer to global area $sp stack pointer $fp frame pointer $ra return address

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 Load, Store and Move Instructions
li rd, imm rd  imm la rd, label rd  label lw rd, imm(rs) rd  imm(rs) sw rd, imm(rs) imm(rs)  rd move rd, rs rd  rs

7 Arithmetic Instructions
add rd, rs, rt rd  rs + rt sub rd, rs, rt rd  rs – rt mul rd, rs, rt rd  rs * rt div rd, rs, rt rd  rs / rt rem rd, rs, rt rd  rs % rt neg rd, rs rd  - rs

8 Branch Instructions beq rs, rt, label branch to label if rs == rt
bne rs, rt, label branch to label if rs != rt bgt rs, rt, label branch to label if rs > rt bge rs, rt, label branch to label if rs >= rt blt rs, rt, label branch to label if rs < rt ble rs, rt, label branch to label if rs <= rt b label branch to label

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 Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal

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

12 Assembler Directives .text Subsequent items are put in the user text
segment. These items may only be instructions. .data Subsequent items are stored in the data segment. .word n Store the 32-bit value n in current memory word

13 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

14 System Call Code Service Code Arguments Result
print_int $v0= $a0=interger read_int $v0= integer in $v0 exit $v0=10

15 An Example procedure SUM is declare
N: integer; S: integer; I: integer; begin read N; if N < 0 then write -1; exit; else S := 0; end if; for I in 1..N loop S := S + I; end loop; write S; end;

16 An Example .data N: .word 0 S: I: .text main: li $v0, 5 syscall
la $t0, N sw $v0, 0($t0) la $t0, n lw $t0, 0($t0) li $t1, 0 blt $t0, $t1, L1 b L2 L1: li $t0, 1 neg $t0, $t0 move $a0, $t0 li $v0, 1 syscall

17 An Example li $v0, 10 syscall b L3 L2: li $t0, 0 la $t1, S
sw $t0, 0($t1) L3: li $t0, la $t1, I la $t0, N lw $t0, 0($t0) L4: la $t1, I lw $t1, 0($t1) ble $t1, $t0, L5 b L6 L5: la $t1, S la $t2, I lw $t2, 0($t2) add $t1, $t1, $t2

18 An Example move $a0, $t0 la $t2, S li $v0, 1 sw $t1, 0($t2) syscall
la $t1, I lw $t1, 0($t1) li $t2, 1 add $t1, $t1, $t2 la $t2, I sw $t1, 0($t2) b L4 L6: la $t0, S lw $t0, 0($t0) move $a0, $t0 li $v0, 1 syscall

Download ppt "Introduction to SPIM Simulator"

Similar presentations

Ads by Google