1 / 26

Introduction to SPIM Simulator

Introduction to SPIM Simulator. 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

brad
Download Presentation

Introduction to SPIM Simulator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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 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 and Logical 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  - rsand rd, rs, rt rd  rs & rt or rd, rs, rt rd  rs | rt not rd, rs rd  ! rs

  8. Branch and Jump 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 jal label jump to label, save the next address in $ra jr 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 Stack segment Dynamic data Data segment Static data 10000000 Text segment 400000 Reserved

  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, 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 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 Higher address … Argument 6 Argument 5 $a3 $a2 $a1 $a0 $ra $fp Saved registers Local variables $fp $sp 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 callee’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 $fp m 28 $fp 24 -4 -8 20 -12 16 -16 12 -20 8 -24 4 -28 $sp

  20. An Example 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 .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 jal fact

  21. An Example sw $a0, -12($fp) lw $t0, -12($fp) bgt $t0, $zero, L1 li $v0, 1 b L2 L1: lw $t0, -12($fp) li $t1, 1 sub $t0, $t0, $t1 move $a0, $t0 jal fact 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. 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 Result print_int $v0=1 $a0=interger print_string $v0=4 $a0=string read_int $v0=5 integer in $v0 exit $v0=10

  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. An Example int main() { int m; print_string(“The factorial of 10 is ”); m = fact(10); print_int(m); }

  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 jal fact 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 ”

More Related