There are two uploads for this homework.
- By 11:00 PM on 6 November, upload with a compressed (ZIP or tar.gz) copy of the MPLABX project you are using to work on this project to the Homework 10A moodle drop off.
- By 11:00 PM on 16 November, upload your PIC32 assembly solution file bubblepass.s to the Homework 10B moodle drop off.
And then there is the hints page.
The Problem
This is your one-and-only assembly language programming project. Write a function that completes one pass of the notorious bubble sort. The function returns the number of elements that were swapped during the pass.
#include "bubblepass.h"
int bubblePass(int V[], int n) {
int i ;
int swappedCount = 0 ;
for (i=0; i < n-1; ++i) {
if (V[i] > V[i+1]) {
int tmpSwapVar;
tmpSwapVar = V[i] ;
V[i] = V[i+1] ;
V[i+1] = tmpSwapVar ;
swappedCount++ ;
}
}
return swappedCount ;
}
Testing
You can test your routine using the following main routine.
#include <stdio.h>
#include <stdlib.h>
#include "bubblepass.h"
void bubbleSort(int *V, int n) {
int num2Sort = n ;
while (bubblePass(V, num2Sort)) {
--num2Sort ;
}
}
void printSorted(int *V, int n) {
int i ;
int sorted = 1 ;
char *ans ;
for (i=0; i<n; ++i) {
printf(" [%2d] is %6d\n", i, V[i]) ;
}
for (i=0; i<n-1 && sorted; ++i) {
sorted = sorted && V[i] < V[i+1] ;
}
if (sorted) {
printf("Sorted!\n") ;
} else {
printf("Unsorted!!!!!!\n") ;
}
}
int main(int argc, char** argv) {
int U[] = {255, -3, 5, 70, -101, 30 } ;
int V[] = {} ;
int L[] = {2, 3, 5, 7, 11, 13, 17, 19, 23} ;
int N[] = {-2, -3, -5, -7, -11, -13, -17, -19, -23} ;
bubbleSort(U, sizeof U/sizeof(int)) ;
printSorted(U, sizeof U/sizeof(int)) ;
bubbleSort(V, 0) ;
printSorted(V, 0) ;
bubbleSort(L, sizeof L/sizeof(int)) ;
printSorted(L, sizeof L/sizeof(int)) ;
bubbleSort(N, sizeof N/sizeof(int)) ;
printSorted(N, sizeof N/sizeof(int)) ;
return (EXIT_SUCCESS);
}
Use following prototype to compile the main routine. Save it in the file bubblepass.h.
#ifndef BUBBLEPASS_H #define BUBBLEPASS_H int bubblePass(int *, int) ; #endif /* BUBBLEPASS_H /*
Getting started
You can download a an incomplete project for MPLAB X to get started (and thus ignore all of that information above about how to set up the project). It has a file bubblepass.s that is the start of the MIPS assembly code.
If you would like to see how it runs in C, you can download a working C implementation as a NetBeans project to check your results.
How hard is this?
Well, not that hard, but you do need to be careful. First of all, since this is a leaf procedure, you don’t need to implement a stack frame.
My initial implementation used 23 instructions. Two of those were the
return instructions
jr $ra
nop
There were also three other nop instructions.
That’s only 18 non-nop instructions.
You can get the implementation down to 18 instructions with two
nop instructions. But that requires a little more work.
Hints
Every few days, I will post hints on the Homework 10 hints page.
Read these to keep on schedule.