Consider some examples. If we show a space as "s" and a newline character as "n", then the command:
prints this line to standard output:--> echosonestwosthreesfoursfiven
The command:onestwosthreesfoursfiven
prints this line to standard output:--> echosones"twosssthree"sfoursssfiven
The command:onestwosssthreesfoursfiven
prints this line to standard output:--> echos%sn
%sn
Another UNIX command is wc (word count). In its simplest form, wc reads characters from standard input until end-of-file, and prints to standard output a count of how many lines, words, and characters it has read. It prints the three counts on the same line, each in a field of width 8. A "word" is a sequence of characters that is delimited by one or more whitespace characters, as defined by the C standard function isspace.
For example, 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:sssssss5ssssss12ssssss82n
Learningsissan treasureswhichn accompaniessitsn ownerseverywhere.n --sChinesesproverb
(note that the last "line" does not end with a newline character) then the command:
--> wc < proverb2
prints this line to standard output:
sssssss4ssssss12ssssss81n
This assignment asks you to create your own versions of the echo and wc commands in C and in SPARC assembly language, as specified below.
Create a C program in a file named wc1.c, from which gcc can produce an executable file named wc1. wc1 should implement the subset of wc described above. (wc1 need not process command-line arguments as wc does.)
Create a SPARC assembly language program in a file named wc2.S, from which gcc can produce an executable file named wc2. Like wc1, wc2 should implement the subset of wc described above.
Hint: The program should read one character at a time from standard input. The C standard library provides several functions/macros that can read a single character: getchar, getc, fgetc, and scanf (with the "%c" format string). On arizona, getchar and getc are implemented as macros; thus it is impossible to call them from an assembly language program. On arizona, fgetc is implemented as a function, and so it is possible to call it from assembly language. But fgets demands that you pass stdin as an actual parameter, and that is awkward to implement in assembly language. Thus, the best option is to call the scanf function.
In accord with the purpose of the assignment, you should not use a C compiler to produce your assembly language programs. Rather you should produce them manually.
We suggest that you not use the m4 preprocessor. We suggest that you use the C preprocessor to define symbolic constants.
You need not optimize your assembly language programs, but we encourage you to do so. In particular, we encourage you to use registers instead of memory whenever possible. We will give you extra credit -- up to 5% -- if you minimize the use of "nop" instructions and optimize "if," "while," and "for" constructs.
You should submit:
Your readme file should contain:
Submit your work electronically via the command:
/u/cs217/bin/submit 5 echo1.c echo2.S wc1.c wc2.S makefile readme