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

Pushing Register in ASM function

Dear all,

When I am writing some ASM function, I always need to PUSH the accumulator and all register I have used inside the function. I am wondering if this ASM function is to be called by other C function, do I still need to PUSH the accumulator and the register? It seems that the C compiler will avoid saving some useful data into the accumulator and the register that may need to be retrived after the ASM function call.

Best Regards,
H.K. Chan

Parents
  • ...if this ASM function is to be called by other C function, do I still need to PUSH the accumulator and the register?

    When C calls a function, the compiler assumes the worst - i.e. that all the registers will be overwritten. So, no, you don't have to save and restore all the register values in a ASM function.

    Of course, you do have to take proper account of any variables passed to or returned by the ASM function.

    However, if you are using global register optimisation, then the C compiler will take advantage of knowing which registers are overwritten by a function. This can have a significant effect on the speed and size of code. In the case of ASM functions, you can use the $REGUSE keyword to tell the compiler which registers are changed by your function.

    For an example, see: http://www.keil.com/forum/docs/thread1252.asp

    In the case of an ASM function which is called frequently or is time critical, it may be worth saving and restoring some registers to get the maximum benefit of global register optimisation. The registers most worth leaving unchanged are R0...R7 and the DPTR.

Reply
  • ...if this ASM function is to be called by other C function, do I still need to PUSH the accumulator and the register?

    When C calls a function, the compiler assumes the worst - i.e. that all the registers will be overwritten. So, no, you don't have to save and restore all the register values in a ASM function.

    Of course, you do have to take proper account of any variables passed to or returned by the ASM function.

    However, if you are using global register optimisation, then the C compiler will take advantage of knowing which registers are overwritten by a function. This can have a significant effect on the speed and size of code. In the case of ASM functions, you can use the $REGUSE keyword to tell the compiler which registers are changed by your function.

    For an example, see: http://www.keil.com/forum/docs/thread1252.asp

    In the case of an ASM function which is called frequently or is time critical, it may be worth saving and restoring some registers to get the maximum benefit of global register optimisation. The registers most worth leaving unchanged are R0...R7 and the DPTR.

Children