Keil Logo

C51: FAR VAR _AT_ COMPARED TO CAST VALUE


Information in this article applies to:

  • C51 Version 8.00 and Later

SYMPTOM

The following function:

char far far_var[10] _at_ 0x021234;

char test (void)
{
char far *fp1 = far_var;
char far *fp2 = (void far *) 0x021234L;

if (fp1 == fp2)
  return(1);
else
  return(0);
}

always returns a zero ('0') even though it should return one ('1').

CAUSE

Addresses specified with the _at_ are converted into a memory type byte + offset combination. So, the variable located using _at_ 0x021234 has an address of 0x031234.

Values cast into an address are assumed to have the correct memory type byte + offset combination. So, the type cast (void far *) 0x021234L has an address of 0x021234.

Based on these conversion rules, these two addresses are not identical. And, the function returns a zero ('0').

RESOLUTION

The best way to resolve this issue is to use the FVAR macro from ABSACC.H in place of the type cast: &FVAR(char,0x021234). For example:

#include <absacc.h>

char far far_var[10] _at_ 0x011234;

char test (void)
{
char far *fp1 = far_var;
char far *fp2 = &FVAR(char,0x021234L);

if (fp1 == fp2)
  return(1);
else
  return(0);
}

Another way to resolve this issue is to change either the _at_ address to 0x011234 or change the type cast to 0x031234. But don't change both. For example:

char far far_var[10] _at_ 0x011234;

char test (void)
{
char far *fp1 = far_var;
char far *fp2 = (void far *) 0x021234L;

if (fp1 == fp2)
  return(1);
else
  return(0);
}

MORE INFORMATION

SEE ALSO

Last Reviewed: Tuesday, April 10, 2007


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  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.