240 likes | 470 Views
SPIM part I. Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU. Outline. Introduction SPIM Interface Simulator Usage MIPS R2000 Assembler Language Program Example. Introduction. SPIM A simulator that runs programs for the MIPS R2000/R3000 RISC computers
E N D
SPIMpart I Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU
Outline • Introduction • SPIM Interface • Simulator Usage • MIPS R2000 Assembler Language • Program Example
Introduction • SPIM • A simulator that runs programs for the MIPS R2000/R3000 RISC computers • Developed by James R. Larus, Computer Sciences Department, University of Wisconsin-Madison • Detail information can be found in Appendix A in the textbook • Functions of SPIM • Reads and executes MIPS assembly language file immediately • Works as a debugger • Provides some OS like services
Installation • Download • http://www2.ee.ntu.edu.tw/~harold/ca/spim.html • Windows Version • Extract the zipped file to a new folder • Execute setup.exe and follow the installation procedure • Unix Version • Get spim-6.3.tar.gz • Move it to a directory • Unzip it -- gzip -d spim6.3.tar.gz • Untar it -- tar xvf spim6.3.tar
Installation (cont ) • Configure • Edit Imakefile, modify RAP_DIR, BIN_DIR and MAN_DIR • make • make xspim • Type “xspim” to execute it • For more information, read the README in source files • For RedHat Linux only • Get spim-6.3-1.i386.rpm • Login Linux with root permission • rpm -i spim-6.3-1.i386.rpm • If some packages needed by SPIM are not found, install them from your RedHat distribution
Windows Interface • Registers window • shows the values of all registers in the MIPS CPU and FPU • Text segment window • shows instructions • Data segment window • shows the data loaded into the program’s memory and the data of the program’s stack • Messages window • shows PCSpim messages (include error messages) Registers Text Segment Data Segment Messages
Simulator Usage • Opening source file • Use File menu or toolbar button ( File Open (*.s) ) • Simulation • Go: Run loaded program • Break / Continue • Single / Multiple Step: Stepping for debugging • Breakpoint: Stop program before it executes a particular instruction • Reload: Reload source file after change it with editor program • Reinitialize / Clear Registers
MIPS Assembly Layout • Program Layout .text #code section .globl main #starting point: must be global main: # user program code .data #data section label: .data_type list_of_data #data loc + data type + data .text #code section label: #function label #user functions
MIPS Assembler Directives (1/2) • Data Types .word, .half - 32/16 bit integer .byte - 8 bit integer (similar to ‘char’ type in C) .ascii, .asciiz - string (asciiz is null terminated) • Strings are enclosed in double-quotas(”) • Special characters in strings follow the C convention • newline(\n), tab(\t), quote(\”) .double, .float - floating point
MIPS Assembler Directives (2/2) • Other Directives • .text - Indicates that following items are stored in the user text segment • .data - Indicates that following data items are stored in the data segment • .globl sym- Declare that symbol sym is global and can be referenced from other files
SPIM Program Example I • A Simple Program #sample example 'add two numbers’ .text # text section .globl main # call main by SPIM main: la $t0, value # load address ‘value’ into $t0 lw $t1, 0($t0) # load word 0(value) into $t1 lw $t2, 4($t0) # load word 4(value) into $t2 add $t3, $t1, $t2 # add two numbers into $t3 sw $t3, 8($t0) # store word $t3 into 8($t0) .data # data section value: .word 10, 20, 0 # data for addition
Example Processes (1/5) • Simulator Clear Registers • Simulator Reinitialize • Open add.s • Simulator Go or Simulator Single Step
Example Processes (2/5) Registers ========================= PC = 00000000 EPC = 00000000 Cause = 00000000 BadVAddr= 00000000 Status = 00000000 HI = 00000000 LO = 00000000 General Registers R0 (r0) = 0 R8 (t0) = 268500992 R16 (s0) = 0 R24 (t8) = 0 R1 (at) = 268500992 R9 (t1) = 10 R17 (s1) = 0 R25 (t9) = 0 R2 (v0) = 0 R10 (t2) = 20 R18 (s2) = 0 R26 (k0) = 0 R3 (v1) = 0 R11 (t3) = 30 R19 (s3) = 0 R27 (k1) = 0 R4 (a0) = 0 R12 (t4) = 0 R20 (s4) = 0 R28 (gp) = 268468224 R5 (a1) = 2147478268 R13 (t5) = 0 R21 (s5) = 0 R29 (sp) = 2147478264 R6 (a2) = 2147478272 R14 (t6) = 0 R22 (s6) = 0 R30 (s8) = 0 R7 (a3) = 0 R15 (t7) = 0 R23 (s7) = 0 R31 (ra) = 4194328 Double Floating Point Registers … Single Floating Point Registers …
Example Processes (3/5) Text Segment ========================= [0x00400000] 0x8fa40000 lw $4, 0($29) ; 102: lw $a0, 0($sp) # argc [0x00400004] 0x27a50004 addiu $5, $29, 4 ; 103: addiu $a1, $sp, 4 # argv [0x00400008] 0x24a60004 addiu $6, $5, 4 ; 104: addiu $a2, $a1, 4 # envp [0x0040000c] 0x00041080 sll $2, $4, 2 ; 105: sll $v0, $a0, 2 addu $a2, $a2, $v0 [0x00400010] 0x00c23021 addu $6, $6, $2 ; 106: addu $a2, $a2, $v0 jal main [0x00400014] 0x0c100008 jal 0x00400020 [main] ; 107: jal main li $v0 10 [0x00400018] 0x3402000a ori $2, $0, 10 ; 108: li $v0 10 [0x0040001c] 0x0000000c syscall ; 109: syscall # syscall 10 (exit) [0x00400020] 0x3c011001 lui $1, 4097 [value] ; 6: la $t0, value # load address ‘value’ into $t0 [0x00400024] 0x34280000 ori $8, $1, 0 [value] [0x00400028] 0x8d090000 lw $9, 0($8) ; 7: lw $t1, 0($t0) # load word 0(value) into $t1 [0x0040002c] 0x8d0a0004 lw $10, 4($8) ; 8: lw $t2, 4($t0) # load word 4(value) into $t2 [0x00400030] 0x012a5820 add $11, $9, $10 ; 9: add $t3, $t1, $t2 # add two numbers into $t3 [0x00400034] 0xad0b0008 sw $11, 8($8) ; 10: sw $t3, 8($t0) # store word $t3 into 8($t0)
Example Processes (4/5) Data Segment ========================= DATA [0x10000000]...[0x1000fffc] 0x00000000 [0x1000fffc] 0x00000000 [0x10010000] 0x0000000a 0x00000014 0x0000001e 0x00000000 [0x10010010]...[0x10040000] 0x00000000 STACK [0x7fffeaf8] 0x00000000 0x00000000 [0x7fffeb00] 0x7fffefe9 0x7fffefd4 0x7fffefc8 0x7fffefb2 … [0x7fffeff0] 0x444e4957 0x5c53574f 0x504d4554 0x00000000 KERNEL DATA [0x90000000] 0x78452020 0x74706563 0x206e6f69 0x636f2000 … [0x900001f0] 0x90000173 0x90000190 0x00000000 0x00000000 [0x90000200]...[0x90010000] 0x00000000
Example Processes (5/5) • File Save log file ( PCSpim.log )
SPIM System Calls • System Calls (syscall) • OS-like services • Method • Load system call code into register $v0 • Load arguments into registers $a0…$a3 • After call, return value is in register $v0 • Frequently used system calls
SPIM Program Example II • A Program with System Call #sample example 'system call' .text .globl main main: la $t0, value li $v0, 5 syscall sw $v0, 0($t0) li $v0, 5 syscall sw $v0, 4($t0) lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4 la $a0, msg1 syscall li $v0, 1 move $a0, $t3 syscall .data value: .word 0, 0, 0 msg1: .asciiz "Result = "
SPIM Program Example III • A Program with Procedure Call # sample example ‘swap two numbers’ .text .globl main main: la $a0, array addi $a1, $0, 0 addi $sp, $sp, -4 sw $ra, 0($sp) jal swap lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra .data array: .word 5, 4, 3, 2, 1 .text # swap(int v[], int k) # { # int temp; # temp = v[k]; # v[k] = v[k+1]; # v[k+1] = temp; # } swap: add $t1, $a1, $a1 add $t1, $t1, $t1 add $t1, $a0, $t1 lw $t0, 0($t1) lw $t2, 4($t1) sw $t2, 0($t1) sw $t0, 4($t1) jr $ra
References • http://www.cnb.uam.es/~coss/Ceu/Material/02-Ceu-SPIM.ppt • J. R. Larus, SPIM S20: A MIPS R2000 Simulator “1/25 the performance at none of the cost”, Computer Sciences Department, University of Wisconsin-Madison, USA, 1990-1997.