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.
If I have the address of particular function, is it possible load the function in assembly? I understand that usually the function is loaded as LCALL _function but I was wondering whether it is possible to load the function with its address? Appreciate anyone who can offer me advise on this. Thank you.
Can you clarify your question? Do you mean that you want to call a function in assembly language when you only have the address of the function to be called stored in a variable or registers? It is possible to use something like the (untested) code below. Basicly, the return address and the function pointer are loaded onto the stack and then a RET instruction causes the CPU to pop the address of the function pointer into the PC. When the function executes a RET, the return address will be popped into the PC and the CPU will be back at return_address.
MOV A,#LOW(return_address) PUSH Acc MOV A,#HIGH(return_address) PUSH Acc MOV A,function_pointer_low PUSH Acc MOV A,function_pointer_high PUSH Acc RET retern_address: ....
Basicly, the return address and the function pointer are loaded onto the stack and then a RET instruction causes the CPU to pop the address of the function pointer into the PC. That technique has no excuse being used on the 8051, which has a perfectly fine "computed goto" instruction:
JMP @A+DPTR
Hi, Sometimes it needs to keep DPTR unmodified, for example, if you pass a parameter to called function via DPTR. So the usage of "call-via-stack" method is still usable with C51. Regards, Oleg
Thank you all for your replies. To clarify my question, what I needed to do was to call a function whose address is stored in variables. Anyway, as suggested, I have tried by loading the return address and the function onto the stack and it is working fine. Thank you.