CSE 410 Programming Assignment 2

Spring 2007

Due: At the beginning of class, Wednesday, 4/18/2007; see turnin instructions below

Write a MIPS assembly program that sorts an array in memory.  The sorting algorithm should be a simple bubble sort.  You may search the web for an in-depth description (and even animations) of bubble sort, but here is the algorithm both in pseudo code and C code.

 

Pseudo code:

Given an array of numbers, array, and an array length, length:

for i from 0 to length-1

      for j from 0 to length-1

            if (array[j] > array[j+1])

swap array[j] and array[j+1]

 

C code:

for (i=0; i<n-1; i++) {
  for (j=0; j<n-1; j++)
    if (a[j] > a[j+1]) {  /* compare the two neighbors */
      tmp = a[j];         /* swap a[j] and a[j+1]      */
      a[j] = a[j+1];
      a[j+1] = tmp;
    }
}

 

Put the following declarations at the beginning of your code.  These declarations will initialize memory to contain the array and length at startup.  The array and length can then be read and modified using loads and stores.

         .data
array:   .word   4 15 8 39 1 
length:  .word   5

 

You will want to use SPIM to test your program to make sure it works properly.  Your code should be well-organized so that it is clear what each section of your code does.  If your solution starts to get lengthy you should reconsider what you are doing - at least one solution we've seen had less than 20 lines of code.

 

You should turn in your code online and hand in a printed copy in class. Use this link to turn in a copy of the file containing your code.