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

How can I get an efficient loop?

I have written the following code to perform a copy procedure between RAM and FLASH Memory:

unsigned char i;
i = 0x00;
do{
i --;
*(&FLASHADDRESS + j + i) = DataBuffer[i];
}while(i != 0x00);

Is there any better way to generate efficient(like DJNZ in assembler) code? :)
THS.

Parents
  • The DJNZ instruction is used wherever possible in do...while, while, and for loops. So, there' is no code reorganization that is required.

    line level    source
    
       1          void main (void)
       2          {
       3   1      volatile unsigned char i;
       4   1
       5   1      while (--i != 0x00)
       6   1        {
       7   2        }
       8   1
       9   1      for (i=100; i; --i)
      10   1        {
      11   2        }
      12   1
      13   1      do
      14   1        {
      15   2        }
      16   1      while (--i);
      17   1      }
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 1
                                               ; SOURCE LINE # 2
    0000         ?C0001:
                                               ; SOURCE LINE # 5
    0000 D500FD      R     DJNZ    i,?C0001
                                               ; SOURCE LINE # 6
                                               ; SOURCE LINE # 7
    0003         ?C0002:
                                               ; SOURCE LINE # 9
    0003 750064      R     MOV     i,#064H
    0006         ?C0003:
                                               ; SOURCE LINE # 10
                                               ; SOURCE LINE # 11
    0006 D500FD      R     DJNZ    i,?C0003
                                               ; SOURCE LINE # 14
                                               ; SOURCE LINE # 15
    0009         ?C0006:
                                               ; SOURCE LINE # 16
    0009 D500FD      R     DJNZ    i,?C0006
                                               ; SOURCE LINE # 17
    000C 22                RET
                 ; FUNCTION main (END)
    

    Jon

Reply
  • The DJNZ instruction is used wherever possible in do...while, while, and for loops. So, there' is no code reorganization that is required.

    line level    source
    
       1          void main (void)
       2          {
       3   1      volatile unsigned char i;
       4   1
       5   1      while (--i != 0x00)
       6   1        {
       7   2        }
       8   1
       9   1      for (i=100; i; --i)
      10   1        {
      11   2        }
      12   1
      13   1      do
      14   1        {
      15   2        }
      16   1      while (--i);
      17   1      }
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 1
                                               ; SOURCE LINE # 2
    0000         ?C0001:
                                               ; SOURCE LINE # 5
    0000 D500FD      R     DJNZ    i,?C0001
                                               ; SOURCE LINE # 6
                                               ; SOURCE LINE # 7
    0003         ?C0002:
                                               ; SOURCE LINE # 9
    0003 750064      R     MOV     i,#064H
    0006         ?C0003:
                                               ; SOURCE LINE # 10
                                               ; SOURCE LINE # 11
    0006 D500FD      R     DJNZ    i,?C0003
                                               ; SOURCE LINE # 14
                                               ; SOURCE LINE # 15
    0009         ?C0006:
                                               ; SOURCE LINE # 16
    0009 D500FD      R     DJNZ    i,?C0006
                                               ; SOURCE LINE # 17
    000C 22                RET
                 ; FUNCTION main (END)
    

    Jon

Children
No data