Loading address relative to the PC

Started by Dave Ubbin, August 04, 2011, 01:26:33 AM

Previous topic - Next topic

Dave Ubbin

How can I store the address of an instruction? E.g. if I wanted to store the address of the NOP (at label1) in r8 I'd like to be able to do something like this:

    lwz r8, 8(pc)
    b somewhere

label1:
    nop
That won't do it, of course, so what is the proper way? Do I have to make a dummy branch-and-link?

dcx2

#1
Quote from: Dave Ubbin on August 04, 2011, 01:26:33 AM
Do I have to make a dummy branch-and-link?

Pretty much.  I call it the "bl trick".  I first saw brkirch use it for SMG2 moon jump.  I have an example using the bl trick here.  http://wiird.l0nk.org/forum/index.php/topic,6517.0.html

If you're using a USB Gecko and you're looking at a breakpoint, SRR0 (Save and Restore Register 0) will tell you the instruction pointer/program counter address.

If you just want the address of an instruction and you're not putting data in-line with your ASM that would require branching over it, then you can turn any conditional branch into a conditional linking branch.  A conditional linking branch will always put the address of the next instruction into the LR, regardless of whether the condition was true or false.

EDIT:

btw, nice first post.  You seem like you already know what you're doing to some extent.

Welcome to the community.