/*-------------------------------------------------------------------*/ /* testsortinline.c */ /* Inline assembly language. */ /*-------------------------------------------------------------------*/ #include #include #include #define MAX_SIZE 10 /*-------------------------------------------------------------------*/ void fillArray(int piArray[], int iSize) /* Fill piArray with iSize random integers. */ { int i; for (i = 0; i < iSize; i++) piArray[i] = rand(); } /*-------------------------------------------------------------------*/ void swap(int piArray[], int iOne, int iTwo) /* Swap piArray[iOne] and piArray[iTwo]. */ { int iTemp; iTemp = piArray[iOne]; piArray[iOne] = piArray[iTwo]; piArray[iTwo] = iTemp; /* __asm__ __volatile__ ( "pushl $0\n\t" "movl 8(%%ebp), %%eax\n\t" "movl 12(%%ebp), %%ecx\n\t" "movl (%%eax, %%ecx, 4), %%ecx\n\t" "movl %%ecx, -4(%%ebp)\n\t" "movl 8(%%ebp), %%eax\n\t" "movl 16(%%ebp), %%ecx\n\t" "movl (%%eax, %%ecx, 4), %%ecx\n\t" "movl 12(%%ebp), %%edx\n\t" "movl %%ecx, (%%eax, %%edx, 4)\n\t" "movl -4(%%ebp), %%ecx\n\t" "movl 8(%%ebp), %%eax\n\t" "movl 16(%%ebp), %%edx\n\t" "movl %%ecx, (%%eax, %%edx, 4)\n\t" : : : "%eax", "%ecx", "%edx" ); */ /* int iTemp; __asm__ __volatile__ ( "movl (%0, %1, 4), %%ecx\n\t" "movl %%ecx, %3\n\t" "movl (%0, %2, 4), %%ecx\n\t" "movl %%ecx, (%0, %1, 4)\n\t" "movl %3, (%0, %2, 4)\n\t" : : "r"(piArray), "r"(iOne), "r"(iTwo), "r"(iTemp) : "%ecx" ); */ /* __asm__ __volatile__ ( "" :"=c"(piArray[iOne]), "=a"(piArray[iTwo]) :"a"(piArray[iOne]), "c"(piArray[iTwo]) ); */ /* __asm__ __volatile__ ( "" :"=r"(piArray[iOne]), "=r"(piArray[iTwo]) :"0"(piArray[iTwo]), "1"(piArray[iOne]) ); */ } /*-------------------------------------------------------------------*/ int partition(int piArray[], int iLeft, int iRight) /* Divide piArray[iLeft...iRight] into two partitions so elements in the first partition are <= elements in the second partition. Return the index of the element that marks the partition boundary. */ { int iFirst = iLeft-1; int iLast = iRight; while (1) { while (piArray[++iFirst] < piArray[iRight]) ; while (piArray[iRight] < piArray[--iLast]) if (iLast == iLeft) break; if (iFirst >= iLast) break; swap(piArray, iFirst, iLast); } swap(piArray, iFirst, iRight); return iFirst; } /*-------------------------------------------------------------------*/ void quicksort(int piArray[], int iLeft, int iRight) /* Sort piArray[iLeft...iRight] in ascending order. */ { if (iRight > iLeft) { int iMid = partition(piArray, iLeft, iRight); quicksort(piArray, iLeft, iMid - 1); quicksort(piArray, iMid + 1, iRight); } } /*-------------------------------------------------------------------*/ void printArray(int piArray[], int iSize) /* Print the iSize elements of piArray to stdout. */ { int i; for (i = 0; i < iSize; i++) printf("%d\n", piArray[i]); } /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* Fill an array of MAX_SIZE random integers, sort the integers in ascending order, and print the integers to stdout. */ { int piArray[MAX_SIZE]; fillArray(piArray, MAX_SIZE); quicksort(piArray, 0, MAX_SIZE - 1); printArray(piArray, MAX_SIZE); return 0; }