Princeton University
COS 217: Introduction to Programming Systems

Assignment 5: The UNIX "echo" and "wc" Commands in SPARC Assembly Language

Purpose

The purpose of this assignment is to help you learn about SPARC architecture and assembly language programming. It will also give you the opportunity to learn more about the GNU/UNIX programming tools, especially bash, Emacs, gcc, gdb (for assembly language programs), and make.

Background

The UNIX operating system has a command named echo. echo prints its command-line arguments to standard output. It prints a single space between each argument, and prints a newline character at the end of the argument sequence. If there are no arguments, echo prints only a newline character. 

Consider some examples. If we show a space as "s" and a newline character as "n", then the command:

--> echosonestwosthreesfoursfiven
prints this line to standard output:
onestwosthreesfoursfiven
The command:
--> echosones"twosssthree"sfoursssfiven
prints this line to standard output:
onestwosssthreesfoursfiven
The command:
--> echos%sn
prints this line to standard output:
%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:

sssssss5ssssss12ssssss82n
If the file "proverb2" contains these characters:
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

Your Task

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.

echo1

Create a C program in a file named echo1.c, from which gcc can produce an executable file named echo1. echo1 should have the same functionality as echo.

echo2

Create a SPARC assembly language program in a file named echo2.S, from which gcc can produce an executable file named echo2. echo2 should have the same functionality as echo.

wc1

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.)

wc2

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.

Logistics

You should develop on arizona. Use Emacs to create source code. Use the "make" tool to automate the build process. Use gdb to debug.

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

Grading

We will grade your work on functionality and design. As always, we will consider understandability to be an important aspect of good design. We will place particular emphasis on the comments in your programs, especially the assembly language ones. To encourage good coding practices, we will take off points based on warning messages during compilation of your C programs.