📄 spim.tex
字号:
% Master File: spim.tex% Document type: LaTeX%% Documentation for SPIM. This is a latex file.%% Copyright (C) 1990--1997 by James R. Larus (larus@cs.wisc.edu).%% SPIM is distributed under the following conditions:%% You may make copies of SPIM for your own use and modify those copies.%% All copies of SPIM must retain my name and copyright notice.%% You may not sell SPIM or distributed SPIM in conjunction with a% commerical product or service without the expressed written consent of% James Larus.%% THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR% IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR% PURPOSE. */% $Header: $%% You are free to make and distribute copies of this documentation as% long as my name, address and copyright notices remain on both the% source and printed versions.%\documentstyle[11pt]{article}% This is necessary for LaTeX2e. Comment it out for older versions.\usepackage{psfig}% Use this command for older versions of LaTeX.%\input{psfig}% Change margins:\setlength{\oddsidemargin}{0.1 true in}\setlength{\evensidemargin}{0.15 true in}\setlength{\marginparwidth}{1 true in}\setlength{\oddsidemargin}{0.125 true in}\setlength{\evensidemargin}{0.125 true in}\setlength{\marginparwidth}{0.75 true in}\setlength{\topmargin}{-.50 true in}\setlength{\textheight}{9.0 true in}\setlength{\textwidth}{6.30 true in}\begin{document}\title{SPIM S20: A MIPS R2000 Simulator\thanks{I grateful to the many students at UW who used SPIM in their coursesand happily found bugs in a professor's code. In particular, thestudents in CS536, Spring 1990, painfully found the last few bugs inan ``already-debugged'' simulator. I am grateful for their patienceand persistence. Alan Yuen-wui Siow wrote the X-window interface.}\\{\small ``$\frac{1}{25}^{th}$ the performance at none of thecost''}}\author{{\normalsize James R. Larus} \\ {\normalsize larus@cs.wisc.edu} \\ {\normalsize Computer Sciences Department} \\ {\normalsize University of Wisconsin--Madison} \\ {\normalsize 1210 West Dayton Street} \\ {\normalsize Madison, WI 53706, USA} \\ {\normalsize 608-262-9519}}\date{Copyright \copyright 1990--1997 by James R. Larus \\ (This document may be copied without royalties, \\ so long as this copyright notice remains on it.)}\maketitle\newcommand {\pinst} [2]% {\bigskip\noindent{\em{{\tt #1}\hfill#2 ${}^{\dagger}$\newline}}}\newcommand {\inst} [2]% {\bigskip\noindent{\em{{\tt #1}\hfill#2\newline}}}\newcommand {\pinstX} [2]% {\noindent{\em{{\tt #1}\hfill#2 ${}^{\dagger}$\newline}}}\newcommand {\instX} [2]% {\noindent{\em{{\tt #1}\hfill#2\newline}}}\section{SPIM}SPIM S20 is a simulator that runs programs for the MIPS R2000/R3000 RISCcomputers.\footnote{For a description of the real machines, see Gerry Kaneand Joe Heinrich, {\em MIPS RISC Architecture,\/} Prentice Hall, 1992.} SPIMcan read and immediately execute files containing assembly language. SPIMis a self-contained system for running these programs and contains adebugger and interface to a few operating system services.The architecture of the MIPS computers is simple and regular, whichmakes it easy to learn and understand. The processor contains 32general-purpose 32-bit registers and a well-designed instruction set thatmake it a propitious target for generating code in a compiler.However, the obvious question is: why use a simulator when many peoplehave workstations that contain a hardware, and hence significantlyfaster, implementation of this computer? One reason is that theseworkstations are not generally available. Another reason is thatthese machine will not persist for many years because of the rapidprogress leading to new and faster computers. Unfortunately, thetrend is to make computers faster by executing several instructionsconcurrently, which makes their architecture more difficult tounderstand and program. The MIPS architecture may be the epitome of asimple, clean RISC machine.In addition, simulators can provide a better environment for low-levelprogramming than an actual machine because they can detect more errorsand provide more features than an actual computer. For example, SPIMhas a X-window interface that is better than most debuggers for theactual machines.Finally, simulators are an useful tool for studying computers and theprograms that run on them. Because they are implemented in software,not silicon, they can be easily modified to add new instructions,build new systems such as multiprocessors, or simply to collect data.\subsection{Simulation of a Virtual Machine}The MIPS architecture, like that of most RISC computers, is difficultto program directly because of its delayed branches, delayed loads,and restricted address modes. This difficulty is tolerable sincethese computers were designed to be programmed in high-level languagesand so present an interface designed for compilers, not programmers.A good part of the complexity results from delayed instructions. A{\em delayed branch\/} takes two cycles to execute. In the secondcycle, the instruction immediately following the branch executes.This instruction can perform useful work that normally would have beendone before the branch or it can be a {\tt nop} (no operation).Similarly, {\em delayed loads\/} take two cycles so the instructionimmediately following a load cannot use the value loaded from memory.MIPS wisely choose to hide this complexity by implementing a {\emvirtual machine\/} with their assembler. This virtual computerappears to have non-delayed branches and loads and a richerinstruction set than the actual hardware. The assembler {\emreorganizes\/} (rearranges) instructions to fill the delay slots. Italso simulates the additional, {\em pseudoinstructions\/} bygenerating short sequences of actual instructions.By default, SPIM simulates the richer, virtual machine. It can alsosimulate the actual hardware. We will describe the virtual machineand only mention in passing features that do not belong to the actualhardware. In doing so, we are following the convention of MIPSassembly language programmers (and compilers), who routinely takeadvantage of the extended machine. Instructions marked with a dagger($\dagger$) are pseudoinstructions.\subsection{SPIM Interface}SPIM provides a simple terminal and a X-window interface. Bothprovide equivalent functionality, but the X interface is generallyeasier to use and more informative.{\tt spim}, the terminal version, and {\tt xspim}, the X version, havethe following command-line options:\begin{description} \item [] {\tt -bare}\newline Simulate a bare MIPS machine withoutpseudoinstructions or the additional addressing modes provided by theassembler. Implies {\tt -quiet}. \item [] {\tt -asm}\newline Simulate the virtual MIPS machineprovided by the assembler. This is the default. \item [] {\tt -pseudo} \newline Accept pseudoinstructions in assemblycode. \item [] {\tt -nopseudo} \newline Do not accept pseudoinstructions inassembly code. \item [] {\tt -notrap} \newline Do not load the standard traphandler. This trap handler has two functions that must be assumed bythe user's program. First, it handles traps. When a trap occurs,SPIM jumps to location 0x80000080, which should contain code toservice the exception. Second, this file contains startup code thatinvokes the routine {\tt main}. Without the trap handler, executionbegins at the instruction labeled {\tt \_\_start}. \item [] {\tt -trap}\newline Load the standard trap handler. Thisis the default. \item [] {\tt -trap\_file}\newline Load the trap handler in the file. \item [] {\tt -noquiet}\newline Print a message when an exceptionoccurs. This is the default. \item [] {\tt -quiet}\newline Do not print a message at anexception. \item [] {\tt -nomapped\_io}\newline Disable the memory-mapped IOfacility (see Section~\ref{sec:IO}). \item [] {\tt -mapped\_io}\newline Enable the memory-mapped IO facility(see Section~\ref{sec:IO}). Programs that use SPIM syscalls (seeSection~\ref{sec:scall}) to read from the terminal should not also usememory-mapped IO. \item [] {\tt -file}\newline Load and execute the assembly code inthe file. \item [] {\tt -s seg size} Sets the initial size of memory segment{\em seg\/} to be {\em size\/} bytes. The memory segments are named:{\tt text}, {\tt data}, {\tt stack}, {\tt ktext}, and {\tt kdata}.For example, the pair of arguments {\tt -sdata 2000000} starts theuser data segment at 2,000,000 bytes. \item [] {\tt -lseg size} Sets the limit on how large memory segment{\em seg\/} can grow to be {\em size\/} bytes. The memory segmentsthat can grow are: {\tt data}, {\tt stack}, and {\tt kdata}.\end{description}\subsubsection{Terminal Interface}The terminal interface ({\tt spim}) provides the following commands:\begin{description} \item [] {\tt exit}\newline Exit the simulator. \item [] {\tt read "file"}\newline Read {\em file\/} of assemblylanguage commands into SPIM's memory. If the file has already beenread into SPIM, the system should be cleared (see {\tt reinitialize},below) or global symbols will be multiply defined. \item [] {\tt load "file"}\newline Synonym for {\tt read}. \item [] {\tt run <addr>}\newline Start running a program. If theoptional address {\em addr\/} is provided, the program starts at thataddress. Otherwise, the program starts at the global symbol {\tt\_\_start}, which is defined by the default trap handler to call theroutine at the global symbol {\tt main} with the usual MIPS callingconvention. \item [] {\tt step <N>}\newline Step the program for {\em N\/}(default: 1) instructions. Print instructions as they execute. \item [] {\tt continue}\newline Continue program execution withoutstepping. \item [] {\tt print \$N}\newline Print register {\em N\/}. \item [] {\tt print \$fN}\newline Print floating point register {\emN\/}. \item [] {\tt print addr}\newline Print the contents of memory ataddress {\em addr\/}. \item [] {\tt print\_sym}\newline Print the contents of the symboltable, i.e., the addresses of the global (but not local) symbols. \item [] {\tt reinitialize}\newline Clear the memory and registers. \item [] {\tt breakpoint addr}\newline Set a breakpoint at address{\em addr\/}. {\em addr\/} can be either a memory address or symboliclabel. \item [] {\tt delete addr}\newline Delete all breakpoints at address{\em addr\/}. \item [] {\tt list}\newline List all breakpoints. \item [] {\tt .}\newline Rest of line is an assembly instructionthat is stored in memory. \item [] {\tt <nl>}\newline A newline reexecutes previous command. \item [] {\tt ?}\newline Print a help message.\end{description}Most commands can be abbreviated to their unique prefix e.g., {\ttex}, {\tt re}, {\tt l}, {\tt ru}, {\tt s}, {\tt p}. More dangerouscommands, such as {\tt reinitialize}, require a longer prefix.\subsubsection{X-Window Interface}\begin{figure} \centerline{\psfig{figure=xinterface.id,height=4.5in}} \caption{X-window interface to SPIM.} \label{fig:xinterface}\end{figure}The X version of SPIM, {\tt xspim}, looks different, but shouldoperate in the same manner as {\tt spim}. The X window has five panes(see Figure~\ref{fig:xinterface}). The top pane displays the contentsof the registers. It is continually updated, except while a programis running.The next pane contains the buttons that control the simulator:\begin{itemize} \item [] {\bf quit}\newline Exit from the simulator. \item [] {\bf load}\newline Read a source file into memory. \item [] {\bf run}\newline Start the program running. \item [] {\bf step}\newline Single-step through a program. \item [] {\bf clear}\newline Reinitialize registers or memory. \item [] {\bf set value}\newline Set the value in a register ormemory location. \item [] {\bf print}\newline Print the value in a register or memorylocation. \item [] {\bf breakpoint}\newline Set or delete a breakpoint or listall breakpoints. \item [] {\bf help}\newline Print a help message. \item [] {\bf terminal}\newline Raise or hide the console window. \item [] {\bf mode}\newline Set SPIM operating modes.\end{itemize}The next two panes display the memory contents. The top one showsinstructions from the user and kernel text segments.\footnote{Theseinstructions are real---not pseudo---MIPS instructions. SPIMtranslates assembler pseudoinstructions to 1--3 MIPS instructionsbefore storing the program in memory. Each source instruction appearsas a comment on the first instruction to which it is translated.} Thefirst few instructions in the text segment are startup code ({\tt\_\_start}) that loads {\tt argc} and {\tt argv} into registers andinvokes the {\tt 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. Itdoes not display output from an executing program. When a programreads or writes, its IO appears in a separate window, called theConsole, which pops up when needed.\subsection{Surprising Features}Although SPIM faithfully simulates the MIPS computer, it is asimulator and certain things are not identical to the actual computer.The most obvious differences are that instruction timing and thememory systems are not identical. SPIM does not simulate caches ormemory latency, nor does it accurate reflect the delays for floatingpoint operations or multiplies and divides.Another surprise (which occurs on the real machine as well) is that apseudoinstruction expands into several machine instructions. Whensingle-stepping or examining memory, the instructions that you see areslightly different from the source program. The correspondencebetween the two sets of instructions is fairly simple since SPIM doesnot reorganize the instructions to fill delay slots.\subsection{Assembler Syntax}\label{sec:syntax}Comments in assembler files begin with a sharp-sign ({\tt \#}).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -