The UNIX operating system has a command named wc (word count). In its simplest form, wc reads characters from stdin until end-of-file, and prints to stdout a count of how many lines, words, and characters it has read. A "word" is a sequence of characters that is delimited by one or more "whitespace" characters. "Whitespace" is defined by the C standard function isspace. The program prints the three counts on the same line -- the line count in a field of width 7, the word count in a field of width 8, and the character count in a field of width 8.
Consider some examples. In the following, a space is shown as "s" and a newline character as "n".
If the file named "proverb" contains these characters:
Learningsissan treasureswhichn accompaniessitsn ownerseverywhere.n --sChinesesproverbn
then the command:
$ wc < proverb
prints this line to standard output:
If the file "proverb2" contains these characters:ssssss5ssssss12ssssss82n
Learningsissan treasureswhichn accompaniessitsn ownerseverywhere.n --sssChinesesproverb
(note that the last "line" does not end with a newline character) then the command:
$ wc < proverb2
prints this line to standard output:
ssssss4ssssss12ssssss83n
Another commonly used UNIX command is sort. In its simplest form, it reads lines from stdin, sorts them into ascending (i.e. alphabetical, i.e. lexicographic) order, and prints them to stdout. For example, if the file proverb contains these lines:
Learning is a treasure which accompanies its owner everywhere. -- Chinese proverb
then the command
$ sort < proverb
prints these lines to standard output:
-- Chinese proverb Learning is a accompanies its owner everywhere. treasure which
Note that the special character '-' has an ASCII code that is less than the ASCII codes of all alphabetic characters, and so the line that begins with '-' appears first. Also note that the uppercase characters have ASCII codes that are less than the ASCII codes of the lower case characters, and so the line that begins with 'L' appears before the line that begins with 'a'.
One small complication... For the Linux sort program to work in the traditional way (as described above), you must set the environment variable named "LC_ALL" such that its value is "C". Using the bash shell, you can accomplish that by issuing this command:
export LC_ALL=C
You will find it convenient to add that command to the end of the .bashrc file in your home directory. Having done so, bash will execute the command each time you login.
Your task is to create your own versions of the wc and sort commands in C and in IA-32 assembly language, as specified below.
Create a program named mywc. The program should implement the subset of wc described above. (The program need not process command-line arguments as wc does.) More precisely, create a C version of the program in file mywc.c and an assembly language version in file mywc.s.
Hint: A printf format string can contain "field widths." For example, the printf format string "%8d" indicates that the given integer should be printed in a field of width 8, padded with leading spaces.
It is acceptable to use global (i.e. bss section and data section) variables in mywc.s.
You should not make any assumptions about how many lines are in stdin. (Hint: You may find the sort4 program discussed in precepts useful as a model.) However, you may assume that no line of stdin contains more than 1023 characters; the terminating newline character is included in that count. Your program need not sort lines properly when a line of stdin contains more than 1023 characters, but it should not corrupt memory. (Hint: You will find the fgets function useful.)
One small complication... Since the ASCII code for the tab character ('\t') is less than the ASCII code for the newline character ('\n'), your mysort program erroneously might consider a line that begins with tab to be less than a line that contains no characters at all (that is, a line that contains only the newline character). Your mysort program should avoid that error by changing the newline character to the null character ('\0') in each line that it reads. (Hint: You will find it more convenient to call the puts function instead of the fputs function to write the lines to stdout.)
The program's primary data structure should be an array of pointers. Each pointer should point to the zero'th character of a null-terminated array of characters (that is, a string). Each string should contain the characters of one line of stdin. The program should dynamically allocate the memory occupied by each string. The amount of memory allocated for each string should depend upon the length of the string.There should be no memory leaks in the program. For each call of the malloc or calloc function, there should be a matching call of the free function.
The program should use these functions to sort the array of strings:
Those functions implement the quicksort algorithm as presented in the Sedgewick textbook./*------------------------------------------------------------------*/ void swap(char *ppcArray[], int iOne, int iTwo) /* Swap ppcArray[iOne] and ppcArray[iTwo]. */ { char *pcTemp; pcTemp = ppcArray[iOne]; ppcArray[iOne] = ppcArray[iTwo]; ppcArray[iTwo] = pcTemp; } /*------------------------------------------------------------------*/ int partition(char *ppcArray[], int iLeft, int iRight) /* Divide ppcArray[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 (strcmp(ppcArray[++iFirst], ppcArray[iRight]) < 0) ; while (strcmp(ppcArray[iRight], ppcArray[--iLast]) < 0) if (iLast == iLeft) break; if (iFirst >= iLast) break; swap(ppcArray, iFirst, iLast); } swap(ppcArray, iFirst, iRight); return iFirst; } /*------------------------------------------------------------------*/ void quicksort(char *ppcArray[], int iLeft, int iRight) /* Sort ppcArray[iLeft...iRight] in ascending order. */ { if (iRight > iLeft) { int iMid = partition(ppcArray, iLeft, iRight); quicksort(ppcArray, iLeft, iMid - 1); quicksort(ppcArray, iMid + 1, iRight); } } /*------------------------------------------------------------------*/
More precisely, create these files:
Note that you need not create a mysort.s file.
Somewhat unrealistically, you need not create C header (.h) files. Instead, it is sufficient to place explicit function declarations in each .c file as necessary.
Your source code files should be such that you can build the mysort program using mysort.c along with either quicksort.c or quicksort.s, either partition.c or partition.s, and either swap.c or swap.s.
Develop a UNIX shell script named grade4 and some associated data files to build and execute your programs. A UNIX shell script is simply a text file that contains UNIX commands, and that has been made executable via the chmod command:
chmod 700 grade4
Describe your testing strategy in your readme file.
It is acceptable for your grade4 script to call other scripts that you create.
You should not use a C compiler to produce your assembly language programs. Doing so would be considered an instance of academic dishonesty. Rather you should produce your assembly language programs manually.
We encourage you to develop "flattened C" code (as described in precepts) to bridge the gap between your "normal" C code and your assembly language code. Using flattened C code as a bridge can eliminate logic errors from your assembly language code, leaving only the possibility of translation errors.
We also encourage you to use your flattened C code as comments in your assembly language code. Such comments can dramatically enhance the understandability of your assembly language code.
You should submit:
Your readme file should contain:
Submit your work electronically via the commands:
/u/cos217/bin/i686/submit 4 mywc.c mywc.s /u/cos217/bin/i686/submit 4 mysort.c quicksort.c quicksort.s partition.c partition.s swap.c swap.s /u/cos217/bin/i686/submit 4 grade4 yourothershellscripts yourdatafiles /u/cos217/bin/i686/submit 4 readme
Comments in your assembly language programs are especially important. Each assembly language function (especially the "main" function) should have a comment that describes what the function does. Local comments within your assembly language functions are equally important. Comments copied from corresponding "flattened C" code are particularly helpful.
Testing is a substantial aspect of the assignment. Approximately 15% of the grade will be based upon your testing strategy, scripts, and data files.
To encourage good coding practices, we will take off points based on warning messages during preparation of your programs.