CSSE 232 - Computer Architecture I
Rose-Hulman Institute of Technology
Computer Science and Software Engineering Department

Exercise 1 -- SPIM

Installing SPIM

You need not be ``localmgr'' to perform this installation.

  1. Insert the ``Companion CD'' into your CDROM drive.
  2. Internet explorer should automatically start and display the navigation screen for the companion CD.
  3. Select the Software button.
  4. Select the link for Spim, PCSpim and xspim.
  5. Under windows, select the link Click here.
  6. Run SETUP.EXE to install PCSPIM. If you are logged in as a regular user, you will be prompted for an installation account with sufficient privileges. You should specify your ``localmgr'' account here. Once you've done that, the installation is straight forward - you can accept all the defaults.

Starting SPIM

Launch PCSPIM. A dialog box will open complaining about a missing exceptions file. Select Yes and then Browse. Navigate to C:\Program Files\PCSpim and select exceptions.s. Click OK. Two windows should appear. One is labelled ``Console'' and is for program input and output. The second is labelled ``PCSPIM'' and is divided input four panes.

  1. The first pane shows the contents of the registers. We will be primarily interested in the program counter and the general registers.
  2. The next pane shows the contents of the ``text segment'' i.e. the program. It looks messy, but there are just four items on each line, each of which relate to a single same machine instruction. The first item is the address. The 0x prefix designates a hexadecimal number. The second item is the hexadecimal representation of the machine instruction. The third item is (basically) the assembly language representation of the machine instruction. The fourth item (if it's present) is the actual line from the source file that generated the machine instruction. Notice that it contains instructions even though you haven't loaded a program yet. That's the code that runs your program and handles any errors.
  3. The third pane shows the data segment, including the stack contents. For now, we'll only be concerned with the first part (labelled ``DATA'').
  4. The last pane shows messages.

Look at the ``text segment'' pane. Notice that instructions start at address 0x00400000 and continues to address 0x00400020, then skips to address 0x80000180 where the kernel starts. The code that starts at 0x00400000 handles command line arguments, calls your program as a procedure, and then exits. Your program will be loaded immediately after that code, at address 0x00400024. Also notice that the PC (program counter) register has the value 0.

Running a Program

  1. Download p1.asm to your local hard disk (Recommendation: right-click on the link and choose Save As). Open it in your favorite text editor and examine it.
  2. Click File:Open and select p1.asm to load the file into PCSPIM. (Note: if you get an error, you probably lost the carriage return at the end of the last line in the process of downloading the file). Notice that the text segment now includes your program starting at address 0x00400024. Also notice that the value of the PC changes to 00400000.
  3. Click Simulator:Breakpoints..., enter main, and click Add and then Close to set a breakpoint at the label main. We want to skip some initialization code that is executed before our program runs. Notice that the instruction at that address (0x00400024) appears to be replaced by ``break $1'' - it isn't really.
  4. Click Simulator:Go (the default starting address of 0x00400000 is correct), Ok, and No to run the simulator upto the breakpoint. The ``break $1'' instruction will be highlighted.
  5. Notice the value of the PC.
  6. Notice that registers $0 and $t2 currently contain zero.
  7. Click Simulator:Single Step. Notice that the instruction that was originally at 0x00400024 (ori $t2, $0, 40) appears in the messages pane, indicating that it has just been executed. Also notice that the contents of register $t2 have changed, and that the new value is shown in hexadecimal form. Finally, notice the value of the PC.
  8. Predict the effects of executing the next three instructions (the second ori, the add, and the third ori). Which registers will change, and what will the new values be?
  9. Execute the next three instructions by clicking Simulator:Single Step three times. Did you predict correctly?
  10. The next instruction attempts to change the value of register $0 to 40, and when you execute it, it will appear to have that effect. When you execute the next instruction, though, you will see that the value of register $0 is changed back to 0 before it is used, so in effect it never really changed. This SPIM behavior is somewhat unfortunate, because it obscures the fact that register $0 always contains 0.
  11. The next instruction is the syscall. If you single step one time, the syscall instruction will remain highlighted, but the PC will change to 0, indicating that the program has exited.
  12. If you single step again, the first instruction in the kernel will be highlighted, because you just tried to execute an instruction at a bad address.
  13. Click Simulator:Continue. The console should fill with exception messages complaining about your attempt to execute instructions at bad addresses. Press Control-C to stop the simulation.

Running a Second Program

  1. Click Simulator:Reinitialize. The register and text segment contents will return to the state prior to your loading the first program (note: SPIM occasionally gets confused to the point that you have to close it reopen it).
  2. Download p2.asm to your local hard disk, look at it in your text editor, and then open it in SPIM.
  3. Set a breakpoint at the beginning of your program, and run to the breakpoint.
  4. In turn, predict the effects of executing the first three instructions (the first ori, the lui, and the second ori). Simulate their execution. Did you predict correctly?
  5. The next instruction in the source file is the first li. Notice that it actually translates into two instructions. The reason is that the immediate cannot be represented in 16 bits. This will become more clear when we talk more about the machine language representations of the instructions).
  6. Predict the effects of each of the two instructions and simulate their execution. Notice that the combined effect is the one intended by the assembly language programmer, except that the contents of register $1 changed. By convention, that register is reserved for the assemblers use.
  7. The next instruction in the source file is the second li. Notice that it actually translates into a single instruction, because the immediate can be represented in 16 bits.

Running a Third Program

  1. Reinitialize SPIM, download p3.asm to your local hard disk, look at it in your text editor, open it in SPIM, and run to the beginning of the program.
  2. Notice that the data segment has changed. The array A is stored starting at address 0x10010000. Notice that each element of the array requires 32 bits, i.e. 4 bytes, of storage, and that SPIM lists four words per line, so every 16th address is displayed. The array has 16 elements, and the variable h is stored immediately following the array, so it is at address 0x10010040.
  3. The first instruction in the source file is an la, which expands to lui and ori. Predict its effects and simulate it. Do the new contents of register $t1 match the assembly language programmer's intent?
  4. Do the same for the remaining instructions in the program.

Writing a Program

Modify the p3.asm to find the sum of the last three elements of the array A and store the result in a new memory variable called total. Remember that the array is zero-based and that each element requires four bytes of storage.



J.P. Mellor 2004-09-08