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

bug in the compiler??

Hi All,

I don't believe this is actually a bug, but can anybody explain why this code works correctly

void func(int v1, int v2){
	unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2', '3', '4'};
	signed char xdata diff = v1 - v2;

	if (diff >= -4 && diff <= 4)
		printf("%c\n", tbl[diff + 4]);

}
and this doesn't?
void func(int v1, int v2){
	unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2', '3', '4'};
	int xdata diff = v1 - v2;

	if (diff >= -4 && diff <= 4)
		printf("%c\n", tbl[diff + 4]);

}
Is this behaviour ANSI-compliant?

Cheers

Parents
  • But I notice that if you assign 'diff + 4' to a temporary int and use that as the array index it works correctly.

    You're right. If you use a temporary variable for diff + 4, it doesn't matter whether it's an int or a char.

    Need to have a look at the listing file to see what's going wrong. Maybe not on a Friday afternoon though..

    No problem. I can have a look at the listing file myself. Here's what I got:

         4: void func(int v1, int v2){
         5:         unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2',
         6:         int xdata diff = v1 - v2;
         7:
                    ...
    
    C:0x0409 900000 MOV   DPTR,#0x0000   ;DPTR = &diff
                                         ;diff = -1 = 0xFFFF
                    ...
    
    C:0x0419 A3     INC   DPTR           ;DPTR points to low byte of diff (0xFF)
    C:0x041A E0     MOVX  A,@DPTR        ;A = 0xFF
    C:0x041B 900472 MOV   DPTR,#0x0472   ;DPTR = tbl + 4
    C:0x041E 93     MOVC  A,@A+DPTR      ;A = *(tbl + 4 + 0xFF) That's it!
    C:0x041F F50B   MOV   0x0B,A
    C:0x0421 120065 LCALL PRINTF(C:0065)
        10: }
        11:
    C:0x0424 22     RET
    
    Obviously this code takes a character at address tbl + 4 + 0xFF instead of tbl + 4 - 1. Should I consider this a compiler bug?

Reply
  • But I notice that if you assign 'diff + 4' to a temporary int and use that as the array index it works correctly.

    You're right. If you use a temporary variable for diff + 4, it doesn't matter whether it's an int or a char.

    Need to have a look at the listing file to see what's going wrong. Maybe not on a Friday afternoon though..

    No problem. I can have a look at the listing file myself. Here's what I got:

         4: void func(int v1, int v2){
         5:         unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2',
         6:         int xdata diff = v1 - v2;
         7:
                    ...
    
    C:0x0409 900000 MOV   DPTR,#0x0000   ;DPTR = &diff
                                         ;diff = -1 = 0xFFFF
                    ...
    
    C:0x0419 A3     INC   DPTR           ;DPTR points to low byte of diff (0xFF)
    C:0x041A E0     MOVX  A,@DPTR        ;A = 0xFF
    C:0x041B 900472 MOV   DPTR,#0x0472   ;DPTR = tbl + 4
    C:0x041E 93     MOVC  A,@A+DPTR      ;A = *(tbl + 4 + 0xFF) That's it!
    C:0x041F F50B   MOV   0x0B,A
    C:0x0421 120065 LCALL PRINTF(C:0065)
        10: }
        11:
    C:0x0424 22     RET
    
    Obviously this code takes a character at address tbl + 4 + 0xFF instead of tbl + 4 - 1. Should I consider this a compiler bug?

Children