WiiRd forum

Wii & Gamecube Hacking => Wii Game hacking help => Topic started by: dcx2 on February 21, 2010, 04:02:19 PM

Title: Can't stw with r0 pointer?
Post by: dcx2 on February 21, 2010, 04:02:19 PM
So I was trying to write an ASM code today.  One of the lines was something like

stw r4,0(r0)

The code was freezing the game.  So I tried stepping through, and it appears that the PPC is trapping the instruction and halting execution.  Every time I hit "Step" the instructions never progresses forward...it's the same symptom as trying to use a null pointer for stw/lwz.

I switched the the ASM code to use

stw r4,0(r3)

and it started working fine.

Has anyone else had this happen to them?

EDIT: by the way, just to be clear, lwz r4,0(r0) works.  stw r4,0(r0) doesn't.

Post Merge: February 21, 2010, 05:07:22 PM

Heh, so after looking at the PPC ASM reference, I noticed that you can't actually use r0 as the pointer for stw.

http://pds.twi.tudelft.nl/vakken/in1200/labcourse/instruction-set/stw.html

If the bits for rA are 0, then instead of getting the pointer in r0 and adding d to it, it just uses d!

So stw r4,0(r0).  Here, rA is r0, and d is 0.  Since rA is 0, it just uses d to compute the address for the stw, and since d is 0, we were actually dereferencing a null pointer, wihch was causing the trap.

Post Merge: February 21, 2010, 04:11:48 PM

And it looks like lwz actually has the same problem as stw?  If rA = r0 then it ignores the address in r0 too.

http://pds.twi.tudelft.nl/vakken/in1200/labcourse/instruction-set/lwz.html
Title: Re: Can't stw with r0 pointer?
Post by: Maniac on February 22, 2010, 12:57:55 AM
There are certain registers that you don't want to use because they cause problems like this.
Title: Re: Can't stw with r0 pointer?
Post by: dcx2 on February 22, 2010, 02:10:03 AM
Well yeah, you don't want to use r1 because you'd mess up the stack, or r2, and you have to be careful that you don't overwrite a register that's in use.

But my problem wasn't any of that.  The problem is that stw and lwz behave differently when rA = r0.  Other instructions, like addi (http://pds.twi.tudelft.nl/vakken/in1200/labcourse/instruction-set/addi.html), have the same flaw.

But not all instructions are like this.  ori (http://pds.twi.tudelft.nl/vakken/in1200/labcourse/instruction-set/ori.html) doesn't have this quirk.