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

Routine calls in Arm

Note: This was originally posted on 19th July 2010 at http://forums.arm.com

I have a quetion about mechanism used while making nested routine calls in ARM based products.

Say suppose at an instant my core is executing the routine A and routine B is invoked by encountering the instruction.

As it tells, the address of the immidiate instruction of routine A from where the execution turned its stream towards B is stoed on list register. Uptill this it is clear like crystal.

But what if now while routine B is executing any interrupt occurs or any call is made to say routine C . . .
Now we need to store the address of the next instruction of the B routine . .
If we store it onto the list register then how can we preserve its previous value which points to immediate instruction of routine A.

Pls eloberate on this.


Thanks in advance,
  • Note: This was originally posted on 20th July 2010 at http://forums.arm.com

    Ignoring specific optimisations, the general solution is to have a "stack" in memory, and to push the old link-register value onto the stack before making another call. Arbitrary depth calls can then be made until memory is exhausted. Example code might be:

    myfunc
      PUSH {LR}; store old return address onto the stack
      BL myfunc; call myfunc again / another procedure
      POP {LR} ; restore old return address from stack
      BX LR ; return from myfunc


    hth
    s.



    So this is the programmer's responcibility to do strack operation before making the jump and doing the tghings in reverse order when subroutine is complete... M I right?

    Now if this is the case then how about the interrupt processing? How is the List register value is managed there?
  • Note: This was originally posted on 19th July 2010 at http://forums.arm.com

    Ignoring specific optimisations, the general solution is to have a "stack" in memory, and to push the old link-register value onto the stack before making another call. Arbitrary depth calls can then be made until memory is exhausted. Example code might be:

    myfunc
      PUSH {LR} ; store old return address onto the stack
      BL myfunc ; call myfunc again / another procedure
      POP {LR}  ; restore old return address from stack
      BX LR  ; return from myfunc


    hth
    s.
  • Note: This was originally posted on 20th July 2010 at http://forums.arm.com

    Yes, the programmer is responsible for preserving the link-register during a procedure call.
    For exception handling, the hardware automatically pushes the link-register, along with over relevant state, onto the stack before it starts executing the interrupt handler code.

    hth
    s.
  • Note: This was originally posted on 21st July 2010 at http://forums.arm.com

    For exception handling, the hardware automatically pushes the link-register, along with over relevant state, onto the stack before it starts executing the interrupt handler code.


    The hardware only does this automatically for the Cortex-M processors.  Other ARM processors have a more complex exception model that involves having different "mode"s each with different stack pointer (r13) and taking an exception causes a mode change.  [This is a somewhat oversimplified description.]  You can find the details in the ARMARM (ARM Architecture Reference Manual) at infocenter.arm.com.]