Why the following bitwise & doesn't work?

unsigned char tp;
unsigned char volatile pdata HOSTCFG2 _at_ 0x0020;
// HOSTCFG2=0x40 at this point
tp=HOSTCFG2 & 0x40;

//!!! tp is 0x00 instead of 0x40 which is expected

//Corresponding Disassembly:

C:0x08F1    7820     MOV      R0,#HOSTCFG2(0x20)
C:0x08F3    E2       MOVX     A,@R0
C:0x08F4    5440     ANL      A,#HPWRSTATE(0x40)
//Why it refers 0x40 as an address instead of a constant here?
C:0x08F6    F508     MOV      0x08,A
   189:         if ((HOSTCFG2 & 0x40)==0x00)


Thanks.

Parents
  • The # means "immediate addressing", which is to say a literal constant, not necessarily an address.

    In the first line, the #20H happens to be a constant used as address in the second line (MOVX). In the third line, the #40H happens to be a constant value used in the ANL operation.

    How do you know that HOSTCFG2 is really 0x40?

    Have you checked the configuration of your pdata area to be sure it matches the hardware? Does your hardware correctly supply the upper 8 bits of the address?

Reply
  • The # means "immediate addressing", which is to say a literal constant, not necessarily an address.

    In the first line, the #20H happens to be a constant used as address in the second line (MOVX). In the third line, the #40H happens to be a constant value used in the ANL operation.

    How do you know that HOSTCFG2 is really 0x40?

    Have you checked the configuration of your pdata area to be sure it matches the hardware? Does your hardware correctly supply the upper 8 bits of the address?

Children
More questions in this forum