COS 217, Spring 1996. Implementing Printf

COS 217. Introduction to Programming Systems. Spring 1996.

Implementing Printf

The goal of this assignment is to write a simplified version of printf in SPARC assembly code.

printf is called (from a C program) with a format string and its arguments:

    printf(format, arg_1, arg_2, ..., arg_n
format contains two kinds of characters: plain characters and formatting specifications. Plain characters are simply copied to the standard output. Formatting specifications control the printing of the arguments, arg_i, and are of the form %x where x is one of d, s, g, or %. The ith formatting specification specifies the formatting for arg_i.

The format argument is the address of the format string, and the format string is terminated by a null character (ascii code 0).

%d causes the corresponding argument to be interpreted as a 32-bit signed integer and printed in decimal.

%s causes the corresponding argument to be interpreted as the address of a character string, and the character string, which is terminated by a null character, is printed.

%g causes the corresponding argument to be interpreted as a D-format floating point (that is, 64 bits) and printed with 6 decimal places.

%% causes a percent sign to be printed.

Other formating codes are not associated with any argument and are simply printed as plain characters, for example, %x causes %x to be printed. If there are more arguments than formatting specifications, the extra arguments are ignored.

For example, the program

main()
   printf("this is a test\n");
   printf("%s [%d %x %d] %% %x\n", "this is a test", 35, -411);
   printf("%s = %g, r = %g\n", "sqrt(s)", 1.14121, -568.1452e12);
   printf("%%");
   printf("%s\n", "");

}
prints
this is a test
this is a test [35 %x -411] % %x
sqrt(s) = 1.41421, r = -5.68145e+14
%

Note that printf does not interpret escaped characters like \n; these are processed by the C compiler, which inserts the correct character code in the string. This test program is available in /u/cs217/6/test.c.

Converting Floating Point Values

Use the C library routine, gcvt, to convert a D-format floating point value to a string with 6 decimal places. gcvt is described in section 3 of the Unix manual; the manual page can be printed with the command man 3 gcvt. To convert a D-format value x into a null-terminated string in buffer, gcvt is called (in C) as follows:
    gcvt(x, 6, buffer)
The value of x is passed as the first argument, a second argument of 6 specifies six decimal places, and the address of the "conversion buffer" is passed as the third argument. When gcvt returns, buffer contains the string representation of x. buffer should be at least 20 bytes long.

You must call gcvt from assembly language using the calling sequence described in class. In assembly language, gcvt is called by referring to _gcvt.

Input/Output

You may not use any of the standard C i/o functions. You must do character output by making a write system call, which is accomplished by the instruction sequence:
mov 4, %g1
ta 0
Output must be sent to stdout, which is specified by passing the file descriptor value 1 as the first argument to the write system call.

In addition, you must write a recursive function to output a signed integer in decimal.

Submission

Submit your program electronically with the command:
/u/cs217/bin/submit 6 myprintf.s,v
where the myprintf.s,v is the RCS file for your implementation of printf.

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

Due: submitted by 11:59pm, Wed. 4/10.

Copyright (c) 1996 by Anne Rogers
Tue Apr 2 09:56:54 EST 1996