Moldevort
Moldevort

Reputation: 193

Reverse PowerPC's rlwinm

I'm currently looking into understanding and modifying some source code for a game mod I found. At start some adjustments are made by using this:

rlwinm r3, r3, 0, 29, 25

I'm now trying to find a way to reverse this very adjustment later on in the game - so I don't want to remove it from the game completely, just make it reversible when certain conditions are met.

What's the assembly command for reversing the change this line did?

Upvotes: 1

Views: 140

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 365537

rlwinm (Rotate Left Word Immediate Then AND with Mask) includes an AND, so some of the original information is gone. You'll have to save the original r3 value somewhere so you can restore it later.

As a simpler example, consider and reg, 0x0f. The information necessary to undo it (the state of the higher bits) is gone, since the destination and source are the same register so the source was overwritten with the modified value.

And yes, this rlwinm does zero some bits; the bit-range for the mask doesn't keep all 32 bits and only rotate. In fact the shift (rotate) count is 0, the first immediate operand in the asm source syntax. So it's just using it for an immediate AND. It's doing something like r3 &= ~0x0000003c, (1<<(31-25)) - (1<<(31-29)) since IBM numbers bits backwards, with MSB=0 and LSB=31 or 63 depending on operand-size!

I may have an off-by-one in one or both of the mask-begin and mask-end immediates; I didn't read the docs super carefully, but 0x3c or ~0x3c seems like a plausible mask value. I think it's actually clearing a few bits and keeping the rest since mask-begin is higher than mask-end. So ~0x3c. Unless that bit-field is saved elsewhere so you could restore it with a rotate-and-insert, it doesn't really matter exactly what masking this rlwinm is doing, just that it's a non-reversible operation.

Upvotes: 3

Related Questions