-

Difference between revisions of "Machine Dependencies"

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search
(Created page with "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 machi...")
 
 
(4 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
compilers.
 
compilers.
  
char   is at least 8 bits
+
;char
short  is at least 16 bits
+
:is at least 8 bits
long    is at least 32 bits
 
int    is the same as either short or long
 
float  is at least 32 bits
 
double  is at least as wide as float
 
  
 +
;short
 +
:is at least 16 bits
  
Because of the variable width of an int, it  is  recom-
+
;long
mended that  short  or  long  be  used wherever possible in
+
:is at least 32 bits
 +
 
 +
;int
 +
:is the same as either short or long
 +
 
 +
;float
 +
:is at least 32 bits
 +
 
 +
;double
 +
: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
 
preference to int. The exception to this is where a quantity
 
is  required  to  correspond to the natural word size of the
 
is  required  to  correspond to the natural word size of the
Line 31: Line 41:
 
this  problem  other  than  to avoid code which depends on a
 
this  problem  other  than  to avoid code which depends on a
 
particular ordering. In particular you should avoid  writing
 
particular ordering. In particular you should avoid  writing
out  whole  structures  to  a file (via _�f_�w_�r_�i_�t_�e()) unless the
+
out  whole  structures  to  a file (via <strong>fwrite</strong>()) unless the
 
file is only to be read by the same  program  then  deleted.
 
file is only to be read by the same  program  then  deleted.
 
Different compilers use different amounts of padding between
 
Different compilers use different amounts of padding between
structure members, though  this  can  be  modified  via  the
+
structure members, though  this  can  be  modified  via  the #pragma pack(n) construct.
#pragma pack(n) construct.
 
  
 
== Predefined Macros ==
 
== Predefined Macros ==
  
 
One technique through which machine unavoidable machine
 
One technique through which machine unavoidable machine
dependencies  may  be  managed is the predefined macros pro-
+
dependencies  may  be  managed is the predefined macros provided by each compiler to identify the target processor  and
vided by each compiler to identify the target processor  and
 
 
operating system (if any). These are defined by the compiler
 
operating system (if any). These are defined by the compiler
 
driver  and  may  be  tested  with  conditional  compilation
 
driver  and  may  be  tested  with  conditional  compilation
Line 49: Line 57:
 
table 2. These can be used as shown in the example in fig .
 
table 2. These can be used as shown in the example in fig .
  
___________________________________________
+
{|
|_M�_a�_c�_r�_o�_______________D�_e�_f�_i�_n�_e�_d�__f�_o�_r�_____________�|
+
|+<strong>_Macro__Defined_for</strong>|
| i8051   8051 processor family          |
+
|-
| i8086   8086 processor family          |
+
|i8051 ||  8051 processor family           
| i8096   8096 processor family          |
+
|-
| z80     Z80 processor and derivatives  |
+
|i8086 ||  8086 processor family           
| m68000  68000 processor family          |
+
|-
| m6800   6801, 68HC11 and 6301 processors|
+
|i8096 ||  8096 processor family           
| m6809   6809 processor                  |
+
|-
| DOS     MS-DOS and PC-DOS              |
+
|z80   ||  Z80 processor and derivatives   
| CPM     CP/M-80 and CP/M-86            |
+
|-
| TOS     Atari ST                        |
+
|m68000||   68000 processor family           
|___________________________________________�|
+
|-
 
+
|m6800 ||  6801, 68HC11 and 6301 processors
 +
|-
 +
|m6809 ||  6809 processor                   
 +
|-
 +
|DOS   ||  MS-DOS and PC-DOS               
 +
|-
 +
|CPM   ||  CP/M-80 and CP/M-86             
 +
|-
 +
|TOS   ||  Atari ST                         
 +
|-
 +
|}
  
Table 2. Predefined Macros
+
;Table 2. Predefined Macros
  
  
#if    DOS
+
#if    DOS
char *  filename = "c:file";
+
char *  filename = "c:file";
#endif  /* DOS */
+
#endif  /* DOS */
#if    CPM
+
#if    CPM
char *  filename = "0:B:afile";
+
char *  filename = "0:B:afile";
#endif  /* CPM */
+
#endif  /* CPM */

Latest revision as of 18:17, 30 July 2017

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.

char
is at least 8 bits
short
is at least 16 bits
long
is at least 32 bits
int
is the same as either short or long
float
is at least 32 bits
double
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
DOS MS-DOS and PC-DOS
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 */