next up previous contents index
Next: 3.5 Absolute Addressing Up: 3 Using SDCC Previous: 3.3 Environment variables   Contents   Index

Subsections


3.4 MCS51/DS390 Storage Class Language Extensions

In addition to the ANSI storage classes SDCC allows the following MCS51 specific storage classes.


3.4.1 data

This is the default storage class for Small Memory model. Variables declared with this storage class will be allocated in the directly addressable portion of the internal RAM of a 8051, e.g.:

data unsigned char test_data;
Writing 0x01 to this variable generates the assembly code:

75*00 01   mov  _test_data,#0x01


3.4.2 xdata

Variables declared with this storage class will be placed in the external RAM. This is the default storage class for Large Memory model, e.g.:

xdata unsigned char test_xdata;
Writing 0x01 to this variable generates the assembly code:

90s00r00   mov  dptr,#_test_xdata  
74 01      mov  a,#0x01  
F0         movx @dptr,a


3.4.3 idata

Variables declared with this storage class will be allocated into the indirectly addressable portion of the internal ram of a 8051, e.g.:

idata unsigned char test_idata;
Writing 0x01 to this variable generates the assembly code:

78r00       mov  r0,#_test_idata 
76 01       mov  @r0,#0x01


3.4.4 pdata

Paged xdata access is currently not as straightforward as using the other addressing modes of a 8051. The following example writes 0x01 to the address pointed to. Please note, pdata access physically accesses xdata memory. The high byte of the address is determined by port P2 (or in case of some 8051 variants by a separate Special Function Register).

pdata unsigned char *test_pdata_ptr; 
 
void main()  
{  
    test_pdata_ptr = (pdata *)0xfe;  
    *test_pdata_ptr = 1;  
}
Generates the assembly code:

75*01 FE   mov  _test_pdata_ptr,#0xFE 
78 FE      mov  r0,#0xFE  
74 01      mov  a,#0x01 
F2         movx @r0,a
Be extremely carefull if you use pdata together with the --xstack option.


3.4.5 code

'Variables' declared with this storage class will be placed in the code memory:

code unsigned char test_code;
Read access to this variable generates the assembly code:

90s00r6F   mov dptr,#_test_code 
E4         clr a 
93         movc a,@a+dptr


3.4.6 bit

This is a data-type and a storage class specifier. When a variable is declared as a bit, it is allocated into the bit addressable memory of 8051, e.g.:

bit test_bit;
Writing 1 to this variable generates the assembly code:

D2*00       setb _test_bit


3.4.7 sfr / sbit

Like the bit keyword, sfr / sbit signifies both a data-type and storage class, they are used to describe the special function registers and special bit variables of a 8051, eg:

sfr at 0x80 P0;  /* special function register P0 at location 0x80 */ 
sbit at 0xd7 CY; /* CY (Carry Flag) */


3.4.8 Pointers to MCS51/DS390 specific memory spaces

SDCC allows (via language extensions) pointers to explicitly point to any of the memory spaces of the 8051. In addition to the explicit pointers, the compiler uses (by default) generic pointers which can be used to point to any of the memory spaces.

Pointer declaration examples:

/* pointer physically in internal ram pointing to object in external ram */  
xdata unsigned char * data p; 
 
/* pointer physically in external ram pointing to object in internal ram */  
data unsigned char * xdata p; 
 
/* pointer physically in code rom pointing to data in xdata space */  
xdata unsigned char * code p; 
 
/* pointer physically in code space pointing to data in code space */  
code unsigned char * code p; 
 
/* the following is a generic pointer physically located in xdata space */ 
char * xdata p;
Well you get the idea.

All unqualified pointers are treated as 3-byte (4-byte for the ds390) generic pointers.

The highest order byte of the generic pointers contains the data space information. Assembler support routines are called whenever data is stored or retrieved using generic pointers. These are useful for developing reusable library routines. Explicitly specifying the pointer type will generate the most efficient code.


next up previous contents index
Next: 3.5 Absolute Addressing Up: 3 Using SDCC Previous: 3.3 Environment variables   Contents   Index
Bernhard Held 2003-08-29