The purpose of this homework is to familiarize you with the R3000 Simulator, spim. In this assignment you will learn how to use spim to run assembly language programs and to extract information to debug your programs. In doing this, you will use a program is already written in assembly language. Since many operations in assembly language are similar to those in high-level languages, you might be able to understand some of what the program is doing. But don't worry if you don't understand all of it now. You will learn assembly operations later on as we discuss them in class and in sections, and as you read Appendix A and Chapter 3. The purpose of this assignment is simply for you to become familiar with the spim environment.
Note: This software is already installed on the Windows machines in the labs at O:\nt\iuns\spim\SPIM63a\bin
. Don't make
65 copies of it on the CSE servers -- if you can, use this already-installed version.
It is small and self contained, so you should be
able to use it on home machines as well. Currently we are only
supporting the PC version. If you want to download SPIM on your personal computer, or cannot access it in the labs, you can find everything
you need on the on the Software link of the
home page
or directly at SPIM
For this assignment, we will use a program which computes the 10th fibonacci number. The C version appears in Listing 1, and the MIPS Assembly version in Listing 2.
You can download the (unnumbered) assembly code from here. Simply click on the link and save it someplace you can remember. You then want to "Open" it using PCSpim.
I. Execute the program in the simulator. Use "Go" (F5
is a shortcut)
in the simulator menu and the start address 0x00400000
(this can be done in the dialog box that will open under the GO command).
What is printed
on the spim console?
Now run the program in single steps (F10 is the keyboard shortcut)
When you want to re-run the program, you will have to "reload"
to make it work!!. In order to set the PC use "Set Value"
in the simulator menu with the register name being PC, and
the value 0x00400000. You can verify it is correct by looking
at the value of the PC displayed as the top lefmost register.
Do you notice
any discrepancies between the given program and the instructions
executed by spim? What are a few (3 - 5) of the
discrepancies?
II. Complete the following table by examining various registers
and memory location contents after executing the appropriate
instructions. Try to use the breakpoint command. If you are seeing
strange things, think about "reloading" the program within
spim.
Location | Line 16 | Line 26
first iteration |
Line 26
fourth iteration |
Line 38 |
$25 | ||||
$24 | ||||
$8 | ||||
8($sp) | ||||
4($sp) |
#include <stdio.h> main() { int i = 3; int fib1 = 1; int fib2 = 1; int temp; for(i; i <= 10; i++) { temp = fib1 + fib2; fib2 = fib1; fib1 = temp; } printf("The 10th fibonacci number is: %d\n", fib1); }
1: .data # Tells assembler that this is data 2: message: 3: .asciiz "The 10th fibonacci number is: " 4: newline: 5: .asciiz "\n" 6: 7: .text # Tells the assembler that the following is code 8: .globl main # Entry into our code must be global 9: 10: main: 11: subu $sp, 12 # Allocate 3 words of storage space on stack 12: sw $31, 0($sp) # Save the return address (to caller of main) 13: 14: li $14, 1 # Put a constant 1 into temporary register 14 15: sw $14, 4($sp) # Put a 1 into our two words of temporary storage 16: sw $14, 8($sp) # Stores c-equivalent of fib1 and fib2 17: 18: li $8, 3 # Our iteration counter starts at 3 because 19: # the first two numbers are one (loaded above) 20: 21: loop: # Label denoting a position of code named "loop" 22: lw $24, 4($sp) # Put fib1 into register 24 23: lw $25, 8($sp) # Put fib2 into register 25 24: addu $25, $25, $24 # Put next fibonacci number into register 25 25: sw $24, 8($sp) # Put fib1 into storage reserved for fib2 26: sw $25, 4($sp) # Put the new fibonacci number into fib1 27: addu $8, $8, 1 # Increment the counter 28: ble $8, 10, loop # If counter $8 <= 10, go to loop 29: 30: # fib1 [4($sp)] now has our 10th fibonacci number 31: 32: li $2, 4 # Use syscall 4 (print_string) 33: la $4, message # Load address of string "message" in .data section 34: syscall # Print the message 35: 36: li $2, 1 # Use syscall 1 (print_int) 37: lw $4, 4($sp) # Load the number 38: syscall # Print the number 39: 40: li $2, 4 41: la $4, newline 42: syscall 43: 44: lw $31, 0($sp) # Get return address 45: addu $sp, 12 # Restore stack pointer 46: j $31 # Return to caller 47: 48: .end main