This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Could someone explain?

This ex. will not work

char	data	test;

char TestF(void);

char TestF(void)
{
if (test == 0x8A)
	{
	//...
	return(1);
	}
}
//Take a look at the Assambly code
             ; FUNCTION TestF (BEGIN)
                                           ; SOURCE LINE # 5
                                           ; SOURCE LINE # 6
                                           ; SOURCE LINE # 7
                 R     MOV     R7,test
                       MOV     A,R7
                       RLC     A
                       SUBB    A,ACC
                       MOV     R6,A
                       MOV     A,R7
                       XRL     A,#08AH
                       ORL     A,R6
                 R     xJNZ    ?C0002
                                           ; SOURCE LINE # 8
                                           ; SOURCE LINE # 10
                       MOV     R7,#01H
                                           ; SOURCE LINE # 11
                                           ; SOURCE LINE # 12
             ?C0002:
                       RET
But this one works
char	data	test;

char TestF(void);

char TestF(void)
{
if (test == (char)0x8A)
	{
	//...
	return(1);
	}
}
//Take a look at the Assambly code
             ; FUNCTION TestF (BEGIN)
                                           ; SOURCE LINE # 5
                                           ; SOURCE LINE # 6
                                           ; SOURCE LINE # 7
                 R     MOV     A,test
                 R     xJNE    A,#08AH,?C0002
                                           ; SOURCE LINE # 8
                                           ; SOURCE LINE # 10
                       MOV     R7,#01H
                                           ; SOURCE LINE # 11
                                           ; SOURCE LINE # 12
             ?C0002:
                       RET

Parents
  • Hi,

    look more careful at the next two lines:

    char	data	test;
    
    if (test == 0x8A)
    

    you declared signed 1-byte wide variable and then compare it with number which may not be presented with signed char!
    Really, signed char may be inside
    -128...127 border but 0x8A is overload this limit. So cimpiler converts your signed char variable into int one before comparing.

    At second example, you said to compiler that number must be converted to signed char one before comparasion. So it just accept it as signed 1-byte value.

    Regards,
    Oleg

Reply
  • Hi,

    look more careful at the next two lines:

    char	data	test;
    
    if (test == 0x8A)
    

    you declared signed 1-byte wide variable and then compare it with number which may not be presented with signed char!
    Really, signed char may be inside
    -128...127 border but 0x8A is overload this limit. So cimpiler converts your signed char variable into int one before comparing.

    At second example, you said to compiler that number must be converted to signed char one before comparasion. So it just accept it as signed 1-byte value.

    Regards,
    Oleg

Children
  • Ok I can understand that but if I write this

       1          char    data    test;
       2
       3          char TestF(void);
       4
       5          char TestF(void)
       6          {
       7   1      if (test == -118)
       8   1              {
       9   2              //...
      10   2              return(1);
      11   2              }
      12   1      }
      13
      14
    CX51 COMPILER V7.09   TEST                                                                 03/04/2004 11:59:16 PAGE 2
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
                 ; FUNCTION TestF (BEGIN)
                                               ; SOURCE LINE # 5
                                               ; SOURCE LINE # 6
                                               ; SOURCE LINE # 7
                     R     MOV     A,test
                     R     xJNE    A,#08AH,?C0002
                                               ; SOURCE LINE # 8
                                               ; SOURCE LINE # 10
                           MOV     R7,#01H
                                               ; SOURCE LINE # 11
                                               ; SOURCE LINE # 12
                 ?C0002:
                           RET
    
    It will work.
    And we can see that -118 = 0x8a.
    Soo why can't i compare a signed char to an nagative hex value?
    It works from 0x00 - 0x7f.

  • hi,
    And we can see that -118 = 0x8a.
    Soo why can't i compare a signed char to an nagative hex value?


    Ofcourse you may! But then type:
    if (test == -0x76)

    Just indicate to compiler that value is negative!!

    Regards,
    Oleg

  • ...and another way:
    if you for some reason need to compare signed value as not signed one then you may use something like:

    if ((unsigned char)test == 0x8A)

    So, my previous post shows you how compare signed variable with signed (here: negative) number, this example above shows how to compare signed variable as unsigned one.

    In any case, it is your work to say to compiler about types conversion.

    Regards,
    Oleg