160 likes | 210 Views
The SPIM Simulator. SPIM is a simulator that let you run and debug MIPS assembler programs. The simulator allows you to look at the content of registers and memory, and to single step through the simulation. Install PCSpim http://www.cs.wisc.edu/~larus/spim.html Documentation
E N D
The SPIM Simulator • SPIM is a simulator that let you run and debug MIPS assembler programs. • The simulator allows you to look at the content of registers and memory, and to single step through the simulation. • Install PCSpimhttp://www.cs.wisc.edu/~larus/spim.html • Documentation • book: appendix A9 (on CD) • www.cs.wisc.edu/~larus/SPIM/spim_documentation.pdf • Webinterface: http://cgi.aggregate.org/cgi-bin/cgispim.cgi
Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o Linker lib.o Executable(mach lang pgm): a.out Loader Memory Steps to Starting a Program (translation) C program: foo.c
Assembly Language (cont.) • Pseudo-instructions: extending the instruction set for convenience. • Examples: • move $2, $4# $2 = $4, (copy $4 to $2)Translates to:add $2, $4, $0 • li $8, 40# $8 = 40, (load 40 into $8)addi $8, $0, 40 • sd $4, 0($29)# mem[$29] = $4; Mem[$29+4] = $5sw $4, 0 ($29)sw $5, 4($29) • la $4, 0x1000056c # Load address $4 = <address>lui $4, 0x1000ori $4, $4, 0x056c
MIPS Pseudo-instructions Copy Arithmetic Shift Logic Memory access Control transfer
op 15 0 0 0 8 10 0 0 0 0 12 13 14 35 43 2 0 1 4 5 3 0 fn 32 34 42 36 37 38 39 8 12 Copy The MIPS Instruction Set Arithmetic Logic Memory access Control transfer
Recalling Register Conventions Registers and data sizes in MIPS.
SPIM/MIPS assembly directives .data start data segment .ascii "str" store the string "str" in memory without '\0' .asciiz "str" idem, with '\0' .byte 3,4,16 store 3 byte values .double 3.14, 2.72 store 2 doubles .float 3.14, 2.72 store 2 floats .word 3,4,16 store 3 32-bit quantities .space 100 reserve 100 bytes .text start text segment
Let’s try .text .globl main main: ori $t0, $0, 0x2 # $8 OR(0, 0x2) ori $t1, $0, 0x3 # $9 OR(0, 0x3) addu $t2, $t0, $t1 # $10 ADD($t0, $t1)
Memory Map in MIPS 80000000 Overview of the memory address space in MIPS.
.data n: .word 0x2 m: .word 0x3 r: .space 4 .text .globl main main: la $t5, n # load address of n to $t5 lw $t0, 0($t5) # load n to $t0 la $t5, m # load address of m to $t5 lw $t1, 0($t5) # load m to $t1 addu $t2, $t0, $t1 # $10 ADD($8, $9) la $t5, r # load address of r to $t5 sw $t2, 0($t5) # store $10 to r
.data n: .word 0x2 m: .word 0x3 r: .space 4 .text .globl main main: lw $t0, n # load n to $t0 lw $t1, m # load m to $t1 addu $t2, $t0, $t1 # $10 ADD($8, $9) sw $t2, r # store $10 to r
System calls – print_str .data str: .asciiz “Hello World” .text .globl main main: li $v0, 4 # code for print_str la $a0, str # argument syscall # executes print_str
System calls – read _int .data num: .space 4 .text .globl main main: li $v0, 5 # code for read_int syscall # executes read_int # return value is stored in $v0 la $t0, num # load address of num to $t0 sw $v0, 0($t0) # store the number in num
SPIM example 1: add two numbers # $t2 - used to hold the sum of the $t0 and $t1. # $v0 - syscall number, and syscall return value. # $a0 - syscall input parameter. .text # Code area starts here main: li $v0, 5 # read number into $v0 syscall # make the syscall read_int move $t0, $v0 # move the number read into $t0 li $v0, 5 # read second number into $v0 syscall # make the syscall read_int move $t1, $v0 # move the number read into $t1 add $t2, $t0, $t1 move $a0, $t2 # move the number to print into $a0 li $v0, 1 # load syscall print_int into $v0 syscall # li $v0, 10 # syscall code 10 is for exit syscall # # end of main Assembler directive starts with a dot Special SPIM instruction: system call