far to near pointer conversion. (MC9S12DP512)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

far to near pointer conversion. (MC9S12DP512)

3,969 次查看
ssinfod
Contributor IV
Hi,
 
I want to display a char[] from a banked page without using the -CpPPAGE=RUNTIME compiler option.
 
I think I can use the lower 2 bytes of the address of the char[] since I will display the data from the same page.
 
So my goal is to convert a char *far  to a char *near.
*far is on 3 bytes : Page, High address, Low address
*near is on 2 byers : High address, Low address.
 
The code below works but I have a compiler warning. What is exact way to convert a *far to a *near without a compiler warning.
 
Note : The data and the code are in page 3D.
 
Thanks..
 
#pragma CONST_SEG __PPAGE_SEG BANKED_DATA_3D                                   
static const char my_string[] = "This is a string";                         // Located in page 3D
 
#pragma CODE_SEG ROM_3D
#pragma CONST_SEG CONST_3D
void far_pstr(const sc *far ptr)
{
   while (*ptr) { putchar(*ptr++); }
}
void near_pstr(const sc *ptr)
{
   while (*ptr) { putchar(*ptr++); }
}
void print_test(void)
{
   char *ptr = my_string;      // This gives  :   Warning : C1860: Pointer conversion: possible loss of data
   char *near ptr2 = (char *near) my_string; // This gives  :   Warning : C1860: Pointer conversion: possible loss of data
  
   near_pstr(my_string);     // This gives  :   Warning : C1860: Pointer conversion: possible loss of data
   far_pstr(my_string);     // This gives  :   Warning : C12002: Use DPAGE, PPAGE, EPAGE to specify the page register. Assumed DPAGE
 
}
 
 
标签 (1)
0 项奖励
回复
3 回复数

1,721 次查看
ssinfod
Contributor IV
FYI,
I found this solution...
 
  char* temp;                        
   asm
   {
       LDD   #my_string
       STD   temp
   }
   pstr(temp);
   
 
 
0 项奖励
回复

1,721 次查看
CompilerGuru
NXP Employee
NXP Employee
You could:
- not tell the compiler that those constants are far.
- use a cast to int, probably hidden by some macro (#define TO_16_BIT_CHAR_PTR(x) ((const char*)(int)(x))

The compiler on its own does not know that the code and the constants end up at the same page,
so by default the warning is justified.

Daniel

#pragma CONST_SEG __PPAGE_SEG BANKED_DATA_3D                                  
static const char my_string[] = "This is a string";                         // Located in page 3D
#pragma CONST_SEG BANKED_DATA_3D_TOO
static const char another_my_string[] = "This is a string";                 // Located in page 3D

#pragma CODE_SEG ROM_3D

void putchar(char s) {
  s++;
}

void near_pstr(const char* ptr) {
   while (*ptr) { putchar(*ptr++); }
}
void t(void) {
  near_pstr((char*)(int)my_string);
  near_pstr(another_my_string);
}
0 项奖励
回复

1,721 次查看
ssinfod
Contributor IV
Thanks, your solution works fine
0 项奖励
回复