Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
96
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part III • Working with Others

Table 11.3. MS-DOS Int 21 function codes.

Function

Function

 

DOS

(hex)

(decimal)

Description

version

00H

0

Terminate the current

1.0+

 

 

program or process.

 

01H

1

Read a character from

1.0+

 

 

the console (with echo

 

 

 

to screen).

 

02H

2

Write a character to the

1.0+

 

 

screen.

 

03H

3

04H

4

05H

5

06H

6

Read a character from AUX:.

1.0+

Write a character to AUX:.

1.0+

Send output to the printer.

1.0+

Perform I/O from the

1.0+

console; DOS does not

 

process the characters.

 

07H 7

08H 8

09H 9

0AH 10

Get a character from the

1.0+

keyboard, waiting for a

 

keypress if no character

 

is available; the character

 

is not processed by DOS.

 

Get a character from the

1.0+

keyboard, waiting for a

 

keypress if no character

 

is available.

 

Print a string to the

1.0+

console; the string is

 

terminated with a dollar

 

sign.

 

Read a character string

1.0+

(a line, typed by the

 

user, up to a carriage

 

return) from the keyboard.

 

456

 

 

 

C and Other Languages

 

C C C

 

 

 

 

 

11C

 

 

 

 

 

C C C

 

 

 

 

 

C C

 

 

 

 

 

 

 

Function

Function

 

DOS

 

 

 

(hex)

(decimal)

Description

version

 

0BH

11

Test to see whether a

1.0+

 

 

 

 

 

 

character is available

 

 

 

 

 

 

 

from the keyboard.

 

 

 

 

 

0CH

12

Discard the contents of

1.0+

 

 

 

 

 

 

the input buffer, and get

 

 

 

 

 

 

 

the input.

 

 

 

 

 

0DH

13

Reset the specified disk

1.0+

 

 

 

 

 

 

drive.

 

 

 

 

 

0EH

14

Make the specified drive

1.0+

 

 

 

 

 

 

the current drive.

 

 

 

 

0FH

15

10H

16

11H

17

Open a file.

1.0+

Close a file.

1.0+

Find the first file meeting

1.0+

the provided specification.

 

12H

18

Find the next file after

1.0+

 

 

using Int 0x11.

 

13H

19

14H

20

15H

21

16H

22

17H

23

18H

24

19H

25

1AH

26

1BH

27

Delete the specified file.

1.0+

Perform a sequential read.

1.0+

Perform a sequential write.

1.0+

Create a new file.

1.0+

Rename a file.

1.0+

Reserved.

 

Get the current drive.

1.0+

Set the Disk Transfer

1.0+

Address (DTA).

 

Get the current default

1.0+

drive data.

 

continues

457

Part III • Working with Others

Table 11.3. continued

Function

Function

 

DOS

(hex)

(decimal)

Description

version

1CH

28

Get data for the specified

2.0+

 

 

drive.

 

1DH

29

1EH

30

1FH

31

20H

32

21H

33

22H

34

23H

35

Reserved

 

Reserved

 

Reserved

 

Reserved

 

Random file read.

1.0+

Random file write.

1.0+

Get the size of the

1.0+

specified file.

 

24H

36

Set the file’s current

1.0+

 

 

position.

 

25H

37

26H

38

27H

39

28H

40

29H

41

2AH

42

2BH

43

2CH

44

2DH

45

2EH

46

2FH

47

Set an interrupt vector.

1.0+

Create a new Program

1.0+

Segment Prefix (PSP).

 

Random block read.

 

Random block write.

1.0+

Parse a filename into a

1.0+

valid DOS filename

 

Get the date.

1.0+

Set the date.

1.0+

Get the time.

1.0+

Set the time.

1.0+

Set the write verify flag.

1.0+

Get the Disk Transfer

2.0+

Address (DTA).

 

458

 

 

C and Other Languages

 

C C C

 

 

 

 

11C

 

 

 

 

C C C

 

 

 

 

C C

 

 

 

 

 

 

Function

Function

DOS

 

 

 

(hex)

(decimal) Description

version

 

30H

48

31H

49

Get the DOS version

2.0+

Terminate-and-Stay-Resident

2.0+

(TSR).

 

32H

50

33H

51

Reserved

 

Get or set the break flag,

2.0+

and get the boot drive.

 

34H

52

35H

53

36H

54

Reserved

 

Get the interrupt vector.

2.0+

Get the drive allocation

2.0+

information.

 

39H

57

3AH

58

3BH

59

3CH

60

3DH

61

3EH

62

3FH

63

40H

64

41H

65

42H

66

43H

67

44H

68

45H

69

46H

70

Create a directory.

2.0+

Delete a directory.

2.0+

Set the current directory.

2.0+

Create a file.

2.0+

Open a file.

2.0+

Close a file.

2.0+

Read from a file (can also

2.0+

read from a device).

 

Write to a file (can also

2.0+

read from a device).

 

Delete a file.

2.0+

Set the file pointer.

2.0+

Get (or set) a file’s

2.0+

attributes.

 

IOCTL processing.

2.0+

Duplicate a file handle.

2.0+

Redirect a file handle.

2.0+

continues

459

Part III • Working with Others

Table 11.3. continued

Function

Function

 

DOS

(hex)

(decimal)

Description

version

47H

71

48H

72

49H

73

4AH

74

4BH

75

Get the current directory.

2.0+

Allocate a memory block.

2.0+

Release a memory block.

2.0+

Resize a memory block.

2.0+

Execute (run) a program

2.0+

(EXEC).

 

4CH

76

Terminate a process with

2.0+

 

 

a return code (which can

 

 

 

be tested in a batch file).

 

4DH

77

Get the return code from

2.0+

 

 

a child process.

 

4EH

78

4FH

79

Find the first file.

2.0+

Find the next file, after

2.0+

finding the first file.

 

50H

80

51H

81

52H

82

53H

83

54H

84

55H

85

56H

86

57H

87

58H

88

Reserved

 

Reserved

 

Reserved

 

Reserved

 

Get the verify flag.

2.0+

Reserved

 

Rename a file.

2.0+

Get (or set) the file

2.0+

date and time.

 

Get (or set) the

3.0+

allocation strategy.

 

460

 

 

 

C and Other Languages

 

C C C

 

 

 

 

 

11C

 

 

 

 

 

C C C

 

 

 

 

 

C C

 

 

 

 

 

 

 

Function

Function

 

DOS

 

 

 

(hex)

(decimal)

Description

version

 

59H

89

Get extended error

3.0+

 

 

 

 

 

 

information following a

 

 

 

 

 

 

 

DOS error.

 

 

 

 

5AH

90

5BH

91

5CH

92

Create a temporary file.

3.0+

Create a new file.

3.0+

Lock (or unlock) a file

3.0+

region.

 

5DH

93

5EH

94

Reserved

 

Get the machine name, get

3.1+

(or set) the printer setup.

 

5FH

95

60H

96

61H

97

62H

98

Device redirection.

3.1+

Reserved

 

Reserved

 

Get the Program Segment

3.0+

Prefix (PSP) address.

 

63H

99

64H

100

65H

101

66H

102

67H

103

68H

104

69H

105

6AH

106

6BH

107

6CH

108

Get the DBCS lead byte

2.25

only table.

 

Reserved

 

Get the extended country

3.3+

information.

 

Get (or set) the code page.

3.3+

Set the file handle count.

3.3+

Commit a file.

3.3+

Reserved

 

Reserved

 

Reserved

 

Extended open file.

4.0+

461

Part III • Working with Others

Calling C from FORTRAN and Pascal

A C function is called from a FORTRAN or a Pascal program in a manner similar to the way any other type of function is called from FORTRAN or Pascal. The main program must be told that the function is written in C (so that the parameters are passed properly), and C library functions must not be called.

Because C is not the language of the main program, the C initialization code cannot be called, which means the C library functions cannot be called. This restriction can limit the usefulness of a C function; you may decide that it is easier to write the entire program in one language.

All the Things that Can Go Wrong

When you mix languages in your programming, you can easily get things mixed up. Much of the discussion in this section is specific to Microsoft’s C compilers. However, other compilers often behave in a similar manner. Borland’s C compilers, for example, offer similar methods for argument passing.

Following are some of the more common things that can ruin your day:

If you are calling a C function from another language, you must tell the compiler the calling convention. You can tell C that a function will be using FORTRAN calling conventions, then call the function from a FORTRAN program without telling the FORTRAN program that the function is written in C. If the function is written in C with C’s calling conventions, however, the calling program must know this.

When assembly is called from C, the parameters must be read from the stack in the correct order. A C function expects its arguments to be passed in right to left order. A FORTRAN or a Pascal function expects arguments in a left to right order.

A function using the C calling conventions expects its caller to clean the arguments from the stack. A FORTRAN or a Pascal function takes the arguments from the stack itself.

The value being returned must be in the correct place. Generally, AX is used for 2-byte return values, and AX and DX are used for 4-byte return values.

462

C and Other Languages

C C C

 

11C

 

C C C

 

C C

The compiler modifies the function’s name. If it is a C function, an underscore is added (either before or after the name, depending on the compiler) and its case is not changed. If it is a FORTRAN or a Pascal function, an underscore is not added and the name is converted to uppercase. When calling C functions from assembly, the underscore is often forgotten, leading to unresolved references.

When accessing a multidimensional array, C and Pascal vary the subscripts in row-major order. FORTRAN and BASIC vary them in column-major order.

The C array nArray[2][10][20] is typically nArray(20,10,2) in

FORTRAN.

In C, arrays are indexed from zero. In FORTRAN, arrays typically are indexed from one. In Pascal, the lower bound is specified by the programmer. Exceeding the bounds of an array is common in mixed C/FORTRAN/ Pascal code because the initial starting points for the bounds differ.

Do not call C library code from a program that has a main procedure written in FORTRAN or Pascal.

Do not call library functions from a function written in a language different from the language of the main program. It may be possible to call the main program language’s library functions, but make sure that you use the correct calling conventions.

In all, mixed language programming is not used frequently. It is complex and prone to subtle failures that may be difficult to find and correct. Mixed language programming should be used as a last resort.

Looking at Data

Each programming language views data a little differently. Table 11.4 is a crossreference of simple data types used in C, BASIC, FORTRAN, and Pascal. The similarity in the data types of FORTRAN and Pascal shows the common roots between those two languages.

463

Part III • Working with Others

Table 11.4. Data types.

C/C++

BASIC

FORTRAN

Pascal

short

variable%

INTEGER*2

INTEGER2

int

INTEGER

 

INTEGER

unsigned short

(unsigned not

(unsigned not

WORD

 

supported)

supported)

 

unsigned (see

 

 

 

unsigned long

 

 

 

and unsigned

 

 

 

short)

 

 

 

long

variable&

INTEGER*4

INTEGER4

 

LONG

INTEGER (default)

 

unsigned long

(unsigned not

(unsigned not

(unsigned not

 

supported)

supported)

supported)

float

variable!

REAL*4

REAL4

 

variable

REAL

REAL

 

SINGLE

 

 

double

variable#

REAL*8

REAL8

 

DOUBLE

DOUBLE PRECISION

 

long double

 

 

 

unsigned char

 

CHARACTER*1

CHAR

 

 

(not the same

 

 

 

as LOGICAL)

 

 

 

LOGICAL *2

 

 

 

LOGICAL *4

 

 

 

 

 

Because BASIC does not predeclare scalar variables, a variable’s type is indicated by a suffix code (%, &, !, or #). If the suffix is missing, the variable is assumed to be a real (floating-point) variable.

464

C and Other Languages

C C C

 

11C

 

C C C

 

C C

Variables are classified according to

Type (what is stored in them), such as integer, character, or floating point

Size (how large they are)

If you picture a variable as a certain number of bytes holding a certain type of data, you can easily convert from one language’s variable types to another.

Names and Limits

C allows a longer name length (the number of characters in a name that are significant) than FORTRAN. In C, the limit is 32 characters for internal names and 6 characters for external names. With some versions of FORTRAN, the limit is 6 characters for internal or external names.

When an external name is processed by C compilers, an underscore is usually added before or after the name. When programming in languages that do not use this convention, you must explicitly add the underscore. In Listing 11.9, for example, the printf() library function is called. The referenced name is _printf because the compiler prefixes the name with an underscore, and you must do the same. A FORTRAN or Pascal identifier does not have an added underscore.

When an external identifier is used in FORTRAN, the case of the name will have been changed to uppercase. This can create problems when linking the program, depending on the linker’s options. If you use the /NOIGNORECASE option (which tells the linker that the case of the identifiers must match) when you create a mixed language program, the linker may generate unresolved reference errors for names in the wrong case.

Summary

In this chapter, you learned about mixed language programming.

Programs written in C can call functions written in assembly, FORTRAN, Pascal, and BASIC, provided the compiler supports mixed language calls to the language to be called.

465