You are on page 1of 3

ECE2030 SPIM HOMEWORK Getting Familiar with SPIM DUE: Nov.

15, 2002
Type in the following program using a text editor (not a word processing package!) and execute it using PCSpim. PAY ATTENTION TO THE SPACES IN THE ASCII DIRECTIVES! There is a bug in PCSpim: you have to leave a blank line at the end of the program. To become familiar with the simulator it is useful to go through the following sequence of steps. FIRST download simulator and run through the on-line tutorials in the NEXT Section of the notes labeled PCSpim Tutorial Load the program into SPIM. If you typed it in correctly it should load with no errors. If there are typographical errors then a dialog window will pop up with the line number. Other wise open the Console window and you can check the error message. Continue to edit/load until all errors are taken care of. Run the program. Answer the questions in Problem #1 and see the results. The program should run with no problem. Look in the data segment. It will be filled with values starting at address 0x10010000. These values are product of the data directives in the program. Check the text segment. You will see instructions at each successive address in the text segment. Learn to associate addresses with instructions. Open the registers window and you will see the contents of all 32 registers. Check the registers contents before and after running the program. Answer the following questions After loading the sample program on following page into the SPIM simulator, what are the contents of the second, third, and fourth word in memory? Indicate the type of computer you are using to run the SPIM simulator, and after looking at the contents of the data segment, indicate if the machine uses big endian or little endian convention to store words.

1. 2.

3.

4.

How many bytes of data are being used in the data segment? At what address does the data segment begin? SPIM Sample Program

################################################################### # INSERT YOUR NAME # ECE 2030 ( E or F) # SPIM Homework#1 #################################################################### # Comments begin with the # character # Begin the data segment (declare variables) .data .asciiz SPIM is COOOOL!!! .byte 0xF2 .align 2 # enforce word alignment .word 0xF28343AA num1: .word 89 num2: .word 7 result: .word 0 result: .word 0 .text lw $t0,num1 lw $t1,num2 add $t2, $t0, $t1 sub $t3,$t0,$t1 sw $t3, result1 sw $t4, result2 # end execution of the program li $v0, 10 syscall # A bug in SPIM requires you to have an extra carriage return after the last line of code.

PCSpim for Windows Tutorial


PCSpim for Windows is an emulator for the MIPS instruction set. You can download it from the following URL: ftp://ftp.cs.wisc.edu/pub/spim. You should get the spimwin.exe file from that location. SPIM allows you to run simple programs written in the MIPS assembly language. Your program can interact with the user with an emulated command prompt (the console). While your program is running, you can examine the state of the emulator including the data storage and the program being run. A local SPIM PCSPIM tutorial page is available at http://users.ece.gatech.edu/~sudha/2030/temp/spim-isa/spim-isa.html. Check the link to the tutorial page. Note that SPIM doesnt have a built-in text editor, so youll have to use Notepad or some other editor to create your assembly files. Assembly files are saved as plain text files with a .s file extension (e.g., lab.s). Each line of the file is either a comment, an instruction to the emulator, or a MIPS assembly instruction. When a program is read in, it is divided into data and program code. The memory model used by the SPIM emulator has a text segment (where your program goes), a data segment (where declared variables are), and a kernel. The kernel code imitates the functions that an operating system would normally provide; you should never have to do anything with it. Interaction between your emulated program and a user is accomplished with a window in SPIM called the console window. Special functions are provided for screen input and output. MENUs in PCSpim Note: the menus have more options than the ones listed here. These are the options youll probably use most often. Feel free to play with the others. FILE -open: This loads an assembly source code file (a .s file) and prepares the emulator to run it. The program will be put in the text segment and the data declarations will be put in the data segment. If you get an error window when you open your .s file, try opening the settings and turn off the loading of the trap file. If the message windows appears with syntax errors listed in it, youll have to correct your assembly code (using Notepad or another editor) and then try to open the file again. -exit: Closes SPIM.

SIMULATOR -reload: Restarts the simulator and re-reads assembly file you have open. Use this if you run your program, make changes to it (in your editor), and want to run it again to see if the changes worked. Note that SPIM can sometimes get confused and reloading wont fix it. In these cases, close SPIM completely and restart it. -go: Starts running the program. It lets you choose what you want the program counter to start at (i.e., it lets you choose which line of code to start running at). Your program always begins at 0x00400000, the beginning of the text segment. -break: This stops the assembly program wherever it might be. You can look at what values are held in data memory, the registers, and you can see what line of code was being executed. This is useful when you are debuggingespecially if you think you are getting stuck in an infinite loop. -single step: Run next instruction only, then break. Use this to watch register and data values change as your program runs and to see which instructions are being run. You can run a single step after breaking or to start the program at the first instruction (0x00400000).

-set breakpoints: This is yet another debugging trick. Suppose you are suspicious of a certain section of your program, and want to watch it run. Open your assembly file and look at the text segment. Find the instructions you want to watch in the text segment. Click on Simulator then Set Breakpoints and enter the address of the instruction that begins the portion of code you want to watch. Now when you run your program SPIM will automatically break for you when it reaches that section of code. WINDOW -messages: This shows SPIM feedback, such as syntax errors when you load your program.

-text segment: This contains the program that is stored on the MIPS machine. The columns in this window are the following: the address of the instruction, the binary for the machine code of this instruction, the actual instruction being used, and finally the line from your assembly file that you typed in to create this instruction. -data segment: This shows the memory locations you allocated in the .data section of your program This window shows four words of data per row. -registers: LO registers. -console: Shows the contents of the 32 general purpose registers, the PC, and the multiply HI and This is the screen that you can print to and read from in your program.

-clear console: Remove the text currently on the console. Debugging Example Type in a sample program. Load the program. Run the program. Look at the text segment. Convince yourself that you recognize the instructions from your original assembly file in the Text Segment window. Set a breakpoint. Suppose you want to make sure that the SUB instruction is working correctly. Find the instruction just before the SUB in the Text Segment window. For this program, the address of that instruction is a MOVE instruction at 0x00400030. Add a breakpoint at this address (click on Simulator, Breakpoints, type in the address, click on Add, then click on Close). Run the program normally. The program will break just before the SUB instruction executestell SPIM not to continue execution. You can watch the values in the Registers window to make sure the values are correct (watch $t0, $t1, and $t2). Press F10 (Single Step) to run the program one instruction at a time. Once youre sure the program is working, press F5 (Run) to complete the program.

You might also like