Machine Dependencies

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search

HI-TECH C eliminates many of the machine dependent aspects of C, since it uniformly implements such features as unsigned char. There are however certain areas where machine dependencies are inherent in the C language; programmers should be aware of these and take them into account when writing portable code.

The most obvious of these machine dependencies is the varying size of C types; on some machines an int will be 16 bits, on others it may be 32 bits. HI-TECH C conforms to the following rules, which represent common practice in most C compilers.

is at least 8 bits
is at least 16 bits
is at least 32 bits
is the same as either short or long
is at least 32 bits
is at least as wide as float

Because of the variable width of an int, it is recommended that short or long be used wherever possible in preference to int. The exception to this is where a quantity is required to correspond to the natural word size of the machine.

Another area of machine differences is that of byte ordering; the ordering of bytes in a short or long can vary significantly between machines. There is no easy approach to this problem other than to avoid code which depends on a particular ordering. In particular you should avoid writing out whole structures to a file (via fwrite()) unless the file is only to be read by the same program then deleted. Different compilers use different amounts of padding between structure members, though this can be modified via the #pragma pack(n) construct.

Predefined Macros

One technique through which machine unavoidable machine dependencies may be managed is the predefined macros provided by each compiler to identify the target processor and operating system (if any). These are defined by the compiler driver and may be tested with conditional compilation preprocessor directives.

The macros defined by various compilers are listed in table 2. These can be used as shown in the example in fig .

i8051 8051 processor family
i8086 8086 processor family
i8096 8096 processor family
z80 Z80 processor and derivatives
m68000 68000 processor family
m6800 6801, 68HC11 and 6301 processors
m6809 6809 processor
CPM CP/M-80 and CP/M-86
TOS Atari ST
Table 2. Predefined Macros

#if     DOS
char *  filename = "c:file";
#endif  /* DOS */
#if     CPM
char *  filename = "0:B:afile";
#endif  /* CPM */