9.2 Keyboard and Display I/OA standard system-supplied C library provides functions to perform text-oriented I/O from the standard input (stdin), normally the keyboard, and to the standard output (stdout), normally the display. Errors are sent to the standard error (stderr), which may be the display, a file, or the null device. Some of those functions may be provided in "macro form" (see Kernighan), which is entirely inside the <stdio.h> header file that is compiled with almost every C program. This is why, in Section 6.7, we first compiled getchar and putchar on their own before linking them with some of our assembly language programs. Other functions may be provided in system libraries that the linker can match according to the name of the external calls that we express in an Itanium assembly language program. Functions that we intend to use in this chapter are for access to stdin, stdout, and to text files. Calling conventions for selected functions are set forth in Table 9-1. Table 9-1 presents the function interfaces from a register-level programming perspective, rather than their C data types, using the calling conventions of Unix and Linux. We include a brief description of any useful value returned in register out0 (r8), but leave detailed return codes to the man reference pages in order to minimize the amount of material in this book unrelated to architectural concepts. Each approach to I/O, be it through the operating system or a language library, has its range of applicability, strengths, and weaknesses. Working with one byte at a time is the most obvious, but also requires the most work by the programmer. The provided routines offer conveniences such as easy handling of newline characters, data conversions, and end of file detection.
9.2.1 Unformatted Line I/OApplications involving traditional "dumb terminals" and text files are typically line-oriented. That is, the conceptual unit of input or output is a line of some n characters. The standard C library meets this need with the gets and puts functions, which move an entire line of ASCII characters between "the system" or the external environment and some particular storage region managed by the calling process. With gets, the storage must be large enough to accommodate all the characters plus an ASCII NUL character, which indicates the end of line. With puts, a null-terminated ASCII string has a newline character appended before being moved to the external environment. The advantage of gets over getchar and puts over putchar is the elimination of byte-handling instructions in an input loop, while retaining a processing loop that deals with each line. The programmer can thus concentrate more on a particular algorithm and less on the details of handling I/O. 9.2.2 Formatted I/OAnyone who has written one-off programs appreciates the convenience of the formatted I/O methods provided by high-level languages, such as the print statement in BASIC or the printf function in C. These are especially convenient as they allow a range of output from quick-and-rough to carefully formatted. Generally, printf expects integer and floating-point quantities to be passed by value. Itanium environments pass the first seven input arguments in the stacked registers out1-out7; floating-point quantities in those registers must be converted to memory format. Subsequent quantities are passed by value in memory, being placed in the expected region of the stack. Output of a string is by address, pointing to a location in memory. Note that the printf function allows and requires explicit placement of newline characters in the format string and arguments. Failure to include a final newline character can result in lost output. Formatted input is provided by the functional inverse scanf. A single call to scanf may work through numerous newline markers to satisfy the total number of objects specified by the format string. Individual words of text (including adjacent punctuation marks) can be read by scanf with the "%s" parameter, using any space, tab, or newline as a field terminator. Refer to books or manuals on ANSI C for more information on the format strings used by printf and scanf. |