Array indexing with unsigned int

C251 v3.12

The C251 compiler does not handle arithmetic for array indices consistently. Consider

char tt[30] ;
char test ;
unsigned char aaa = 27 ;
unsigned int  bbb = 27 ;

test = tt[aaa - 20] ;
test = tt[bbb - 20] ;
The assembly code for the index using the unsigned char (aaa) has "-20" in its address construction. The assembly code for the index using the unsigned int (bbb) has "+65576" in its address construction. OOPS!
With the 8051's 16-bit address, this is no problem, but when using far addressing on the 251 you end up in different memory banks. int and char index variables work correctly like the unsigned char.

Parents
  • Sorry for the long delay in responding. I forgot to check back right away and a month slipped by.
    My test was a little different in that I used local parameters. Note the code expansion for line #96 versus #97. This test was compiled with v3.20b. I used the following compiler options:
    CD SB DB XSMALL OT(3,SPEED)
    DF(PROCESSOR=251)

    ----------------------------------
    89 void main(void)
    90 {
    91 1 char tt[30];
    92 1 char test ;
    93 1 unsigned char aaa = 27 ;
    94 1 unsigned int bbb = 27 ;
    95 1
    96 1 test = tt[aaa - 20] ;
    97 1 test = tt[bbb - 20] ;
    98 1 }

    ------------------------------------
    ; FUNCTION main (BEGIN)
    ; SOURCE LINE # 89
    ; SOURCE LINE # 90
    ; SOURCE LINE # 93
    000000 7E701B MOV R7,#01BH
    ;---- Variable 'aaa' assigned to Register 'R7' ----
    ; SOURCE LINE # 94
    000003 7E24001B MOV WR4,#01BH
    ;---- Variable 'bbb' assigned to Register 'WR4' ----
    ; SOURCE LINE # 96
    000007 0A37 MOVZ WR6,R7
    000009 09730000 R MOV R7,@WR6+tt-20
    ;---- Variable 'test' assigned to Register 'R7' ----
    ; SOURCE LINE # 97
    00000D 09720000 R MOV R7,@WR4+tt+65516
    ; SOURCE LINE # 98
    000011 22 RET
    ; FUNCTION main (END)
    --------------------------------
    Joel

Reply
  • Sorry for the long delay in responding. I forgot to check back right away and a month slipped by.
    My test was a little different in that I used local parameters. Note the code expansion for line #96 versus #97. This test was compiled with v3.20b. I used the following compiler options:
    CD SB DB XSMALL OT(3,SPEED)
    DF(PROCESSOR=251)

    ----------------------------------
    89 void main(void)
    90 {
    91 1 char tt[30];
    92 1 char test ;
    93 1 unsigned char aaa = 27 ;
    94 1 unsigned int bbb = 27 ;
    95 1
    96 1 test = tt[aaa - 20] ;
    97 1 test = tt[bbb - 20] ;
    98 1 }

    ------------------------------------
    ; FUNCTION main (BEGIN)
    ; SOURCE LINE # 89
    ; SOURCE LINE # 90
    ; SOURCE LINE # 93
    000000 7E701B MOV R7,#01BH
    ;---- Variable 'aaa' assigned to Register 'R7' ----
    ; SOURCE LINE # 94
    000003 7E24001B MOV WR4,#01BH
    ;---- Variable 'bbb' assigned to Register 'WR4' ----
    ; SOURCE LINE # 96
    000007 0A37 MOVZ WR6,R7
    000009 09730000 R MOV R7,@WR6+tt-20
    ;---- Variable 'test' assigned to Register 'R7' ----
    ; SOURCE LINE # 97
    00000D 09720000 R MOV R7,@WR4+tt+65516
    ; SOURCE LINE # 98
    000011 22 RET
    ; FUNCTION main (END)
    --------------------------------
    Joel

Children
More questions in this forum