COS 217. Introduction to Programming Systems. Spring 1999.

Assignment 5: Implementing "tail" in SPARC Assembly Language

Write a program, in SPARC assembly language, to print the ``tail'' of the file, that is, the last 22 lines of the file. If there are fewer than 22 lines in the file, print the entire file. Your program can take a filename as a command-line argument, but if there is no filename specified, your program is supposed read from the standard input. Usage: mytail [filename]

There are several ways to write tail. Since we want you to gain familiarity with assembly language, you MUST use the following algorithm. Read the entire input, keeping the last 4096 characters read in a buffer. Thus, at the end of the input, the buffer will contain the last 4096 characters (or less) of the input, or the entire input. The last 22 lines can be found by scanning backwards over the buffer looking for newline characters (character code 012), and these lines can be printed in a final forward scan through the buffer.

It's most efficient to maintain the buffer as a circular buffer. That is, when it fills up, subsequent input is placed at the front of the buffer. This implies the following. Suppose the buffer is represented as an array, buffer[0:4095], and the length of the input is greater than 4096. Then, when you have read the last bit of input, the tail of the input is in buffer[j:4095] followed by buffer[0:k], where k<j. If the length of the input, l, is less than 4096, the tail is in buffer[0:l-1].

Make sure your program handles boundary conditions correctly, for example, input with exactly 22 lines and 4096 characters, input with no characters, etc.

Program Layout

The general form for an assembly language program is:

.seg "bss"
 global data -- initial value of zero

.seg "data"
 global data

.seg "text"
.global _main
.align 4
.proc 4
_main: save %sp,  WINDOWSIZE ,%sp

       program body

       ret; restore

Assembler directives for both initialized and uninitialized data can appear in the global data area. For example, a 4096-byte buffer, called _buf, can be allocated with .common _buf, 4096. See SPARC Architecture, Assembly Language Programming, & C for syntactic details.

Execution begins by calling the procedure _main and terminates when _main returns via ret and restore instructions. To assemble, link, and load an assembly language program in a file, say, mytail.s, use the command:

lcc -o mytail -g mytail.s

lcc will call the assembler and linker for you. The -v option to lcc will show the commands that lcc executes.

Style

Put lots of comments in your program; one per line is often appropriate. But do not just restate what the assembly language says, explain it.

Note: You are not allowed to use a compiler (gcc, lcc, or whatever) to produce assembly code for this assignment. The assembly must be coded by hand "from scratch".

Submission

Submit your program electronically with the command:

/u/cs217/bin/submit 5 mytail.s,v

where the mytail.s,v is the RCS file for your your implementation of tail.

Make sure your RCS file holds the latest revision when you submit it.

Due: submitted by 11:59pm, Monday, April 5, 1999.


Copyright (c) 1996 by Anne Rogers, J.P. Singh