Topic : compiler bug about array offset

Forum : 8051

Original Post
Post Information Post
April 14, 2008 - 4:38pm
Guest

Please consider this piece of code:

static unsigned char a;
static unsigned char array[10];

void main() {

   a=2;
   
   array[(a-1)*3] = 0x55;
}

The result is wrong, the "0x55" is written in the wrong memory address!

the assembly code is here:

 main PROC
                        ; SOURCE LINE # 342 
                        ;$@$C_SOURCE_LINE(342)
        MOV     A,#002H
        MOV     DPTR,#a?
        MOVX    @DPTR,A
                        ; SOURCE LINE # 344 
                        ;$@$C_SOURCE_LINE(344)
        MOV     B,#003H
        MUL     AB
        ADD     A,#LOW (array + 0FDH)
        MOV     DPL,A
        MOV     A,B
        ADDC    A,#HIGH (array + 0FDH)
        MOV     DPH,A
        MOV     A,#055H
        MOVX    @DPTR,A
                        ; SOURCE LINE # 345 
                        ;$@$C_SOURCE_LINE(345)
        RET     

        ENDP

I think that troubles arises from the optimization of the "(a-1)*3" formula,
more precisely, the

ADDC A,#HIGH (array + 0FDH) istruction, should became:

ADDC A,#HIGH (array + 0FFFDH) let me know what you think

thank you

Replies
Post Information Post
+1
0
-1
April 14, 2008 - 6:26pm
Guest

Hi Massimo

I did compile the code sample I used Optimization level of 7 in SPEED, and I obtain this assembly code:

MOV A,#02h ; 03E3 > 7402
MOV DPTR,#0000 ; 03E5 > 900000
MOVX @DPTR,A ; 03E8 > F0
MOV B,#03h ; 03E9 > 75F003
MUL AB ; 03EC > A4
ADD A,#FEh ; 03ED > 24FE
MOV DPL,A ; 03EF > F582
MOV A,B ; 03F1 > E5F0
ADDC A,#FFh ; 03F3 > 34FF
MOV DPH,A ; 03F5 > F583
MOV A,#55h ; 03F7 > 7455
MOVX @DPTR,A ; 03F9 > F0

I get from array watch in debug mode array {0xFF, 0xFF, 0xFF, 0x55,0xFF,0xFF......} which is what is expected I guess.

So may be you have an older version than mine so please send me an email with a screen shot of
your HELP->about and click on 8051 toolchain

Matloub

+1
0
-1
April 30, 2008 - 5:00pm
Guest

Hi

Finally after exchanging project with Massimo
We found out that this bug concerning the optimization code appears only in a specific configuration.

If you meet this problem it means you are working in SRC mode.
So I suggest you change this option.

The correction of this bug will be in our next release.
If you really can not wait for it feel free to send me an email at support@raisonance