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

help!why not use Rx as loop counter?

I have a problem. I write a function in another c source file not main file, in this function nothing do, and I declare the function as extern. In main function I invoke it 200 times. However, the compiler does not use registerbank as counter. Why?

extern void func1(void);

void func2(void)
{
        unsigned char data i;
        for(i=200;i;i--);
}

main()
{
        unsigned char data i,j;
        for(i=200;i;i--)        // store i in 0x08
        {
                func1();
        }
        for(j=200;j;j--)        // store j in R6
        {
                func2();
        }
}
the assembly code:
C:0x0003    7508C8   MOV      0x08,#TMR2CN(0xC8)
    19:         {
    20:                 func1();
C:0x0006    120025   LCALL    func1(C:0025)
    21:         }
C:0x0009    D508FA   DJNZ     0x08,C:0006
    22:         for(j=200;j;j--)
C:0x000C    7EC8     MOV      R6,#TMR2CN(0xC8)
    23:         {
    24:                 func2();
C:0x000E    120020   LCALL    func2(C:0020)
    25:         }
C:0x0011    DEFB     DJNZ     R6,C:000E

Parents
  • I read the Keil C51 user manual. I find that the REGUSE directive specifies which registers are modified by the specified function. So I add following code in func1() source file:
    #pragma asm
    $REGUSE func1()
    #pragma endasm
    and I get the assembly code:

        61:         MOV     R7,#0C8H
        62: ?C0002:
        63: ;       {
        64:                         ; SOURCE LINE # 27
        65: ;             func1();
        66:                         ; SOURCE LINE # 28
    C:0x0003    7FC8     MOV      R7,#TMR2CN(0xC8)
        67:         LCALL   func1
        68: ;       }
        69:                         ; SOURCE LINE # 29
    C:0x0005    120022   LCALL    FUNC1(C:0022)
        70:         DJNZ    R7,?C0002
    

    and the problem fixed. Many thanks,everybody.

Reply
  • I read the Keil C51 user manual. I find that the REGUSE directive specifies which registers are modified by the specified function. So I add following code in func1() source file:
    #pragma asm
    $REGUSE func1()
    #pragma endasm
    and I get the assembly code:

        61:         MOV     R7,#0C8H
        62: ?C0002:
        63: ;       {
        64:                         ; SOURCE LINE # 27
        65: ;             func1();
        66:                         ; SOURCE LINE # 28
    C:0x0003    7FC8     MOV      R7,#TMR2CN(0xC8)
        67:         LCALL   func1
        68: ;       }
        69:                         ; SOURCE LINE # 29
    C:0x0005    120022   LCALL    FUNC1(C:0022)
        70:         DJNZ    R7,?C0002
    

    and the problem fixed. Many thanks,everybody.

Children