CSE 378 - Winter 2002

Machine Organization and Assembly Language Programming

Problem Set 1: Familiarization with SPIM
Due: Monday, January 14

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 that has been written in assembly language by someone else. 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 you read Appendix A and Chapter 3. The purpose of this assignment is simply for you to become familiar with the spim environment.

You will have to download spim, as it is not installed on the PCs in the labs. 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. You can find everything you need on the software page.

For this assignment, we will use a program which computes the sum of the first 3 powers of 10. The C version appears in Listing 1, and the MIPS Assembly version in Listing 2. The line numbers on the left of Listing 2 are for reference only, and are not part of the program.

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. What is printed on the spim console?
 
 
 
 

Now run the program in single steps (F10 is the keyboard shortcut) - you will have to "reload" to make it work!!. 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 23
first iteration
Line 23
second iteration
Line 39
$25        
$24        
$8        
8($sp)        
4($sp)        


Listing 1 (C Code):
#include <stdio.h>
main()
{
  int i;
  int sum = 1;
  int power = 1;
  for (i = 1; i <= 2; i = i + 1) {
    power = power * 10;
    sum = sum + power;
  }
  printf ("The sum of the first 3 powers of 10 is %d\n", sum);
}

 
Listing 2 (Assembly Code):
1. .data # tells assembler that this is data
2. str: .asciiz "The sum of the first 3 powers of 10 is: "  
3. newline: .asciiz "\n"  
4. .text # tells assembler the following is code
5. .globl main # Entry into our code must be global
5. main:  
6. subu $sp, 16 # Allocate space for storage on stack (4 words)
7. sw $31, 0($sp) # Save return address (to caller of main)
8. sw $4, 12($sp) # Save R4 (not necessary)
9. li $14, 1 # Put constant 1 into temp register 14
10. sw $14, 4($sp) # Place 1's into our two words of
11. sw $14, 8($sp) #  temporary storage (sum & power)
12. li $16, 10 # Put constant 10 into register 16
13. li $8, 1 # Iteration counter
14. loop:  
15. lw $24, 8($sp) # Load value of power (1st time it's 1)
16. lw $25, 4($sp) # Load value of sum (1st time it's 1)
17. mul $24, $24, $16 # Compute next power of 10
18. sw $24, 8($sp) # Store the power
19. addu $25, $25, $24 # Add it to the sum
20. sw $25, 4($sp) # Store the sum
21. addu $8, $8, 1 # Increment the counter
22. ble $8, 2, loop # If counter != 2, repeat
    # Sum now contains the sum of powers of 10
23. li $2, 4 # Use syscall 4 (print_string)
24. la $4, str #  to display string str
25. syscall  
26. li $2, 1 # Use syscall 1 (print_int)
27. lw $4, 4($sp) #  to print the sum
28. syscall  
29. li $2, 4  
30. lw $4, newline  
31. syscall  
32. lw $31, 0($sp) # Get return address
33. addu $sp, 16 # Restore stack
34. j $31 # Return to caller
35. .end main