• AVR Freaks

Hot!$S or $T used as temporary registers in assembly

Author
henkebenke
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2008/09/27 23:55:51
  • Location: 0
  • Status: offline
2020/01/23 08:06:55 (permalink)
0

$S or $T used as temporary registers in assembly

Sorry I'm bothering you with such a trivial question, but the internet is vast and I cannot find an answer.
 
According to the manual can S0 - S7 be used by a called function, without preserving them. It's said that the Caller has to preserve them.
But in the MPLAB X Help file for XC32, there is a code example:
 
asmFunction:
   /* function prologue - save registers used in this function
     * on stack and adjust stack-pointer
     */
   addiu   sp, sp, -4
   sw      s0, 0(sp)
 
   la      s0, LATACLR
   sw      a0, 0(s0)      /* clear specified bits */
 
I'm just showing the beginning.  Here, the S0 register is saved, even though the caller is supposed to preserve these registers before calling this function. Wouldn't the example code have used the T0 register instead?
 
Well, there is an ambiguity here, and I'm glad if I don't will discover the solution to this mystery the hard ( nerve wrecking ) way.
 
So, the key question is;  are the S registers the ones that do not need to be backed up by the called function? Or is it the T ones?
 
Thanks.
 
 
 
 
#1

12 Replies Related Threads

    andersm
    Super Member
    • Total Posts : 2741
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/23 08:16:14 (permalink)
    0
    S0-s7 are callee-saved registers, ie. a called function must preserve their values. T0-t9 are caller-saved registers, and can be used without saving them. See section 12.3 of the XC32 manual.
    #2
    simong123
    Lab Member No. 003
    • Total Posts : 1359
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/23 08:26:39 (permalink)
    5 (1)
    Which manual?.
    The Callee (not Caller) is supposed to preserve s0-s7.
    i.e. the example code is correct.
     
    See https://en.wikipedia.org/wiki/Calling_convention#MIPS
    or search for mips32abi.pdf
    #3
    henkebenke
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2008/09/27 23:55:51
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/25 13:34:00 (permalink)
    0
    Ha!   confusing!
     
    Wikipedia:
    For the N32 and N64 ABIs, a function must preserve the $S0-$s7 registers, the global pointer ($gp or $28), the stack pointer ($sp or $29) and the frame pointer ($30).
     
    XC32 help: 
    s0-s7
    Temporary registers whose values are saved across function calls.
     
    The Wiki version suggests that a function must preserve s0-s7 on function entry.
    The XC32 help is more ambiguous.  I interpreted it as a calling function must backup them before calling. But I assume the Wikipedia version is right.
     
    I would have preferred this way of putting it:
     
    S0-S7 ;  a called function may not use these registers without backing them up on entry, and restoring them on exit. 
     
    A typical problem with engineers. They know everything, and they can't imagine how it feels not to understand everything, so they cannot really figure out what information is more important than others. That's why manuals always will be a mess.
     
    OK, do you agree with Wikipedia?   The t0-t9 would be the ones to use.
    #4
    NorthGuy
    Super Member
    • Total Posts : 5917
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/25 14:29:04 (permalink)
    5 (1)
    henkebenke
    A typical problem with engineers. They know everything, and they can't imagine how it feels not to understand everything, so they cannot really figure out what information is more important than others. That's why manuals always will be a mess.



    Manuals are not written by engineers. I believe they would be much better if they were.
    #5
    andersm
    Super Member
    • Total Posts : 2741
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/25 15:10:41 (permalink)
    0
    XC32 uses the O32 ABI. The N32 and N64 ABIs only exist for 64-bit MIPS CPUs.
     
    henkebenkeXC32 help: 
    s0-s7
    Temporary registers whose values are saved across function calls.
     
    The Wiki version suggests that a function must preserve s0-s7 on function entry.
    The XC32 help is more ambiguous.  I interpreted it as a calling function must backup them before calling. But I assume the Wikipedia version is right.

    They say the same thing. If a register's value is preserved across a function call, then the called function must save the register before touching it.
    post edited by andersm - 2020/01/25 15:16:34
    #6
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: $S or $T used as temporary registers in assembly 2020/01/25 16:25:01 (permalink)
    0
    I think the OP would prefer if "are" was changed to "must be". :)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/25 16:57:32 (permalink)
    0
    The caller is responsible for saving and restoring any of the following caller-saved registers that it cares about: $t0-$t9, $a0-$a3, and $v0-$v1. In other words, the callee may freely modify these registers, under the assumption that the caller already saved them if necessary.
     
    The callee is responsible for saving and restoring any of the following callee-saved registers that it uses: $s0-$s7, $gp, $sp, $fp (or $s8), and $ra. So the caller may assume these registers are not changed by the callee.
     
    Be very careful when writing nested functions which act as both a caller and a callee. ;)
     
    post edited by 1and0 - 2020/01/25 18:40:09
    #8
    andersm
    Super Member
    • Total Posts : 2741
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/25 16:58:59 (permalink)
    0
    Then the question becomes must be saved by whom? Most literature uses the terms caller- and callee-saved registers.
    #9
    mjr93
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/12/20 03:53:34
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/01/28 03:49:10 (permalink)
    0
    This post was helpfull, thanks
    #10
    henkebenke
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2008/09/27 23:55:51
    • Location: 0
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/02/08 10:55:48 (permalink)
    0
    Ha, the confusion continues. I found this in "Section 50 - CPU" DS60001192B.
     
    r8 - r15 t0-t7 Temporary – Caller does not need to preserve contents.
    r16 - r23 s0-s7 Saved Temporary – Caller must preserve contents
     
    Now that is totally wrong!
     
    I have now witnessed three pieces of information in this matter.
     
    1.  The one in the Help file of MPLABX - that one is ambiguous.
     
    2. Wikipedia - that one is correct.  ( Wikipedia is always correct )
     
    3 Section 50 - that one is wrong.
     
    This is actually an important topic.  Who wants to save registers when he don't need to?
    But now you know guys, Wikipedia is always right!
    #11
    NorthGuy
    Super Member
    • Total Posts : 5917
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/02/08 14:09:43 (permalink)
    0
    henkebenke
    This is actually an important topic.  Who wants to save registers when he don't need to?



    Write the whole thing in assembler. Then you don't need to worry about calling conventions - everything is entirely up to you. You will need to save even less :)
    #12
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: $S or $T used as temporary registers in assembly 2020/02/08 14:59:35 (permalink)
    0
    NorthGuy
     
    Write the whole thing in assembler. Then you don't need to worry about calling conventions - everything is entirely up to you. You will need to save even less :)

    +1
     
    Those conventions are not enforced by the hardware or assembler. It is the programmers agreeing to them so functions written by different people can interface with each other. When you write the whole thing in assembly yourself, you're in total control. ;)
     
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5