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]); }
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]); }
Can you post a minimal compilable program that shows the problem?
#include <reg51.H> #include <stdio.h> void func(int v1, int v2){ unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2', '3', '4'}; char xdata diff = v1 - v2; if (diff >= -4 && diff <= 4) printf("%c\n", tbl[diff + 4]); } void main(){ SCON = 0x50; /* Setup serial port control register */ /* Mode 1: 8-bit uart var. baud rate */ /* REN: enable receiver */ PCON &= 0x7F; /* Clear SMOD bit in power ctrl reg */ /* This bit doubles the baud rate */ TMOD &= 0xCF; /* Setup timer/counter mode register */ /* Clear M1 and M0 for timer 1 */ TMOD |= 0x20; /* Set M1 for 8-bit autoreload timer */ TH1 = 0xFD; /* Set autoreload value for timer 1 */ /* 9600 baud with 11.0592 MHz xtal */ TR1 = 1; /* Start timer 1 */ TI = 1; /* Set TI to indicate ready to xmit */ func(1, 2); }
"This program works (prints 'd'). If you replace char with int, it doesn't." But I notice that if you assign 'diff + 4' to a temporary int and use that as the array index it works correctly. Need to have a look at the listing file to see what's going wrong. Maybe not on a Friday afternoon though..
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
"Should I consider this a compiler bug?" Well, I have no other explanation. Could you let us know what version of the compiler you are using?
Well, I have no other explanation. Could you let us know what version of the compiler you are using? Version 7.01. Is that the latest one?
"Version 7.01. Is that the latest one?" 7.20 is the latest. Maybe someone who uses a more recent version than 7.01 could check whether this has been fixed?