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

Load function in assembly

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:
        ....
    

  • whether it is possible to load the function with its address?

    Your terminology is somewhat strange. What you're doing here is usually referred as "calling" a function", not "loading" it.

    That set aside, it's completely unclear what it is you want to do. Note that _function is the address of that function, effectively --- _function is just a more convenient way the assembler and tools allow you to write it.

  • 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
    

    There are platforms where indeed you need such stack hacks to do a computed jump, but the 8051 is, fortunately, not one of them.

  • Hi,
    what is a problem?

    CALL function_address
    so type
     CALL 0x1234
    where 0x1234 is the address of function you have.

    Regards,
    Oleg

  • 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.