70 likes | 284 Views
ECE 0142. Recitation #5. SPIM Simulator. Official website: http://pages.cs.wisc.edu/~larus/spim.html Versions Unix, Linux, Mac: spim , xspim Windows: PCSpim. Register Pane. Memory address of instructions. Instructions. Correspondent line in source file. Program Pane.
E N D
ECE 0142 Recitation #5
SPIM Simulator • Official website: • http://pages.cs.wisc.edu/~larus/spim.html • Versions • Unix, Linux, Mac: spim, xspim • Windows: PCSpim
Register Pane Memory address of instructions Instructions Correspondent line in source file Program Pane Encoded instruction Memory & stack Program input/output Message Pane
SPIM Basics • Must have a label “main” – entrance of your program • Data segment • Starts with “.data” directive • Memory locations, constants, string literals • Text segment • Your program
System Calls • Simple I/O functions in your program • Example • print_int: print an integer on console • print_string: print a zero-ended string on console • read_int • ...... • How to use? • Load $v0 with service number (e.g. 1 for print_int) • Load arguments (if any) to $a0, $a1 or $f12 • syscall
Example: Swap Words .data value1: .word 1111 value2: .word 2222 msg_v1: .asciiz "v1=" msg_v2: .asciiz "v2=" newline: .asciiz "\n" .text .globl main main: # Load value1, value2 into $s0, $s1 la $t0, value1 lw $s0, 0($t0) la $t1, value2 lw $s1, 0($t1) # swap $s0, $s1 move $s2, $s0 move $s0, $s1 move $s1, $s2 # Save $s0, $s1 back to value1, value2 sw $s0, 0($t0) sw $s1, 0($t1)
Example (cont.) # print value1 li $v0, 4 la $a0, msg_v1 syscall li $v0, 1 la $t0, value1 lw $a0, 0($t0) syscall li $v0, 4 la $a0, newline syscall # print value2 li $v0, 4 la $a0, msg_v2 syscall li $v0, 1 la $t0, value2 lw $a0, 0($t0) syscall li $v0, 4 la $a0, newline syscall # exit li $v0, 10 syscall