Assignment #3

Driver program:

/////////////////////////////////////////////////////////////////////
//
//   File: assign3.cpp
//   Author: Michael Lack
//   Description:  Sample solution driver for assignment 3
//
////////////////////////////////////////////////////////////////////
 

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
 
 

//function prototype
extern "C" void divide(int Input[], int InputSize, int OutLow[],
         int& OutLowSize, int OutHigh[], int barrier);
 

main(int argc, char *argv[])
{

 int input[128];  //input array
   int lower[128];  //lower array
   int higher[128]; //upper array
   int size = 0;    //input size
   int lowsize = 0; //lower size
   int barrier;     //barrier value
 

 //check to make sure that all command line arguments were properly passed
 if (argc != 3)
   {
    cerr << "Program usage: assign3 filename barrier" << endl;
      exit(1);
   }

 //parse the barrier value
   barrier = atoi(argv[2]);

 //open the input file
   ifstream fin(argv[1]);

 //if the file cannot be opened, terminate
   if (!fin)
   {
    cerr << "Could not open " << argv[1] << endl;
      exit(1);
   }
 

 //read in the input data
   while (!fin.eof())
   {
    fin >> input[size++];
   }
 

   //close the input file
   fin.close();

 //call the routine
   divide(input,size,lower,lowsize,higher,barrier);

 //display the initial input
   cout << "Input Vector:" << endl;
   for (int i=0; i<size; i++)
    cout << input[i] << endl;

 //display the lower array
   cout << "\nNumbers less than " << barrier << ":" << endl;
   for (int i=0; i<lowsize; i++)
    cout << lower[i] << endl;

 //display the upper array
   cout << "\nNumbers greater than or equal to " << barrier << ":" << endl;
   for (int i=0; i<(size-lowsize); i++)
    cout << higher[i] << endl;

 //all finished
   return 0;
}


Assembly Language Routine:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;   File: divide.asm
;;   Author: Michael Lack
;;   Description: sample solution for the divide routine
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 

;assembler directives
.486
MODEL FLAT
.CODE
PUBLIC _divide
_divide PROC

;standard prologue
push ebp
mov ebp, esp
push edi
push esi
push edx
push ecx
push ebx
push eax

;load input parameters
mov eax, [ebp+8]         ;Input[]
mov ebx, [ebp+16]        ;OutLow[]
mov ecx, [ebp+24]        ;OutHigh[]
mov edx, [ebp+20]        ;&OutLowSize
mov edi, [ebp+12]        ;InputSize
 

;main loop
loop:  cmp edi,0         ;check to see if processed all of input
       je done           ;if so, break out of loop
       mov esi, [eax]    ;move element from Input[] into esi
       add eax, 4        ;advance through input
       dec edi           ;decrement the counter
       cmp esi, [ebp+28] ;compare with barrier
       jl less           ;if less than, jump to less
       mov [ecx], esi    ;otherwise, copy element to OutHigh
       add ecx, 4        ;advance pointer to OutHigh
       jmp loop          ;go back to top of loop

less:  mov [ebx], esi    ;copy element to OutLow
       inc [edx]         ;increment contents of memory location stored in edx
       add ebx, 4        ;advance pointer to OutLow
       jmp loop          ;go back to top of loop

;standard epilogue
done:  pop eax
       pop ebx
       pop ecx
       pop edx
       pop esi
       pop edi
       mov esp, ebp
       pop ebp
       ret

ENDP _divide
END


Sample Output:

Input Vector:
6
-45
-4
0
909
-33
-769
71756
-9009
4
33
6
793
-793
0
6
0

Numbers less than -4:
-45
-33
-769
-9009
-793

Numbers greater than or equal to -4:
6
-4
0
909
71756
4
33
6
793
0
6
0