Special purpose registers

Started by benny3t3, November 24, 2010, 08:11:41 PM

Previous topic - Next topic

benny3t3

I have a question, What is a special purpose register, alot of times I will find 'mtlr' and 'mflr'.

For example:

mtlr r0
add. r0,r4,r0
mflr r0

What does that mean?
(I just made that up, chances are I messed up the syntax)

Link

mtlr = move to lr (link register)
mflr = move from lr

Basically the lr register is used for branch commands when a function has finished running. When you use breakpoints you always see the LR value.


When the Power PC executes the command blr it means "Branch back to link register value". Normally if you run a bl (branch and link - comparable to call in x86 assembly) command your Wii will overwrite the lr register and when this sub procedure executes blr it will resume under the bl command.

dcx2

Any register that's not r0-r31 or f0-f31 is a special purpose register.  That is, you can't use it as an operand for most instructions.

So you can't addi lr, r3, 4.  lr is not allowed to be an operand for addi.  You would need to mflr into a normal register, then addi with it, then mtlr back to lr.

There are other SPRs.  ctr, srr0, cr and so on.

ctr is used for loops.  Certain instructions implicitly used the ctr register, like bdnz.

srr0 holds the current instruction during an interrupt.  If you modify this during a breakpoint you can make the game "jump" to different places; useful for recovering from some crashes.

cr holds the result of any computation in terms of less than, equal to, or greater than.  Many instructions like branches (beq, blt, bge, etc) implicitly use the cr.  For instance, blt will branch if the lt bit of the cr is set.  Otherwise, execution continues to the next instruction.

benny3t3

Thanks guys, that helps some... But what would really help is some help with my first code step by step, I know that you were helping me before dcx2 and you told me to list all of the registers and their values and copy/paste the function. Sorry that I never got to that, I can do that now...