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

Concept of Scratch and Preserved Registers

Hi,

I am confused in understanding the difference between scratch and preserved registers. I have read the development guide of IAR Embedded Workbench but the topic was written in such a way that I could not get the real sence of it.
Any help or if possible some example that can help me differenciating between the both will be regarded.

Thanks in advance
Parents
  • Note: This was originally posted on 5th April 2011 at http://forums.arm.com

    I've not used IAR's tools, but I'm assuming it follows the AAPCS (Procedure Call Standard for the ARM Architecture).  The AAPCS can be summarized as:

      r0-r3    - parameter passing, and return value
      r4-r11 - protected
      r12  - scratch

    So what does this mean?  Well lets say you have some code like this:


    main
      ...
      BL   foo
      ...

    foo
      ...
      BX  lr


    Main() is the caller, and foo() is callee.  That is, main() is calling foo().  So the AAPCS rules say that main() must put the arguments to foo() in r0 to r3.  It can expect the return value of foo() in r0.  It can also assume that when foo() returns r4-r11 will have the same values as they did before the call.

    Foo() can assume its arguments are in r0-r3, and it knows it must put its return value in r0.  It also knows that it can corrupt r12, using it as a scratch register.  For r4-r11, foo() can use these registers - BUT ONLY IF IT RESTORES THE ORIGINAL VALUES BEFORE RETURNING.
Reply
  • Note: This was originally posted on 5th April 2011 at http://forums.arm.com

    I've not used IAR's tools, but I'm assuming it follows the AAPCS (Procedure Call Standard for the ARM Architecture).  The AAPCS can be summarized as:

      r0-r3    - parameter passing, and return value
      r4-r11 - protected
      r12  - scratch

    So what does this mean?  Well lets say you have some code like this:


    main
      ...
      BL   foo
      ...

    foo
      ...
      BX  lr


    Main() is the caller, and foo() is callee.  That is, main() is calling foo().  So the AAPCS rules say that main() must put the arguments to foo() in r0 to r3.  It can expect the return value of foo() in r0.  It can also assume that when foo() returns r4-r11 will have the same values as they did before the call.

    Foo() can assume its arguments are in r0-r3, and it knows it must put its return value in r0.  It also knows that it can corrupt r12, using it as a scratch register.  For r4-r11, foo() can use these registers - BUT ONLY IF IT RESTORES THE ORIGINAL VALUES BEFORE RETURNING.
Children
No data