Hardware Reference
In-Depth Information
Nr
Name
Arguments
Return value
Description
5
OPEN
*name, 0/1/2
file descriptor Open file
8
CREAT
*name, *mode
file descriptor
Create file
3
READ
fd, buf, nbytes
# bytes
Read nbytes in buffer buf
4
WRITE
fd, buf, nbytes
# bytes
Write nbytes from buffer buf
6
CLOSE
fd
0 on success
Close file with fd
19
LSEEK
fd, offset(long), 0/1/2 position (long) Move file pointer
1
EXIT
status
Close files Stop process
117
GETCHAR
read character Read character from std input
122
PUTCHAR
char
write byte
Write character to std output
127
PRINTF
*format, arg
Print formatted on std output
121
SPRINTF
buf, *format, arg
Print formatted in buffer buf
125
SSCANF
buf, *format, arg
Read arguments from buffer buf
Figure C-7. Some UNIX system calls and subroutines in the interpreter.
These twelve routines can be activated by the standard calling sequence; first
push the necessary arguments on the stack in reverse order, then push the call num-
ber, and finally execute the system trap instruction SYS without operands. The sys-
tem routine finds all the necessary information on the stack, including the call
number of the required system service. Return values are put either in the AX regis-
ter, or in the DX : AX register combination (when the return value is a long).
It is guaranteed that all other registers will keep their values over the SYS
instruction. Also, the arguments will still be on the stack after the call. Since they
are not needed any more, the stack pointer should be adjusted after the call (by the
caller), unless they are needed for a subsequent call.
For convenience, the names of the system calls can be defined as constants at
the start of the assembler program, so that they can be called by name instead of by
number. In the examples, several system calls will be discussed, so in this section
only a minimum of necessary detail is supplied.
In these system calls, files are opened either by the OPEN or by the CREAT call.
In both cases, the first argument is the address of the start of a string containing the
file name. The second argument in the OPEN call is either 0 (if the file should be
opened for reading), 1 (if it should be opened for writing), or 2 (for both). If the
file should allow writes, and does not exist, it is created by the call. In the CREAT
call an empty file is created, with permission set according to the second argument.
Both the OPEN and the CREAT call return a small integer in the AX register, which
is called the file descriptor and which can be used for reading, writing or closing
the file. A negative return value means the call failed. At the start of the program,
three files are already opened with file descriptors: 0 for standard input, 1 for stan-
dard output, and 2 for standard error output.
The READ and WRITE calls have three arguments: the file descriptor, a buffer to
hold the data, and the number of bytes to transfer. Since the arguments are stacked
 
Search WWH ::




Custom Search