Princeton University
COS 217: Introduction to Programming Systems

Assignment 4: SPARC Assembly Language "echo"

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

As you may know, the UNIX operating system has a command named "echo."  Its job is to print its command-line arguments to stdout.  Each argument is followed by a single space.  For example, the command
echo one two three    four five
writes this line to stdout:
one two three four five
and the command:
echo one "two   three" four   five
writes this line to stdout:
one two   three four five
Your job in this assignment is to create several versions of the echo command in SPARC assembly language.

Part 1: echop

In Part 1 your task is to create a program named echop.  The echop program should be a version of echo that calls functions from the standard C library (typically printf) to print its command-line arguments.

Part 2: echow

In Part 2 your task is to create echow.  The echow program should do the same job as echop, but should not call any functions that are defined in the standard C library (e.g. printf, strlen).  Instead it should call the UNIX-specific "write" system function.

Part 3:  echos

In Part 3 you should create a program named echos.  It should be the same as echop, except that it should print its arguments in sorted (i.e. alphabetic, i.e. lexicographic) order.  Thus its functionality should be somewhat different from that of the standard echo command.

For example, the command:

echos one two three four   five
should print this to stdout:
five four one three two
The echos program should sort its command-line arguments by calling assembly language versions of these functions:
void swap(char *v[], int i, int j)
{
   char *temp;
   temp = v[i];
   v[i] = v[j];
   v[j] = temp;
}

void qsort(char *v[], int left, int right)
{
   int i, last;
   if (left >= right)
      return;
   swap(v, left, (left + right) / 2);
   last = left;
   for (i = left+1; i <= right; i++)
      if (strcmp(v[i], v[left]) < 0)
         swap(v, ++last, i);
   swap(v, left, last);
   qsort(v, left, last-1);
   qsort(v, last+1, right);
}
They are are taken from p. 110 of the book The C Programming Language (Kernighan and Ritchie, Prentice Hall, Murray Hill, NJ, 1988).  An important aspect of this assignment is that you hand-translate those functions into assembly language and use them to create your echos program.  You should implement the swap function as a leaf subroutine, as defined in your Paul textbook in Section 7.9.  Hint:  Remember that multiplication and division by powers of 2 can be implemented using shift instructions, and that doing so is more convenient and efficient than using the ".mul" and ".div" functions.

Logistics

We suggest that you first write your programs in the C programming language, and then hand-translate them into SPARC assembly language.  In accord with the purpose of the assignment, you should not use a C compiler to produce the assembly language programs that you submit.  Rather you should produce them manually.  

We suggest that you not use the m4 preprocessor.  If you find it helpful to define symbolic constants, we suggest that you use the C preprocessor.

You should comment your programs heavily.

You need not optimize your programs by minimizing memory accesses, eliminating "nop" instructions, using annul bits, etc.

You should develop on arizona using the bash shell. Use Emacs to create source code. Use the "make" tool to automate the build process; define your makefile so it generates executable files named echop, echow, and echos. Use gdb to debug.

You should create five files for submission:

Submit your work electronically via the command "/u/cs217/bin/submit 4 echop.S echow.S echos.S makefile readme".

Grading

We will grade your work on correctness, design, and understandability.