|
|||||||||||
|
Technical Support On-Line Manuals Cx51 User's Guide |
Abstract PointersYou 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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 PointerThe 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
| ||||||||||
|
|||||||||||
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.