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

use of bit as array index fails? (if bit is set)

Hi,

I suspect I have hit upon a bug in C51 V6.
When a bit variable is used as an index into
array of int or long int, it fails. Actually,
it fails only if the bit is 1; otherwise it
works ok. I am not 100% sure whether this
is a bug or am I doing something wrong?
For now, I replaced the bit variable by an
unsigned char, but I can not afford to do
so in the final version of my program. Is
there a workaround?

I wrote the test code below when I suspected
this bug. I tested it with the simulator and
I see un-expected results.

One User

#include <reg52.h>
#include <stdio.h>

xdata unsigned int UnIntArr[2] = {	0x1234, 0x5678	} ;
xdata unsigned long int UnLongIntArr[2] = {	0x12345678, 0x9abcdef0	} ;

void main ()
{
	unsigned char uch ;
	bit b ;

	RCAP2H = 0xff ;
	RCAP2L = 0xdc ;
	T2CON = 0x34 ;
	SCON = 0x50 ;
	TI = 1 ;

	uch = 0 ;
	b = 0 ;
	printf("first uch = 0 and b = 0\n") ;
	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[b]) ;
	// expected values = 0x1234, 0x1234 (works ok)
	printf("UnLongIntArr[uch] = %08lX, UnLongIntArr[b] = %08lX\n",UnLongIntArr[uch],UnLongIntArr[b]) ;
	// expected values = 0x12345678, 0x12345678 (works ok)

	uch = 1 ;
	b = 1 ;
	printf("now uch = 1 and b = 1\n") ;
	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[b]) ;
	// expected values = 0x5678, 0x5678
	// BUG HERE-> you get 0x5678, 0x3456
	printf("UnLongIntArr[uch] = %08lX, UnLongIntArr[b] = %08lX\n",UnLongIntArr[uch],UnLongIntArr[b]) ;
	// expected values = 0x9ABCDEF0, 0x9ABCDEF0
	// BUG HERE TOO-> you get 0x9ABCDEF0, 0x3456789A
}

Parents
  • Hi,

    Thanks!
    I tried to cast the bit to unsigned char:

    	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[(unsigned char) b]) ;
    

    It produced:

    00B2 A200        R     MOV     C,b
    C51 COMPILER V6.21  TEST                                                                   02/08/2003 20:18:31 PAGE 4
    
    00B4 E4                CLR     A
    00B5 33                RLC     A
    00B6 25E0              ADD     A,ACC
    

    Looks pretty good. So I don't need to use
    an extra unsigned char. 1 byte saved is
    precious for me. Thanks for the suggestion
    of using cast!

    One User

Reply
  • Hi,

    Thanks!
    I tried to cast the bit to unsigned char:

    	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[(unsigned char) b]) ;
    

    It produced:

    00B2 A200        R     MOV     C,b
    C51 COMPILER V6.21  TEST                                                                   02/08/2003 20:18:31 PAGE 4
    
    00B4 E4                CLR     A
    00B5 33                RLC     A
    00B6 25E0              ADD     A,ACC
    

    Looks pretty good. So I don't need to use
    an extra unsigned char. 1 byte saved is
    precious for me. Thanks for the suggestion
    of using cast!

    One User

Children