{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

spim-manual - SPIM S20 A MIPS R2000 Simulator 1 25 th the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: SPIM S20: A MIPS R2000 Simulator 1 \ 25 th the performance at none of the cost" James R. Larus [email protected] Computer Sciences Department University of Wisconsin{Madison 1210 West Dayton Street Madison, WI 53706, USA 608-262-9519 Copyright c 1990{1997 by James R. Larus (This document may be copied without royalties, so long as this copyright notice remains on it.) 1 SPIM SPIM S20 is a simulator that runs programs for the MIPS R2000/R3000 RISC computers.1 SPIM can read and immediately execute les containing assembly language. SPIM is a selfcontained system for running these programs and contains a debugger and interface to a few operating system services. The architecture of the MIPS computers is simple and regular, which makes it easy to learn and understand. The processor contains 32 general-purpose registers and a well-designed instruction set that make it a propitious target for generating code in a compiler. However, the obvious question is: why use a simulator when many people have workstations that contain a hardware, and hence signi cantly faster, implementation of this computer? One reason is that these workstations are not generally available. Another reason is that these machine will not persist for many years because of the rapid progress leading to new and faster computers. Unfortunately, the trend is to make computers faster by executing several instructions concurrently, which makes their architecture more di cult to understand and program. The MIPS architecture may be the epitome of a simple, clean RISC machine. In addition, simulators can provide a better environment for low-level programming than an actual machine because they can detect more errors and provide more features than an actual computer. For example, SPIM has a X-window interface that is better than most debuggers for the actual machines. I grateful to the many students at UW who used SPIM in their courses and happily found bugs in a professor's code. In particular, the students in CS536, Spring 1990, painfully found the last few bugs in an \already-debugged" simulator. I am grateful for their patience and persistence. Alan Yuen-wui Siow wrote the X-window interface. 1 For a description of the real machines, see Gerry Kane and Joe Heinrich, MIPS RISC Architecture, Prentice Hall, 1992. 1 Finally, simulators are an useful tool for studying computers and the programs that run on them. Because they are implemented in software, not silicon, they can be easily modi ed to add new instructions, build new systems such as multiprocessors, or simply to collect data. The MIPS architecture, like that of most RISC computers, is di cult to program directly because of its delayed branches, delayed loads, and restricted address modes. This di culty is tolerable since these computers were designed to be programmed in high-level languages and so present an interface designed for compilers, not programmers. A good part of the complexity results from delayed instructions. A delayed branch takes two cycles to execute. In the second cycle, the instruction immediately following the branch executes. This instruction can perform useful work that normally would have been done before the branch or it can be a nop (no operation). Similarly, delayed loads take two cycles so the instruction immediately following a load cannot use the value loaded from memory. MIPS wisely choose to hide this complexity by implementing a virtual machine with their assembler. This virtual computer appears to have non-delayed branches and loads and a richer instruction set than the actual hardware. The assembler reorganizes (rearranges) instructions to ll the delay slots. It also simulates the additional, pseudoinstructions by generating short sequences of actual instructions. By default, SPIM simulates the richer, virtual machine. It can also simulate the actual hardware. We will describe the virtual machine and only mention in passing features that do not belong to the actual hardware. In doing so, we are following the convention of MIPS assembly language programmers (and compilers), who routinely take advantage of the extended machine. Instructions marked with a dagger (y) are pseudoinstructions. 1.1 Simulation of a Virtual Machine 1.2 SPIM Interface SPIM provides a simple terminal and a X-window interface. Both provide equivalent functionality, but the X interface is generally easier to use and more informative. spim, the terminal version, and xspim, the X version, have the following command-line options: -bare Simulate a bare MIPS machine without pseudoinstructions or the additional addressing modes provided by the assembler. Implies -quiet. -asm Simulate the virtual MIPS machine provided by the assembler. This is the default. Accept pseudoinstructions in assembly code. Do not accept pseudoinstructions in assembly code. Do not load the standard trap handler. This trap handler has two functions that must be assumed by the user's program. First, it handles traps. When a trap occurs, SPIM jumps to location 0x80000080, which should contain code to service the exception. Second, 2 -pseudo -nopseudo -notrap this le contains startup code that invokes the routine main. Without the trap handler, execution begins at the instruction labeled start. -trap Load the standard trap handler. This is the default. Load the trap handler in the le. Print a message when an exception occurs. This is the default. Do not print a message at an exception. Disable the memory-mapped IO facility (see Section 5). -trap file -noquiet -quiet -nomapped io -mapped io Enable the memory-mapped IO facility (see Section 5). Programs that use SPIM syscalls (see Section 1.5) to read from the terminal should not also use memory-mapped IO. Load and execute the assembly code in the le. -s seg size Sets the initial size of memory segment seg to be size bytes. The memory segments are named: text, data, stack, ktext, and kdata. For example, the pair of arguments -sdata 2000000 starts the user data segment at 2,000,000 bytes. -lseg size Sets the limit on how large memory segment seg can grow to be size bytes. The memory segments that can grow are: data, stack, and kdata. -file 1.2.1 Terminal Interface The terminal interface (spim) provides the following commands: exit Exit the simulator. Read le of assembly language commands into SPIM's memory. If the le has already been read into SPIM, the system should be cleared (see reinitialize, below) or global symbols will be multiply de ned. Synonym for read. read "file" load "file" run <addr> Start running a program. If the optional address addr is provided, the program starts at that address. Otherwise, the program starts at the global symbol start, which is de ned by the default trap handler to call the routine at the global symbol main with the usual MIPS calling convention. 3 step <N> Step the program for N (default: 1) instructions. Print instructions as they execute. Continue program execution without stepping. Print register N . Print oating point register N . Print the contents of memory at address addr . Print the contents of the symbol table, i.e., the addresses of the global (but not local) symbols. Clear the memory and registers. Set a breakpoint at address addr . addr can be either a memory address or symbolic label. continue print $N print $fN print addr print sym reinitialize breakpoint addr delete addr list . Delete all breakpoints at address addr . List all breakpoints. Rest of line is an assembly instruction that is stored in memory. A newline reexecutes previous command. <nl> ? Print a help message. Most commands can be abbreviated to their unique pre x e.g., ex, re, l, dangerous commands, such as reinitialize, require a longer pre x. ru, s, p. More 1.2.2 X-Window Interface The X version of SPIM, xspim, looks di erent, but should operate in the same manner as spim. The X window has ve panes (see Figure 1). The top pane displays the contents of the registers. It is continually updated, except while a program is running. The next pane contains the buttons that control the simulator: quit Exit from the simulator. 4 xspim PC = 00000000 Status= 00000000 R0 R1 R2 R3 R4 R5 R6 R7 FP0 FP2 FP4 FP6 (r0) (at) (v0) (v1) (a0) (a1) (a2) (a3) = = = = = = = = = = = = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0.000000 0.000000 0.000000 0.000000 EPC HI R8 R9 R10 R11 R12 R13 R14 R15 FP8 FP10 FP12 FP14 = 00000000 = 00000000 (t0) (t1) (t2) (t3) (t4) (t5) (t6) (t7) = = = = = = = = = = = = Cause = 0000000 BadVaddr = 00000000 LO = 0000000 General Registers 00000000 R16 (s0) = 0000000 R24 (t8) = 00000000 00000000 R17 (s1) = 0000000 R25 (s9) = 00000000 00000000 R18 (s2) = 0000000 R26 (k0) = 00000000 00000000 R19 (s3) = 0000000 R27 (k1) = 00000000 00000000 R20 (s4) = 0000000 R28 (gp) = 00000000 00000000 R21 (s5) = 0000000 R29 (gp) = 00000000 00000000 R22 (s6) = 0000000 R30 (s8) = 00000000 00000000 R23 (s7) = 0000000 R31 (ra) = 00000000 Double Floating Point Registers 0.000000 FP16 = 0.00000 FP24 = 0.000000 0.000000 FP18 = 0.00000 FP26 = 0.000000 0.000000 FP20 = 0.00000 FP28 = 0.000000 0.000000 FP22 = 0.00000 FP30 = 0.000000 Single Floating Point Registers step terminal clear mode set value Register Display Control Buttons quit print load breakpt run help Text Segments User and Kernel Text Segments [0x00400000] [0x00400004] [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] 0x8fa40000 0x27a50004 0x24a60004 0x00041090 0x00c23021 0x0c000000 0x3402000a 0x0000000c lw R4, 0(R29) addiu R5, R29, 4 addiu R6, R5, 4 sll R2, R4, 2 addu R6, R6, R2 jal 0x00000000 ori R0, R0, 10 syscall Data Segments [0x10000000]...[0x10010000] 0x00000000 [0x10010004] 0x74706563 0x206e6f69 0x636f2000 [0x10010010] 0x72727563 0x61206465 0x6920646e [0x10010020] 0x000a6465 0x495b2020 0x7265746e [0x10010030] 0x0000205d 0x20200000 0x616e555b [0x10010040] 0x61206465 0x65726464 0x69207373 [0x10010050] 0x642f7473 0x20617461 0x63746566 [0x10010060] 0x555b2020 0x696c616e 0x64656e67 [0x10010070] 0x73736572 0x206e6920 0x726f7473 SPIM Version 3.2 of January 14, 1990 Data and Stack Segments 0x726f6e67 0x74707572 0x6e67696c 0x6e69206e 0x00205d68 0x64646120 0x00205d65 SPIM Messages Figure 1: X-window interface to SPIM. 5 load run Read a source le into memory. Start the program running. step Single-step through a program. clear Reinitialize registers or memory. set value print Set the value in a register or memory location. Print the value in a register or memory location. breakpoint help Set or delete a breakpoint or list all breakpoints. Print a help message. terminal mode Raise or hide the console window. Set SPIM operating modes. The next two panes display the memory contents. The top one shows instructions from the user and kernel text segments.2 The rst few instructions in the text segment are startup code ( start) that loads argc and argv into registers and invokes the main routine. The lower of these two panes displays the data and stack segments. Both panes are updated as a program executes. The bottom pane is used to display messages from the simulator. It does not display output from an executing program. When a program reads or writes, its IO appears in a separate window, called the Console, which pops up when needed. Although SPIM faithfully simulates the MIPS computer, it is a simulator and certain things are not identical to the actual computer. The most obvious di erences are that instruction timing and the memory systems are not identical. SPIM does not simulate caches or memory latency, nor does it accurate re ect the delays for oating point operations or multiplies and divides. Another surprise (which occurs on the real machine as well) is that a pseudoinstruction expands into several machine instructions. When single-stepping or examining memory, the instructions that you see are slightly di erent from the source program. The correspondence between the two sets of instructions is fairly simple since SPIM does not reorganize the instructions to ll delay slots. These instructions are real|not pseudo|MIPS instructions. SPIM translates assembler pseudoinstructions to 1{3 MIPS instructions before storing the program in memory. Each source instruction appears as a comment on the rst instruction to which it is translated. 2 1.3 Surprising Features 6 Comments in assembler les begin with a sharp-sign (#). Everything from the sharp-sign to the end of the line is ignored. Identi ers are a sequence of alphanumeric characters, underbars ( ), and dots (.) that do not begin with a number. Opcodes for instructions are reserved words that are not valid identi ers. Labels are declared by putting them at the beginning of a line followed by a colon, for example: .data item: .word 1 .text .globl main main: lw $t0, item 1.4 Assembler Syntax # Must be global Strings are enclosed in double-quotes ("). Special characters in strings follow the C convention: newline tab quote \n \t \" SPIM supports a subset of the assembler directives provided by the MIPS assembler: .align n Align the next datum on a 2n byte boundary. For example, .align 2 aligns the next value on a word boundary. .align 0 turns o automatic alignment of .half, .word, .float, and .double directives until the next .data or .kdata directive. Store the string in memory, but do not null-terminate it. Store the string in memory and null-terminate it. Store the values in successive bytes of memory. n .ascii str .asciiz str .byte b1, ..., bn .data <addr> The following data items should be stored in the data segment. If the optional argument addr is present, the items are stored beginning at address addr . Store the n .double d1, ..., dn .extern sym size oating point double precision numbers in successive memory locations. Declare that the datum stored at sym is size bytes large and is a global symbol. This directive enables the assembler to store the datum in a portion of the data segment that is e ciently accessed via register $gp. Store the n .float f1, ..., fn .globl sym oating point single precision numbers in successive memory locations. Declare that symbol sym is global and can be referenced from other les. 7 print int print oat print double print string read int read oat read double read string sbrk exit Service System Call Code 1 2 3 4 5 6 7 8 9 10 $a0 = integer $f12 = oat $f12 = double $a0 = string Arguments Result $a0 $a0 = bu er, $a1 = length = amount address (in $v0) integer (in $v0) oat (in $f0) double (in $f0) Table 1: System services. .half h1, ..., hn n Store the 16-bit quantities in successive memory halfwords. The following data items should be stored in the kernel data segment. If the optional argument addr is present, the items are stored beginning at address addr . The next items are put in the kernel text segment. In SPIM, these items may only be instructions or words (see the .word directive below). If the optional argument addr is present, the items are stored beginning at address addr . n .kdata <addr> .ktext <addr> .space n Allocate SPIM). bytes of space in the current segment (which must be the data segment in .text <addr> The next items are put in the user text segment. In SPIM, these items may only be instructions or words (see the .word directive below). If the optional argument addr is present, the items are stored beginning at address addr . n Store the 32-bit quantities in successive memory words. SPIM does not distinguish various parts of the data segment (.data, .rdata, and .sdata). 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 (see Table 1) into register $v0 and the arguments into registers $a0 $a3 (or $f12 for oating point values). System calls that return values put their result in register $v0 (or $f0 for oating point results). For example, to print \the answer = 5", use the commands: ::: .word w1, ..., wn 1.5 System Calls str: .data .asciiz "the answer = " 8 Memory CPU Registers $0 . . . $31 Arithmetic Unit Multiply Divide Lo Hi FPU (Coprocessor 1) Registers $0 . . . $31 Arithmetic Unit Coprocessor 0 (Traps and Memory) BadVAddr Status Cause EPC Figure 2: MIPS R2000 CPU and FPU .text li $v0, 4 la $a0, str syscall li $v0, 1 li $a0, 5 syscall print int # system call code for print_str # address of string to print # print the string # system call code for print_int # integer to print # print it is passed an integer and prints it on the console. print float prints a single oating point number. print double prints a double precision number. print string is passed a pointer to a null-terminated string, which it writes to the console. read int, read float, and read double read an entire line of input up to and including the newline. Characters following the number are ignored. read string has the same semantics as the Unix library routine fgets. It reads up to ; 1 characters into a bu er and terminates the string with a null byte. If there are fewer characters on the current line, it reads through the newline and again null-terminates the string. Warning: programs that use these syscalls to read from the terminal should not use memory-mapped IO (see Section 5). sbrk returns a pointer to a block of memory containing additional bytes. exit stops a program from running. n n 9 Register Name Number zero at v0 v1 a0 a1 a2 a3 t0 t1 t2 t3 t4 t5 t6 t7 s0 s1 s2 s3 s4 s5 s6 s7 t8 t9 k0 k1 gp sp fp ra 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Constant 0 Reserved for assembler Expression evaluation and results of a function Argument 1 Argument 2 Argument 3 Argument 4 Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Saved temporary (preserved across call) Temporary (not preserved across call) Temporary (not preserved across call) Reserved for OS kernel Reserved for OS kernel Pointer to global area Stack pointer Frame pointer Return address (used by function call) Usage Table 2: MIPS registers and the convention governing their use. 2 Description of the MIPS R2000 A MIPS processor consists of an integer processing unit (the CPU) and a collection of coprocessors that perform ancillary tasks or operate on other types of data such as oating point numbers (see Figure 2). SPIM simulates two coprocessors. Coprocessor 0 handles traps, exceptions, and the virtual memory system. SPIM simulates most of the rst two and entirely omits details of the memory system. Coprocessor 1 is the oating point unit. SPIM simulates most aspects of this unit. The MIPS (and SPIM) central processing unit contains 32 general purpose registers that are numbered 0{31. Register is designated by $n. Register $0 always contains the hardwired value 0. MIPS has established a set of conventions as to how registers should be used. These n 2.1 CPU Registers 10 15 10 5 4 3 2 1 0 In E n te ab rru le pt K U ern se e r l/ Figure 3: The Status register. suggestions are guidelines, which are not enforced by the hardware. However a program that violates them will not work properly with other software. Table 2 lists the registers and describes their intended use. Registers $at (1), $k0 (26), and $k1 (27) are reserved for use by the assembler and operating system. Registers $a0{$a3 (4{7) are used to pass the rst four arguments to routines (remaining arguments are passed on the stack)...
View Full Document

{[ snackBarMessage ]}