Keil Logo

Abstract Pointers

You may use abstract pointers to access fixed memory locations or to call functions located at fixed addresses.

Abstract pointer types are described here using code examples with the following variables.

char xdata *px;       /* ptr to xdata */
char idata *pi;       /* ptr to idata */
char code  *pc;       /* ptr to code  */

char c;               /* char variable in data space */
int  i;               /* int variable in data space */

Assigning a Function Address to a Pointer

The following example assigns the address of the main C function to a pointer (stored in data memory) to a char stored in code memory.

pc = (void *) main;

0000 750000  R     MOV     pc,#HIGH main
0003 750000  R     MOV     pc+01H,#LOW main

Casting a Data Variable Address to an IData Pointer

The following example casts the address of the variable i (which is an int data *) to a pointer to a char in idata. Since i is stored in data and since indirectly accessed data is idata, this pointer conversion is valid.

pi = (char idata *) &i;

0000 750000  R     MOV     pi,#LOW i

Casting an XData Pointer to an IData Pointer

The following example casts a pointer to a char in xdata to a pointer to a char in idata. Since xdata pointers occupy 2 bytes and idata pointers occupy 1 byte, this pointer conversion may not yield the desired results since the upper byte of the xdata pointer is ignored. Refer to Pointer Conversions for more information about converting between different pointer types.

pi = (char idata *) px;

0000 850000  R     MOV     pi,px+01H

Casting a Constant Value to a Code Pointer

The following example casts 0x1234 as a pointer to a char in code memory.

pc = (char code *) 0x1234;

0000 750012  R     MOV     pc,#012H
0003 750034  R     MOV     pc+01H,#034H

Casting a Constant Value to a Function Pointer

The following example casts 0xFF00 as a function pointer that takes no arguments and returns an int, invokes the function, and assigns the return value to the variable i. The portion of this example that performs the function pointer type cast is: ((int (code *)(void)) 0xFF00). By adding the argument list to the end of the function pointer, the compiler can correctly invoke the function.

i = ((int (code *)(void)) 0xFF00) ();

0000 12FF00        LCALL   0FF00H
0003 8E00    R     MOV     i,R6
0005 8F00    R     MOV     i+01H,R7

Dereferencing a Constant Value Cast to a Code Pointer

The following example casts 0x8000 as a pointer to a char in code memory, extracts the char pointed to, and assigns it to the variable c.

c = *((char code *) 0x8000);

0000 908000        MOV     DPTR,#08000H
0003 E4            CLR     A
0004 93            MOVC    A,@A+DPTR
0005 F500    R     MOV     c,A

Dereferencing a Constant Value Cast to an XData Pointer

The following example casts 0xFF00 as a pointer to a char in xdata memory, extracts the char pointed to, and adds it to the variable c.

c += *((char xdata *) 0xFF00);

0000 90FF00        MOV     DPTR,#0FF00H
0003 E0            MOVX    A,@DPTR
0004 2500    R     ADD     A,c
0006 F500    R     MOV     c,A

Dereferencing a Constant Value Cast to an IData Pointer

The following example casts 0xF0 as a pointer to a char in idata memory, extracts the char pointed to, and adds it to the variable c.

c += *((char idata *) 0xF0);

0000 78F0          MOV     R0,#0F0H
0002 E6            MOV     A,@R0
0003 2500    R     ADD     A,c
0005 F500    R     MOV     c,A

Dereferencing a Constant Value Cast to a PData Pointer

The following example casts 0xE8 as a pointer to a char in pdata memory, extracts the char pointed to, and adds it to the variable c.

c += *((char pdata *) 0xE8);

0000 78E8          MOV     R0,#0E8H
0002 E2            MOVX    A,@R0
0003 2500    R     ADD     A,c
0005 F500    R     MOV     c,A

Dereferencing a Constant Value Cast to a Code Pointer

The following example casts 0x2100 as a pointer to an int in code memory, extracts the int pointed to, and assigns it to the variable i.

i = *((int code *) 0x2100);

0000 902100        MOV     DPTR,#02100H
0003 E4            CLR     A
0004 93            MOVC    A,@A+DPTR
0005 FE            MOV     R6,A
0006 7401          MOV     A,#01H
0008 93            MOVC    A,@A+DPTR
0009 8E00    R     MOV     i,R6
000B F500    R     MOV     i+01H,A

Dereferencing a Constant Value
Cast to a Pointer to an XData Pointer to a Char in XData

The following example casts 0x4000 as a pointer to a pointer in xdata that points to a char in xdata. The assignment extracts the pointer stored in xdata that points to the char which is also stored in xdata.

px = *((char xdata * xdata *) 0x4000);

0000 904000        MOV     DPTR,#04000H
0003 E0            MOVX    A,@DPTR
0004 FE            MOV     R6,A
0005 A3            INC     DPTR
0006 E0            MOVX    A,@DPTR
0007 8E00    R     MOV     px,R6
0009 F500    R     MOV     px+01H,A

Dereferencing a Constant Value
Cast to a Pointer to an XData Pointer to an Array of Char

This example casts 0x4000 as a pointer to a pointer in xdata that points to a char in xdata. The pointer is accessed as an array of pointers in xdata. The assignment accesses array element 0 (which is stored at 0x4000 in xdata) and extracts the pointer there that points to the char stored in xdata.

px = ((char xdata * xdata *) 0x4000) [0];

0000 904000        MOV     DPTR,#04000H
0003 E0            MOVX    A,@DPTR
0004 FE            MOV     R6,A
0005 A3            INC     DPTR
0006 E0            MOVX    A,@DPTR
0007 8E00    R     MOV     px,R6
0009 F500    R     MOV     px+01H,A

Dereferencing a Constant Value
Cast to a Pointer to an XData Pointer to an Array of Char

This example casts 0x4000 as a pointer to a pointer in xdata that points to a char in xdata. The pointer is accessed as an array of pointers in xdata. Since the object pointed to is a pointer in xdata (to a char), the size of each element in the array is 2 bytes. The assignment accesses array element 1 (which is stored at 0x4002 in xdata) and extracts the pointer there that points to the char stored in xdata.

px = ((char xdata * xdata *) 0x4000) [1];

0000 904002        MOV     DPTR,#04002H
0003 E0            MOVX    A,@DPTR
0004 FE            MOV     R6,A
0005 A3            INC     DPTR
0006 E0            MOVX    A,@DPTR
0007 8E00    R     MOV     px,R6
0009 F500    R     MOV     px+01H,A
  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.