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

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
  • Joel,

    I got the following:

    stmt  level    source
    
        1          char tt[30] ;
        2          char test ;
        3          unsigned char aaa = 27 ;
        4          unsigned int  bbb = 27 ;
        5          
        6          void main (void)
        7          {
        8   1      
        9   1      test = tt[aaa - 20] ;
       10   1      test = tt[bbb - 20] ;
       11   1      
       12   1      
       13   1      }
       14          
       15          
       16          
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    ;       FUNCTION main (BEGIN)
                                                 ; SOURCE LINE # 6
                                                 ; SOURCE LINE # 9
    000000 7E1100      R  MOV      R1,aaa
    000003 2E1000      R  ADD      R1,#LOW tt+236
    000006 A5E7           MOV      A,@R1         ; A=R11
    000008 F500        R  MOV      test,A        ; A=R11
                                                 ; SOURCE LINE # 10
    00000A 7E0500      R  MOV      WR0,bbb
    00000D 9E040014       SUB      WR0,#014H
    000011 2E1000      R  ADD      R1,#LOW tt
    000014 A5E7           MOV      A,@R1         ; A=R11
    000016 F500        R  MOV      test,A        ; A=R11
                                                 ; SOURCE LINE # 13
    000018 22             RET      
    ;       FUNCTION main (END)
    
    

    This looks about right to me. Can you give me a complete example showing the problem that you get?

    Thanks,
    Jon

Reply
  • Joel,

    I got the following:

    stmt  level    source
    
        1          char tt[30] ;
        2          char test ;
        3          unsigned char aaa = 27 ;
        4          unsigned int  bbb = 27 ;
        5          
        6          void main (void)
        7          {
        8   1      
        9   1      test = tt[aaa - 20] ;
       10   1      test = tt[bbb - 20] ;
       11   1      
       12   1      
       13   1      }
       14          
       15          
       16          
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    ;       FUNCTION main (BEGIN)
                                                 ; SOURCE LINE # 6
                                                 ; SOURCE LINE # 9
    000000 7E1100      R  MOV      R1,aaa
    000003 2E1000      R  ADD      R1,#LOW tt+236
    000006 A5E7           MOV      A,@R1         ; A=R11
    000008 F500        R  MOV      test,A        ; A=R11
                                                 ; SOURCE LINE # 10
    00000A 7E0500      R  MOV      WR0,bbb
    00000D 9E040014       SUB      WR0,#014H
    000011 2E1000      R  ADD      R1,#LOW tt
    000014 A5E7           MOV      A,@R1         ; A=R11
    000016 F500        R  MOV      test,A        ; A=R11
                                                 ; SOURCE LINE # 13
    000018 22             RET      
    ;       FUNCTION main (END)
    
    

    This looks about right to me. Can you give me a complete example showing the problem that you get?

    Thanks,
    Jon

Children
No data