Page 84 - ARM 64 Bit Assembly Language
P. 84

Load/store and branch instructions 69

                     bottom of the stack), and stores the procedure link register (x30)at Mem[sp + 8],which
                     completely fills the 16 bytes that were allocated on the stack.

                    1         stp    x29, x30, [sp, #-16]!

                     The opposite of the previous instruction can be used to restore the values of the frame pointer
                     and procedure link registers:

                    1         ldp    x29, x30, [sp], #16

                     Here x29 is set to Mem[sp] and x30 is set to Mem[sp + 8]. Then, sp is incremented
                     by 16, essentially deleting the values of x29 and x30 from the stack. Although the bits
                     are still there, they may be overwritten by subsequent instructions that store data on the
                     stack.

                     The following assembly program demonstrates the two instructions used together to save and
                     restore a pair of registers.

                    1         .text
                    2         .type  main, %function
                    3         .global main
                    4  main:
                    5         stp    x29, x30, [sp, #-16]!  // Store x29 and x30 on the stack
                    6         mov    x29, #0x0              // change their contents
                    7         mov    x30, #0x0
                    8         ldp    x29, x30, [sp], #16    // Restore x29 and x30, and sp
                    9         ret
                   10         .size  main, (. - main)

                     The following function demonstrates how to use the stp and ldp instructions to save, and
                     later restore, all of the non-volatile registers.

                    1         .text
                    2         .type  exmpl, %function
                    3         .global exmpl
                    4  exmpl:
                    5         stp    x19, x20, [sp, #-16]!  // Store x19 and x20 on the stack
                    6         stp    x21, x22, [sp, #-16]!  // Store x21 and x22 on the stack
                    7         stp    x23, x24, [sp, #-16]!  // Store x23 and x24 on the stack
                    8         stp    x25, x26, [sp, #-16]!  // Store x25 and x26 on the stack
                    9         stp    x27, x28, [sp, #-16]!  // Store x27 and x28 on the stack
                   10         stp    x29, x30, [sp, #-16]!  // Store x29 and x30 on the stack
                   11
                   12         // The function can now use x19 through x30. If it calls another
                   13         // function, then it is guaranteed that these registers will not
                   14         // be modified by the function that was called.
                   15         // INSERT FUNCTION BODY HERE
   79   80   81   82   83   84   85   86   87   88   89