Homework 7
C++, Assembler
and Macros

Modified
Homework 4 sorted an array, passing parameters to functions in registers.

Homework 6 followed the same algorithm but used pass-by-value and pass-by-reference.

Homework 7 combines C++, Assembler and Macros to implement Homework 6.

Assignment

An algorithm in  C++ is given below to perform a bubble-sort on an array and display the sorted array.

Implement the high-level bubbleSort algorithm as an Assembler language solution.

Use pass-by-reference for array parameter X and pass-by-value for scalar parameter N.

Define and use a macro to interchange the array entries, as is performed in the following:

               int temp=X[ j ];
               X[ j ]=X[ j+1 ];
               X[ j+1 ]=temp;

Use the interchange from HW6 as a pattern for the macro code, taking care not to destroy any necessary registers (e.g. use Push/Pop in the macro).

#include <iostream>
using namespace std;

extern "C" void bubbleSort(int X[], int N);

int A[] = { 400, 200, 100, 700, 500, 800, 600, 300 };
int B[] = { 5555, 3333, 8888, 1010, 7777, 1111, 6666, 9999, 2222, 4444 };

extern "C" void print( int X[], int N) {
  for(int i=0; i<N; i++)
	  cout << X[i] << "\n";
}

void bubbleSort( int X[], int N) { 
   for(int pass=1; pass < N; pass++) 
      for(int j=0; j < N-pass; j++)
         if(X[ j ] > X[ j+1 ])
         {
               int temp=X[ j ];
               X[ j ]=X[ j+1 ];
               X[ j+1 ]=temp;
         }
   print( X, N );
}    
int main()
{
  bubbleSort( A, 8);

  bubbleSort( B, 10);

  return 0; 
}

Turn in

  1. Cover sheet with your name, date, and Homework 7.
  2. Give program listing of solution (e.g. HW7.lst), see Hints on creating a listing file.

    Test output using both A and B array values given in the C++.

    To capture the output window:

    For documentation and as helpful reminder of algorithm when debugging assembly code, include pseudocode comments to the right of assembly instructions.

Hints

  1. Download the example from class notes as a starting project for mixed C++ and Assembler.
  2. The bubbleSort function of Homework 6 should require only two changes other than those necessary to make the function visible to a C++ program.
    1. Add the call to function print after the array is sorted. Having a sort function also print is not good design but provides an opportunity for Assembler to call a C++ function with a minimal changes to Homework 6.
    2. Add the macro to interchange array entries.
  3. The C++ program will need a prototype for the bubbleSort function.
  4. Create a listing file of the Assembler; this will show the macro expansions, a help in debugging.

    In Visual Studio:

    This will create a file named HW7.lst, containing a listing of the program and macro expansions.