PSoC >> PSoC Mixed-Signal Array

Pages: 1
IceCube
Tester - PRO for PSoC
***

Reged: Jun 27 2007
Posts: 86
Loc: The Netherlands
type mis-interpretation??
      #62452 - Thu Jun 26 2008 03:47 PM

Hi,

This Code:
typedef union
{
unsigned int Word;
struct {
unsigned char High;
unsigned char Low;
} Byte;
} WordByteType;

WordByteType Checksum;
main()
{
if (RX_Buffer == ~Checksum.Byte.Low) RXstate++;
}

gives Code:
             	mov	a, 0
cpl a
mov [(??_BootLoader^0+2)], a
mov a, [(_Checksum+1)^0]
cpl a
mov [(??_BootLoader^0+3)], a
mov a, reg[46]
cmp a, [(??_BootLoader^0+3)]
jnz l34
mov a, [(??_BootLoader^0+2)]
jnz l34


So the 'Checksum.Byte.Low' is interpreted as an integer. I guess this should be interpreted as a byte.

Casting 'Checksum.Byte.Low' to a byte will give the right results. Code:
if (RX_Buffer == (unsigned char) ~Checksum.Byte.Low) RXstate++;


Regards, Rolf


Post Extras: Print Post   Remind Me!   Notify Moderator  
clydeAdministrator
HI-TECH team member
*****

Reged: Oct 16 2003
Posts: 633
Re: type mis-interpretation?? [Re: IceCube]
      #62472 - Thu Jun 26 2008 06:19 PM

Rolf,

The code generated is correct. Even though the value is a byte, the C standard specifies that it is converted to an integer before applying the complement operator. So the value of ~x, where x is an unsigned byte, is always in the range 0xFF00-0xFFFF.

Applying the typecast to the result of the ~ operator as you have done is the correct way to deal with this to achieve the result you wanted.

Incidentally, this treatment of data (promoting to integer before performing arithmetic) applies to all the standard operators. The compiler optimizes the resultant code where possible to achieve the correct result. The C standard allows arithmetic to be done in any way as long as the result is the same as if it were done strictly according to the standard (this is known as the "as-if" rule).

In this case if the complement and subsequent comparison were done in 8 bits, the result would not be the same as if they were done in the correct 16 bit length. Adding the typecast to the result of the complement ensures that the top 8 bits are always zero, so the comparison can then be done in 8 bits, which in turn means the complement only needs to be done on the 8 bits of the variable.

An alternative way of achieving what you wanted would be:
Code:

if (RX_Buffer == (Checksum.Byte.Low ^ 0xFF)) RXstate++;





Clyde


Post Extras: Print Post   Remind Me!   Notify Moderator  
IceCube
Tester - PRO for PSoC
***

Reged: Jun 27 2007
Posts: 86
Loc: The Netherlands
Re: type mis-interpretation?? [Re: clyde]
      #62922 - Mon Jun 30 2008 02:55 AM

Thank you Clyde, that clears up

Regards, Rolf


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
1 registered and 1 anonymous users are browsing this forum.

Moderator:  ndouglas, Dan Henry, jtemples, jeff, Andrew L, mikerj, dave g, meisty, josh stevo 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 886

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5