We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
You need to read the C51 Manual to determine its requirements for register usage etc. The section titled "Interfacing C to Assembler" would seem like an obvious place to start... http://www.keil.com/support/man/docs/c51/c51_ap_ctoasm.htm
Or even more directly: http://www.keil.com/support/man/docs/c51/c51_ap_regusage.htm Though it's probably a good idea to read that whole section of the manual.
...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.
Hi Graham, Thank you very much for your kindly help. You know my concern exactly and you give me the most useful answer. Best Regards, H.K. Chan