CSE 378 - Autumn 1999

Machine Organization and Assembly Language Programming

Exercise: Familiarization with SPIM

The purpose of this homework is to familiarize you with the R3000 Simulator, xspim.   You will learn how to use xspim to run assembly language programs and to extract information to debug your programs.  You will learn assembly operations as you read Appendix A and Chapter 3.  (Recall that you need to start reading the Appendix.)  Don't worry if you cannot understand all of it now.  The purpose of this assignment is simply for you to become familiar with the xspim environment.

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 do not have to type this program in yourself.  The program already resides here.  Simply copy this file into your own directory and use it.

I.  Run the simulator in single steps.  What is printed on the spim console?
 
 
 
 

Do you notice any discrepancies between the given program and the instructions executed?  What are 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.
 
 
Location Line 16 Line 23
first iteration
Line 23
second iteration
Line 39
$25        
$24        
$8        
8($sp)        
4($sp)        

 

Below is a program written in C.  You can also find a separate file for this C program here.  However, this program is short enough that you can also type it in, and we ask you to do that in the Getting Started exercise so you can practice with emacs.
 

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);
}
Take a look at the assembly code (prob0.s) and the C code (prob0.c) to begin to see the relationship and differences between operations in a high-level language and operations in assembly language.
 
 
Listing 2 (Assembly Code):
 
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
.data
str: .asciiz "The sum of the first 3 powers of 10 is: "
newline: .asciiz "\n"

.text
.globl main

main:
subu $sp, 16
sw $31, 0($sp)
sw $4, 12($sp)
li $14, 1
sw $14, 4($sp)
sw $14, 8($sp)
li $16, 10
li $8, 1
loop:
lw $24, 8($sp)
lw $25, 4($sp)
mul $24, $24, $16
sw $24, 8($sp)
addu $25, $25, $24
sw $25, 4($sp)
addu $8, $8, 1
ble $8, 2, loop

li $2, 4
la $4, str
syscall

li $2, 1
lw $4, 4($sp)
syscall

li $2, 4
lw $4, newline
syscall

lw $31, 0($sp)
addu $sp, 16
j $31

.end main

# tells assembler that this is data
 
 

# tells assembler the following is code
# Entry into our code must be global
 

# Allocate space for storage on stack (4 words)
# Save return address (to caller of main)
# Save R4 (not necessary)
# Put constant 1 into temp register 14
# Place 1's into our two words of
#  temporary storage (sum & power)
# Put constant 10 into register 16
# Iteration counter

# Load value of power (1st time it's 1)
# Load value of sum (1st time it's 1)
# Compute next power of 10
# Store the power
# Add it to the sum
# Store the sum
# Increment the counter
# If counter != 2, repeat
# Sum now contains the sum of powers of 10
# Use syscall 4 (print_string)
#  to display string str
 

# Use syscall 1 (print_int)
#  to print the sum
 
 
 
 
 

# Get return address
# Restore stack
# Return to caller