9.2 Keyboard and Display IO

9.2 Keyboard and Display I/O

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

Table 9-1. Calling Conventions for C Functions Related to stdin and stdout

Function

Argument

Register(s)

Description

puts

first

out0

Address of a null-terminated string.

 

returned

ret0[*]

Any non-negative value indicates success.

gets

first

out0

Address of a sufficient storage area.

 

returned

ret0[]

Copy of out0 if successful, or the value zero if any error occurred.

printf

first

out0

Address of a string with format information.

 

other(s)

out1 to out7 (then stack)

Integer or floating-point (memory-format) quantities to be formatted according to the format string, or the address for any string quantities.

 

returned

ret0[*]

Total number of bytes written (or various negative codes indicating error conditions).

scanf

first

out0

Address of a string with format information.

 

other(s)

out1 to out7 (then stack)

Address(es) of integer, floating-point, or string quantities interpreted according to the format string.

 

returned

ret0[*]

Number of input objects processed successfully, or EOF if any error occurred.

perror

first

out0

Address of a string containing a comment.

 

returned

 

Prints a system-dependent error message onto stderr (meaningful only after an actual error).

[*] Test this returned value as a double word (int in C).

[] Test this returned value as an address (quad word for Linux and for HP-UX with +DD64 compiler option, or double word for HP-UX without +DD64 compiler option).

9.2.1 Unformatted Line I/O

Applications 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/O

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



ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ItaniumR Architecture for Programmers. Understanding 64-Bit Processors and EPIC Principles
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net