Updating the stack pointer

The PowerPC® stwu (Store Word with Update) instruction is used for computing the new SP and saving the back chain.

The PowerPC® stwu (Store Word with Update) instruction is used for computing the new SP and saving the back chain. This instruction has a signed 16-bit displacement field that can represent a maximum signed value of 32,768. A stack frame size greater than 32K bytes requires two instructions to update the SP, and the update must be done atomically.

The two assembly code examples illustrate how to update the SP in a prolog.

To compute a new SP and save the old SP for stack frames larger than or equal to 32K bytes:

addis   r12, r0, (<-frame_size> > 16) & 0XFFFF
                        # set r12 to left half of frame size
ori     r12, r12 (-frame_size> & 0XFFFF
                        # Add right halfword of frame size
stwux   r1, r1, r12     # save old SP and compute new SP
To compute a new SP and save the old SP for stack frames smaller than 32K bytes:

stwu   r1, <-frame_size>(r1)   #update SP and save caller's SP