Keil Logo

Memory-Specific Pointers

Memory-specific pointers always include a memory type specification in the pointer declaration and always refer to a specific memory area. For example:

char data *str;        /* ptr to string in data */
int xdata *numtab;     /* ptr to int(s) in xdata */
long code *powtab;     /* ptr to long(s) in code */

Because the memory type is specified at compile-time, the memory type byte required by generic pointers is not needed by memory-specific pointers. Memory-specific pointers can be stored using only one byte (idata, data, bdata, and pdata pointers) or two bytes (code and xdata pointers).

Like generic pointers, you may specify the memory area in which a memory-specific pointer is stored. To do so, prefix the pointer declaration with a memory type specifier. For example:

char data * xdata str;         /* ptr in xdata to data char */
int xdata * data numtab;       /* ptr in data to xdata int */
long code * idata powtab;      /* ptr in idata to code long */

Memory-specific pointers may be used to access variables in the declared 8051 memory area only. Memory-specific pointers provide the most efficient method of accessing data objects, but at the cost of reduced flexibility.

The following code and assembly listing shows how pointer values are assigned to memory-specific pointers. Note that the code generated for these pointers is much less involved than the code generated in the generic pointers example listing.

stmt level  source

   1        char data  *c_ptr;      /* memory-specific char ptr */
   2        int  xdata *i_ptr;      /* memory-specific int ptr */
   3        long code  *l_ptr;      /* memory-specific long ptr */
   4
   5        long code powers_of_ten [] =
   6          {
   7          1L,
   8          10L,
   9          100L,
  10          1000L,
  11          10000L,
  12          100000L,
  13          1000000L,
  14          10000000L,
  15          100000000L
  16          };
  17
  18        void main (void)
  19        {
  20   1    char data strbuf [10];
  21   1    int xdata ringbuf [1000];
  22   1
  23   1    c_ptr = &strbuf [0];
  24   1    i_ptr = &ringbuf [0];
  25   1    l_ptr = &powers_of_ten [0];
  26   1    }


ASSEMBLY LISTING OF GENERATED OBJECT CODE

      ; FUNCTION main (BEGIN)
                      ; SOURCE LINE # 18
                      ; SOURCE LINE # 19
                      ; SOURCE LINE # 23
0000 750000 R   MOV   c_ptr,#LOW strbuf
                      ; SOURCE LINE # 24
0003 750000 R   MOV   i_ptr,#HIGH ringbuf
0006 750000 R   MOV   i_ptr+01H,#LOW ringbuf
                      ; SOURCE LINE # 25
0009 750000 R   MOV   l_ptr,#HIGH powers_of_ten
000C 750000 R   MOV   l_ptr+01H,#LOW powers_of_ten
                      ; SOURCE LINE # 26
000F 22            RET
      ; FUNCTION main (END)

Note

  • The code generated for a memory-specific pointer executes more quickly than the equivalent code generated for a generic pointer. This is because the memory area is known at compile-time rather than at run-time. The compiler can use this information to optimize memory accesses. If execution speed is a priority, you should use memory-specific pointers instead of generic pointers wherever possible.

Related Knowledgebase Articles

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.