搜索此博客

2012年9月5日星期三

Jump to uncertain (run-time determined) address in assembly language

Sometimes I want program to jump to uncertain (run-time determined) address like function pointer does in C language. For example, a function table. If the chip supports "jsr" with register indirect addressing, like H8, it will be as simple as
    jsr   @er0
But on the core I am using called "Flex RISC", 8bit, performs as peripheral device in an ARM based SoC, thers is only one "jsr" instruction which using immediate addressing. Branch instructions (brXX) and jmp instruction do the same.
At last I found there is another instruction changes PC pointer, "rts". It pop PC point from stack. So here is the way out:
    ...
variable SomeAddr keeps SomeFunc's sddress
    ld     a1, SomeAddr
    ld     a2, SomeAddr + 1
    jsr    CallByAddr
    ...

SomeFunc:
    ...
    rts          ; this "rts" will exactly returns from the "jsr".

CallByAddr:
    psh    a2
    psh    a1
    rts          ; this "rts" will pop a2a1 to PC that makes SomeFunc called.

没有评论:

发表评论