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

Necessary to save used registers in assembly ?

I am using assembly language routines
called from C code.
Within the assembly routines, is it necessary to save the registers used?

Ex:
MY_ASM_ROUTINE:
PUSH ACC ; necessary ?
CLR A ; overwrite ACC
POP ACC
RET

main(){
MY_ASM_ROUTINE();
}

Or does the C compiler assume that
registers can be overwritten by
calls to routines?

I couldn't find the answer in the manuals.

Thanks in advance
Jim Burke

Parents
  • >1. There is no difference between a C function and an assembly function.

    >5. The regfile is used to perform
    >global register optimizations.
    >Basically, a bipmap of the registers
    >used in each function is built up and
    >used by the C compiler to optimize >saving and restoring registers.

    I think you forget about $REGUSE directive.

    Sentence (1.) is not true.
    Main difference between C and Assembler functions is that C compiler don't know anything about using registers in assembler function.

    So, to force compiler apply 'global optimisation' under assembler functions user MUST explicitly declare used registers with $REGUSE directive.

    For example:
    --- file SWAPBYTE.C ---

    #pragma  SRC(SWAPBYTE.a51)
    
    
    #pragma asm
       $REGUSE _SwapByte(A, R7 )
    #pragma endasm
    
    uchar SwapByte(unsigned char Byte)
    {
      ACC = Byte;
    #pragma asm
      swap a
    #pragma endasm
      return( ACC );
    }
    



    P.S.
    Without $REGUSE compiler generate code for all callers of SwapByte() with reloading registers R0,R1,...,R6,DPTR,B.

    Adding lines:
    #pragma asm
       $REGUSE _SwapByte(A, R7 )
    #pragma endasm
    
    in my current project reduce code size
    from 44639 to 44489 bytes.


Reply
  • >1. There is no difference between a C function and an assembly function.

    >5. The regfile is used to perform
    >global register optimizations.
    >Basically, a bipmap of the registers
    >used in each function is built up and
    >used by the C compiler to optimize >saving and restoring registers.

    I think you forget about $REGUSE directive.

    Sentence (1.) is not true.
    Main difference between C and Assembler functions is that C compiler don't know anything about using registers in assembler function.

    So, to force compiler apply 'global optimisation' under assembler functions user MUST explicitly declare used registers with $REGUSE directive.

    For example:
    --- file SWAPBYTE.C ---

    #pragma  SRC(SWAPBYTE.a51)
    
    
    #pragma asm
       $REGUSE _SwapByte(A, R7 )
    #pragma endasm
    
    uchar SwapByte(unsigned char Byte)
    {
      ACC = Byte;
    #pragma asm
      swap a
    #pragma endasm
      return( ACC );
    }
    



    P.S.
    Without $REGUSE compiler generate code for all callers of SwapByte() with reloading registers R0,R1,...,R6,DPTR,B.

    Adding lines:
    #pragma asm
       $REGUSE _SwapByte(A, R7 )
    #pragma endasm
    
    in my current project reduce code size
    from 44639 to 44489 bytes.


Children
No data